以编程方式访问由属性占位符创建的属性 [英] Programmatic access to properties created by property-placeholder
问题描述
我正在使用context:property-placeholder
读取属性文件.如何以编程方式访问它们(@Value
不起作用-开发时我不知道产权)?
I'm reading properties file using context:property-placeholder
. How can I access them programatically (@Value
doesn't work - I don't know property titles at the moment of developing)?
主要问题是我无法更改applicationContext.xml
文件,因为它是由父"框架设置的
The main problem is I can't change applicationContext.xml
file because it's setted up by "parent" framework
ps.很奇怪,但是Environment.getProperty
返回null
ps. It's strange but Environment.getProperty
returns null
推荐答案
不,您不能. PropertyPlaceholderConfigurer
是 BeanFactoryPostProcessor
,在bean创建过程中它仅是活动的".遇到${property}
表示法时,它将尝试根据其内部属性来解决该问题,但不会使这些属性可用于容器.
No you can't. PropertyPlaceholderConfigurer
is a BeanFactoryPostProcessor
, it is only "alive" during bean creation. When it encounters a ${property}
notation, it tries to resolve that against its internal properties, but it does not make these properties available to the container.
那是说:类似的问题一次又一次地出现,提出的解决方案通常是子类化PropertyPlaceHolderConfigurer
并使手动可用于上下文的属性.或使用PropertiesFactoryBean
That said: similar questions have appeared again and again, the proposed solution is usually to subclass PropertyPlaceHolderConfigurer
and make the Properties available to the context manually. Or use a PropertiesFactoryBean
这篇关于以编程方式访问由属性占位符创建的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!