我这样做..
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);
有谁知道为什么我的属性没有加载到Environment中?谢谢.
我在使用@Value注释将Tomcat中的JNDI值注入spring java配置时遇到了麻烦,而我可以通过Environment类检索值.我们使用的是Java 1.7.0_17,Spring 4.0.3和Tomcat 7.0.52.
我在context.xml以下变量中定义了:
<Environment name="USER_NAME" value="initech" type="java.lang.String" />
<Environment name="USER_PASSWORD" value="1n3+3ch!" type="java.lang.String" />
在我的Java配置文件中我有下面的代码工作:
@Bean
public Foo foo( Environment env ){
    return new Foo( env.getProperty("USER_NAME"), env.getProperty("USER_PASSWORD") );
}
当我查看服务器日志时,我看到:
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [servletConfigInitParams]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [servletContextInitParams]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [jndiProperties]
12:50:45.214 …