Apache 公共配置 spring 集成
Apache commons configuration spring intergration
我是 apache commons 配置的新手,想将其包含在我的项目中。我从 apache commons 页面
获得了以下代码
@Configuration
static class Config {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(ConfigurableEnvironment env)
throws ConfigurationException {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
MutablePropertySources sources = new MutablePropertySources();
sources.addLast(new ConfigurationPropertySource("xml configuration", new Configurations().xml("aXmlConfigFile.xml")));
configurer.setPropertySources(sources);
configurer.setEnvironment(env);
return configurer;
}
}
问题是如果我这样做 envirnment.getProperty("some_key")
这是空的。它是否仅作为@Value 使用而不是在使用 Environment
.
时起作用
还有我如何在运行时覆盖属性并将其保存到它们所属的文件中...
以下对我有用:
@Configuration
public class PropertiesConfig {
@Autowired
private ConfigurableEnvironment env;
@Autowired
public void propertySourcesPlaceholderConfigurer(ConfigurableEnvironment env) throws ConfigurationException {
ConfigurationPropertySource configurationPropertySource = new ConfigurationPropertySource("xml configuration",
new Configurations().xml("config.xml"));
env.getPropertySources().addLast(configurationPropertySource);
}
}
这是我的测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test1 {
@Autowired
private Environment env;
@Test
public void test1() throws Exception {
assertEquals("qa", env.getProperty("processing[@stage]"));
}
}
这是我的xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
<processing stage="qa">
<paths>
<path>/data/path1</path>
<path>/data/otherpath</path>
<path>/var/log</path>
</paths>
</processing>
</configuration>
我是 apache commons 配置的新手,想将其包含在我的项目中。我从 apache commons 页面
获得了以下代码@Configuration
static class Config {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(ConfigurableEnvironment env)
throws ConfigurationException {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
MutablePropertySources sources = new MutablePropertySources();
sources.addLast(new ConfigurationPropertySource("xml configuration", new Configurations().xml("aXmlConfigFile.xml")));
configurer.setPropertySources(sources);
configurer.setEnvironment(env);
return configurer;
}
}
问题是如果我这样做 envirnment.getProperty("some_key")
这是空的。它是否仅作为@Value 使用而不是在使用 Environment
.
还有我如何在运行时覆盖属性并将其保存到它们所属的文件中...
以下对我有用:
@Configuration
public class PropertiesConfig {
@Autowired
private ConfigurableEnvironment env;
@Autowired
public void propertySourcesPlaceholderConfigurer(ConfigurableEnvironment env) throws ConfigurationException {
ConfigurationPropertySource configurationPropertySource = new ConfigurationPropertySource("xml configuration",
new Configurations().xml("config.xml"));
env.getPropertySources().addLast(configurationPropertySource);
}
}
这是我的测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test1 {
@Autowired
private Environment env;
@Test
public void test1() throws Exception {
assertEquals("qa", env.getProperty("processing[@stage]"));
}
}
这是我的xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
<processing stage="qa">
<paths>
<path>/data/path1</path>
<path>/data/otherpath</path>
<path>/var/log</path>
</paths>
</processing>
</configuration>