我做这个工作..春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);
有谁知道为什么我的属性没有加载到环境中吗?谢谢。
我通过MutuablePropertySources API去了,但真的很混乱,我觉得春天不是招不会使物业处理变得简单和美观。但是你说我不想这么做,为什么不呢?如果它能完成这项工作,我可以尝试。我认为我的财产将在环境中的原因是在阅读本文后.. [http://www.javaworld.com/community/node/8309](http://www.javaworld.com/community/node/8309) – endless
@ user1364959请检查http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/ - PS我说“不太可能”,因为仅仅用'@PropertySource(“classpath:SpringConfig.properties”)来装饰你的Conifuguration类应该足以将属性源添加到环境中。 –
至于PropertySourcesPlaceholderConfigurer - 当你在容器中注册一个像'@Bean()public static PropertySourcesPlaceholderConfigurer(){..}'时,它会自动从应用程序上下文(因为它实现了EnvironmentAware)获取属性源。所以它将能够替换bean定义中的占位符。 –