Use Java util properties instead of propertyplaceholder configurator in springbeans.xml

依然范特西╮ 提交于 2019-12-11 12:52:27

问题


I need to dynamically placehold the property values in springbeans.xml using Java.util.properties instead of PropertyPlaceHolderConfigurator in Spring Eg.

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location" value="file:test.properties" />
 </bean>   
<bean id="dbconnectionFactory" class="com.test.Test">           
      <property name="username" value="${username}" />
      <property name="password" value="${password}" />
 </bean>

Can I use java.util.properties to mimic the same result i.e.

<bean id="javaproperty" class="java.util.Properties">
                                   <property name="location" value="file:test.properties" />
            </bean> 

            <bean id="dbconnectionFactory"
                        class="con.test.Test">

              <property name="username" value="${username}" />
                        <property name="password" value="${password}" />
            </bean>

回答1:


As you are already using Spring, you could do

<bean id="properties"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath*:application.properties</value>
        </list>
    </property>
</bean

And then just inject it, it is of type java.util.properties

@Resource
private Properties properties;


来源:https://stackoverflow.com/questions/36418786/use-java-util-properties-instead-of-propertyplaceholder-configurator-in-springbe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!