Spring 3.1 Environment不适用于用户属性文件 [英] Spring 3.1 Environment does not work with user property files
问题描述
我这样做..
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
xmlReader
.loadBeanDefinitions(new ClassPathResource("SpringConfig.xml"));
PropertySourcesPlaceholderConfigurer propertyHolder = new PropertySourcesPlaceholderConfigurer();
propertyHolder.setLocation(new ClassPathResource(
"SpringConfig.properties"));
context.addBeanFactoryPostProcessor(propertyHolder);
......
context.refresh();
现在在我的@Configuration文件中,我的SpringConfig.properties中存在的属性如果没有被提取我这样做...
Now in my @Configuration files, the properties present in my SpringConfig.properties are not getting picked up if I do this...
@Autowired
private Environment env
.....
env.getProperty("my.property")
但如果我使用
@Value("${my.property}")
private String myProperty;
我甚至尝试添加更多这样的行,但没有用。
I even tried adding couple of more lines like this, but of no use.
ConfigurableEnvironment env = new StandardEnvironment();
propertyHolder.setEnvironment(env);
有人知道为什么我的属性没有加载到Environment中吗?谢谢。
Does anybody know why my properties are not loaded into Environment? Thanks.
推荐答案
PropertySourcesPlaceholderConfigurer直接读取属性文件(正如PropertyPlaceholderConfigurer在Spring 3.0中所做的那样),它只是一个后处理器,不会更改Spring上下文中使用属性的方式 - 在这种情况下,属性仅可用作bean定义占位符。
PropertySourcesPlaceholderConfigurer reads property files directly(as it was done by PropertyPlaceholderConfigurer in Spring 3.0 times), it's just a postprocessor which does not change the way properties are used in the Spring context - in this case properties are only available as bean definition placeholders.
使用环境的是PropertySourcesPlaceholderConfigurer,反之亦然。
It's the PropertySourcesPlaceholderConfigurer who uses Environment and not vice versa.
属性源框架适用于应用程序上下文级别,而属性占位符配置程序仅提供处理bean定义中占位符的功能。要使用属性源抽象,您应该使用 @PropertySource
注释即使用
@PropertySource(classpath:SpringConfig.properties)来装饰你的配置类)
Property sources framework works on the application context level, while property placeholder configurers only provide the functionality to process placeholders in the bean definitions. To use property source abstraction you should use @PropertySource
annotation i.e. decorate your configuration class with something like
@PropertySource("classpath:SpringConfig.properties")
我相信您可以通过编程方式执行相同的操作,即您可以在刷新上下文之前获取容器的ConfigurableEnvironment,修改其MutablePropertySources(首先需要通过 context.getEnvironment()获取
)通过 AbstractApplicationContext
environment
属性 getPropertySources()。addFirst(new ResourcePropertySource(new ClassPathResource(
但是你不太可能想做 - 如果你已经有一个
SpringConfig.properties))); @Configuration
带注释的类,用 @Property进行装饰Source(classpath:SpringConfig.properties)
要简单得多。
I believe that you can do the same thing programmatically, i.e. you can get the container's ConfigurableEnvironment before the context was refreshed, modify its MutablePropertySources(you need first to get AbstractApplicationContext
environment
property via context.getEnvironment()
) via getPropertySources().addFirst(new ResourcePropertySource(new ClassPathResource(
"SpringConfig.properties")));
but it's unlikely what you want to do - if you already have a @Configuration
annotated class, decorating it with @PropertySource("classpath:SpringConfig.properties")
is much simpler.
至于 PropertySourcesPlaceholderConfigurer
instance - 它将从其应用程序上下文中自动获取属性源(因为它实现了EnvironmentAware),因此您只需要注册它的默认实例。
As for the PropertySourcesPlaceholderConfigurer
instance - it will fetch property sources automatically(as it implements EnvironmentAware) from its application context so you need just to register a default instance of it.
自定义属性源实现的示例请参见 http ://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/
For the examples of custom property source implementation see http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/
这篇关于Spring 3.1 Environment不适用于用户属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!