简体   繁体   中英

How to set spring bean properties in a .properties resource file

Is this possible to set a spring bean's properties through a dot notation in a .properties file. For example suppose you have defined:

<bean name="myBean" class="MyBean" />

Is it possible to set MyBean.someProperty of myBean to a value defined in a properties file? For example by this properties file:

myBean.someProperty = aValue

I know about PropertyPlaceholderConfigurer, but my requirement is somewhat different from what it supports.

If you have a property in your xml like that, you can assign a value from a properties file like that

<property name="someProperty" value="${myBean.someProperty}" />

which has to deal with the PropertyPlaceholderConfigurer

But I think, you want to override your properties, so have a look at the documentation of the PropertyOverrideConfigurer

<context:property-placeholder location="classpath:/application.properties" />

<property name="myProperty" value="${myProperty.key.of.properties}" />

Spring发行版中还提供了一个PropertyOverrideConfigurer可以完全满足您的需求。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM