2013-01-05 29 views
16

我做这个工作..春3.1环境不符合用户属性文件

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的属性没有得到回升,如果我这样做...

@Autowired 
private Environment env 
..... 
env.getProperty("my.property") 

但我得到的财产,如果我用

@Value("${my.property}") 
private String myProperty; 

我甚至尝试添加几个更多这样的线条,但没用。

ConfigurableEnvironment env = new StandardEnvironment(); 
propertyHolder.setEnvironment(env); 

有谁知道为什么我的属性没有加载到环境中吗?谢谢。

回答

13

PropertySourcesPlaceholderConfigurer直接读取属性文件(因为它是在Spring 3.0中由PropertyPlaceholderConfigurer完成的),它只是一个不改变Spring上下文中属性使用方式的后处理器 - 在这种情况下,属性只能作为bean定义占位符。

这是PropertySourcesPlaceholderConfigurer谁使用环境,反之亦然。

属性源框架在应用程序上下文级别上工作,而属性占位符配置程序只提供处理bean定义中占位符的功能。要使用属性源抽象,你应该使用@PropertySource注释即装饰的东西你的配置类似 @PropertySource("classpath:SpringConfig.properties")

我相信你可以做同样的事情编程,即可以得到容器的ConfigurableEnvironment之前的背景是刷新,修改其MutablePropertySources(您首先需要通过getPropertySources().addFirst(new ResourcePropertySource(new ClassPathResource( "SpringConfig.properties")));得到AbstractApplicationContextenvironment财产,通过context.getEnvironment()),但它不太可能是你想要做的 - 如果你已经有@Configuration注释类,用@PropertySource("classpath:SpringConfig.properties")装饰它就简单多了。

至于PropertySourcesPlaceholderConfigurer实例 - 它将从其应用程序上下文自动获取属性源(因为它实现了EnvironmentAware),因此您只需注册它的默认实例即可。

对于自定义属性代码实现的例子中看到http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/

+0

我通过MutuablePropertySources API去了,但真的很混乱,我觉得春天不是招不会使物业处理变得简单和美观。但是你说我不想这么做,为什么不呢?如果它能完成这项工作,我可以尝试。我认为我的财产将在环境中的原因是在阅读本文后.. [http://www.javaworld.com/community/node/8309](http://www.javaworld.com/community/node/8309) – endless

+0

@ user1364959请检查http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/ - PS我说“不太可能”,因为仅仅用'@PropertySource(“classpath:SpringConfig.properties”)来装饰你的Conifuguration类应该足以将属性源添加到环境中。 –

+0

至于PropertySourcesPlaceholderConfigurer - 当你在容器中注册一个像'@Bean()public static PropertySourcesPlaceholderConfigurer(){..}'时,它会自动从应用程序上下文(因为它实现了EnvironmentAware)获取属性源。所以它将能够替换bean定义中的占位符。 –

3

添加当地属性PropertySourcesPlaceholderConfigurer(与setProperties()setLocation())并不能让他们在Environment可用。

实际上它工作以相反的方式 - Environment作为属性的主要来源(参见ConfigurableEnvironment),并PropertySourcesPlaceholderConfigurer可使用${...}语法使从所述可用Environment性质。

0

我这样做是每@Boris建议..

PropertySourcesPlaceholderConfigurer propertyHolder = new PropertySourcesPlaceholderConfigurer(); 
    ConfigurableEnvironment env = new StandardEnvironment(); 
    env.getPropertySources().addFirst(
      new ResourcePropertySource(new ClassPathResource(
        "SpringConfig.properties"))); 
    propertyHolder.setEnvironment(env); 
    context.addBeanFactoryPostProcessor(propertyHolder); 
      context.register(SpringConfig.class); 
      context.refresh(); 
在@Configuration类

现在所有属性(包括我自己和系统属性)可以使用@Value解决。

但是@Autowired到@Configuration类中的环境只有系统属性,而不是我如上设置的SpringConfig.properties。但很明显,在上面调用context.refresh()之前,ConfigurableEnvironment也有我的属性。但是一旦调用context.refresh(),我的属性就会从自动装入到@Configuration的环境中移除。

我希望能够使用更好的语法env.getProperty(“my.property”)。有人知道为什么这样吗?

+2

你不应该创建你自己的Environment实例。您应该修改应用程序上下文所具有的一个。附:请编辑该问题,而不是将其作为答案发布。 –

+0

我修改了答案 –

+0

...并且如果您通过@Configuration创建自己的应用程序上下文? –

0

我正在加载2种属性一种是环境属性,另一种是您通常从servletContext.getServletContext()得到的上下文。 我的环境属性定义为:MOD_CONFIG_ROOT单独设置环境,从而分隔位置详细信息包含代码的ear文件。这是配置。 [注:我已经加载的servlet使用${someProperty}利用的特性之前加载属性文件的第一件事]

<bean id="externalProperties" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>file:#{ systemEnvironment['MOD_CONFIG_ROOT'] 
       }#{servletContext.contextPath}/users.properties</value> 
     </list> 
    </property> 
    <property name="searchSystemEnvironment" value="true" /> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_FALLBACK" /> 
</bean>