Spring PropertySourcesPlaceholderConfigurer 不为 @Value 使用自定义 PropertySource
Spring PropertySourcesPlaceholderConfigurer does not use custom PropertySource for @Value
我一直在尝试在 Spring 应用程序中获取自定义 PropertySource 运行 的非常基本的示例。
这是我的 PropertySource:
public class RemotePropertySource extends PropertySource{
public RemotePropertySource(String name, Object source) {
super(name, source);
}
public RemotePropertySource(String name) {
super(name);
}
public Object getProperty(String s) {
return "foo"+s;
}
}
它通过 ApplicationContextInitializer 添加到 ApplicationContext:
public class RemotePropertyApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
public void initialize(GenericApplicationContext ctx) {
RemotePropertySource remotePropertySource = new RemotePropertySource("remote");
ctx.getEnvironment().getPropertySources().addFirst(remotePropertySource);
System.out.println("Initializer registered PropertySource");
}
}
现在我创建了一个简单的单元测试来查看 PropertySource 是否被正确使用:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RemotePropertySourceTest.ContextConfig.class, initializers = RemotePropertyApplicationContextInitializer.class)
public class RemotePropertySourceTest {
@Autowired
private UnderTest underTest;
@Autowired
Environment env;
@Test
public void testContext() {
assertEquals(env.getProperty("bar"),"foobar");
assertEquals(underTest.getFoo(),"footest");
}
@Component
protected static class UnderTest {
private String foo;
@Autowired
public void setFoo(@Value("test")String value){
foo=value;
}
public String getFoo(){
return foo;
}
}
@Configuration
@ComponentScan(basePackages = {"test.property"})
protected static class ContextConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
return configurer;
}
}
}
通过环境访问值给我正确的结果("foobar"),但使用@Value-Annotation 失败。据我在文档中阅读的内容,我的配置中的 PropertySourcesPlaceholderConfigurer 应该会自动从环境中获取我的 PropertySource,但显然它不会。有什么我想念的吗?
我知道最好通过环境显式访问属性,但现有应用程序经常使用 @Value-Annotations。
非常感谢任何帮助。谢谢!
要使用 @Value
从 属性 源获取值,您必须使用 ${}
语法:
@Autowired
public void setFoo(@Value("${test}")String value){
foo=value;
}
看看官方documentation.
我一直在尝试在 Spring 应用程序中获取自定义 PropertySource 运行 的非常基本的示例。
这是我的 PropertySource:
public class RemotePropertySource extends PropertySource{
public RemotePropertySource(String name, Object source) {
super(name, source);
}
public RemotePropertySource(String name) {
super(name);
}
public Object getProperty(String s) {
return "foo"+s;
}
}
它通过 ApplicationContextInitializer 添加到 ApplicationContext:
public class RemotePropertyApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
public void initialize(GenericApplicationContext ctx) {
RemotePropertySource remotePropertySource = new RemotePropertySource("remote");
ctx.getEnvironment().getPropertySources().addFirst(remotePropertySource);
System.out.println("Initializer registered PropertySource");
}
}
现在我创建了一个简单的单元测试来查看 PropertySource 是否被正确使用:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RemotePropertySourceTest.ContextConfig.class, initializers = RemotePropertyApplicationContextInitializer.class)
public class RemotePropertySourceTest {
@Autowired
private UnderTest underTest;
@Autowired
Environment env;
@Test
public void testContext() {
assertEquals(env.getProperty("bar"),"foobar");
assertEquals(underTest.getFoo(),"footest");
}
@Component
protected static class UnderTest {
private String foo;
@Autowired
public void setFoo(@Value("test")String value){
foo=value;
}
public String getFoo(){
return foo;
}
}
@Configuration
@ComponentScan(basePackages = {"test.property"})
protected static class ContextConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
return configurer;
}
}
}
通过环境访问值给我正确的结果("foobar"),但使用@Value-Annotation 失败。据我在文档中阅读的内容,我的配置中的 PropertySourcesPlaceholderConfigurer 应该会自动从环境中获取我的 PropertySource,但显然它不会。有什么我想念的吗?
我知道最好通过环境显式访问属性,但现有应用程序经常使用 @Value-Annotations。
非常感谢任何帮助。谢谢!
要使用 @Value
从 属性 源获取值,您必须使用 ${}
语法:
@Autowired
public void setFoo(@Value("${test}")String value){
foo=value;
}
看看官方documentation.