以编程方式访问由属性占位符创建的属性 [英] Programmatic access to properties created by property-placeholder

查看:44
本文介绍了以编程方式访问由属性占位符创建的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆