是否可以从属性文件中加载嵌套占位符?我正在尝试动态加载URL.
例如,如果我的属性文件包含
my.url=http://localhost:8888/service/{nestedProperty}/
Run Code Online (Sandbox Code Playgroud)
有没有办法在运行时加载{nestedProperty}的值?与ResourceBundle的行为类似.如果是这样,我将如何有效地实例化String?到目前为止,我在想
<bean id="myURLString" class="java.lang.String" scope="prototype" lazy-init="true">
<property name="URL" value="${my.url}" />
</bean>
Run Code Online (Sandbox Code Playgroud)
...但我不确定要嵌套什么属性.如果可能的话,我想使用Annotations获取bean,尽管我目前有一些东西
ctx.getBean("myURLString", String.class, new Object[] { nestedProperty} );
Run Code Online (Sandbox Code Playgroud)
我在这里查看了PropertyPlaceholderConfigurer和其他几个属性文件问题,但我似乎无法弄清楚这是否可行.
我还应该注意,我想从我的代码中动态加载这个嵌套属性,或者至少从那里操作它们(可能通过@PostConstruct?)
我写了一个小应用程序.我已将数据库特定信息放在属性文件中.
db.url=jdbc:mysql://localhost:3306/librarydb
db.user=root
db.passwd=pas$w0rd
Run Code Online (Sandbox Code Playgroud)
当我构建应用程序以获取可执行jar文件时,属性文件包含在其中.将这些信息放在属性文件中的全部目的是允许用户更改这些信息,但这使得它变得不可能.
如何包含不包含jar文件的属性文件?有点像你在.NET应用程序中使用AppSettings文件.
我是Java的新手,所以如果你能把它分解为一系列步骤我会很感激.
谢谢.
编辑:
这就是我从程序中的属性文件中获取值的方法.
public class DatabaseHandler {
public static Connection connectToDb() {
Connection con = null;
Properties props = new Properties();
InputStream in = null;
try {
in = DatabaseHandler.class.getResourceAsStream("database.properties");
props.load(in);
in.close();
String url = props.getProperty("db.url");
String user = props.getProperty("db.user");
String password = props.getProperty("db.passwd");
con = DriverManager.getConnection(url, user, password);
} catch (Exception ex) {
Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
}
Run Code Online (Sandbox Code Playgroud) 我key = value在.properties文件中有一个属性:
give names: (1) code = xxx
Run Code Online (Sandbox Code Playgroud)
...但是当我试图获得该密钥时,它引发了一个错误:
在代码下找不到任何消息给出名称:(1)code = xxx
我试图逃避空白,\但它没有用.
我需要逃避:,(和)人物呢?
我正在使用Spring Boot 1.2.1.RELEASE,JUnit4和Gradle 2.1来获取RESTful服务,并且我正在尝试定义一个属性文件,该文件包含和/或覆盖将仅在JUnit测试中使用的值.当作为JUnit测试调用时,所有测试都会执行而不会出现问题.但是,当在gradle"build"或"test"任务期间运行这些相同的测试时,它们会失败,并出现"引发:java.lang.IllegalStateException:处于失败状态的Tomcat连接器",其根本原因为"java.net" .BindException:地址已在使用中:bind".
有多个测试类需要应用程序上下文才能运行.
为了通过属性文件来促进替代值,我已经包含了TestPropertySource()
是否需要额外的Gradle配置才能使其工作?
ControllerTest类
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes = RESTAdapter.class)
@TestPropertySource(locations="classpath:application-junit.properties")
public class ControllerTest {
@Value("${test.token}")
private String token;
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
public void TestMockMvc(String[] strings) throws Exception{
mockMvc.perform(URL_all_paramters, strings)
.param(Constants.PARAM_TOKEN, token)
.accept(Constants.APPLICATION_JSON_UTF8))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType(Constants.APPLICATION_JSON_UTF8))
.andExpect(content().json(strings[strings.length-1]));
}
}
Run Code Online (Sandbox Code Playgroud)
application.properties(/ src/main/resources)
logging.level.org.springframework.web=TRACE
server.port=4040
shape.file=./shape/tl_2014_34_tabblock10_county_muni.shp
Run Code Online (Sandbox Code Playgroud)
application-junit.properties(/ src/test/resources)
test.token=..dDKidjwel
Run Code Online (Sandbox Code Playgroud)
的build.gradle
buildscript {
ext {
springBootVersion = '1.2.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") …Run Code Online (Sandbox Code Playgroud) junit4 gradle properties-file spring-boot springjunit4classrunner
我有这个 JavaScript 类:
'use strict;'
/* global conf */
var properties = {
'PROPERTIES': {
'CHANNEL': 'sport',
'VIEW_ELEMENTS': {
'LOADER_CLASS': '.loader',
'SPLASH_CLASS': '.splash'
}
}
};
Run Code Online (Sandbox Code Playgroud)
在 JavaScript 中,我可以使用这些属性: properties.PROPERTIES.CHANNEL
是否可以将其转换为 DART?有没有最好的做法来做到这一点?
我环顾四周阅读了很多,但找不到明确的答案来解释“ nuget.props”文件的含义以及它的用途是什么?
任何解释,也许有一些例子会有所帮助?
我想覆盖我在Quarkus应用程序的配置文件中配置的属性。
我怎样才能做到这一点?
我有 Spring 启动应用程序
@SpringBootApplication
@EntityScan(basePackages = {"${scan.packages}"})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
Run Code Online (Sandbox Code Playgroud)
从一个属性中读取多个实体扫描包时,用逗号分隔,如下所示?
scan.packages=com.mycompany.model.package1 , com.mycompany.model.package2
我得到了这个例外:
java.lang.IllegalArgumentException:未知实体:com.mycompany.model.package2.Myclass
我继承的Maven项目有一些JUnits中使用的资源文件。
这些文件在属性文件中被称为绝对路径。
我们假设 Maven 项目位于myproject主项目所在的 目录下pom.xml。
一个config.properties文件有:
keystore.file=C:/Users/tom/myproject/web/src/test/resources/myfile.jks
Run Code Online (Sandbox Code Playgroud)
我想从 Maven 项目的相对路径引用该资源。
所以我一直在尝试类似的事情:
keystore.file=${project.basedir}/web/src/test/resources/myfile.jks
Run Code Online (Sandbox Code Playgroud)
该项目使用 SpringBoot,并且在运行 JUnit 时抱怨:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'project.basedir' in string value "${project.basedir}/web/src/test/resources/myfile.jks"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
Run Code Online (Sandbox Code Playgroud) 当我尝试执行mvn clean install. 作为构建的一部分,我们编译多个组件,最后使用wiremock 执行功能测试。它应该从功能测试配置文件中选择特定配置,并且应该从 application.properties 文件中选择默认属性。但由于某种原因,相同的代码无法找到这些文件中提到的属性。所以,只是想知道是否可以以某种方式获取在wiremock 期间加载的属性文件列表?这将提供一些线索,说明为什么没有选择预期的属性文件?
所有属性文件都位于内部:
src/main/resources
Run Code Online (Sandbox Code Playgroud)
并且,从测试课开始。
@ContextConfiguration(classes = SampleFTConfiguration.class)
public class SampleControllerTest{
//test method
}
@ComponentScan("com.xxx.xxx.xxx.ft")
@PropertySource("classpath:application-test.properties")
public class SampleFTConfiguration{
}
Run Code Online (Sandbox Code Playgroud)
注意:我不希望任何人解决这个问题,我只想知道,我们是否可以获得加载的属性文件的名称?
properties-file ×10
java ×5
spring-boot ×4
spring ×2
dart ×1
database ×1
escaping ×1
gradle ×1
junit ×1
junit4 ×1
maven ×1
nuget ×1
nuget-spec ×1
quarkus ×1
wiremock ×1