【问题标题】:Use Java util properties instead of propertyplaceholder configurator in springbeans.xml在 springbeans.xml 中使用 Java util 属性而不是 propertyplaceholder 配置器
【发布时间】:2016-07-24 22:43:36
【问题描述】:

我需要使用 Java.util.properties 而不是 Spring Eg 中的 PropertyPlaceHolderConfigurator 来动态放置 springbeans.xml 中的属性值。

<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>

我可以使用 java.util.properties 来模拟相同的结果,即

<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>

【问题讨论】:

  • 你为什么需要这个,你能解释一下你为什么不想要或者可以使用PropertyPlaceHolderConfigurer吗?

标签: java spring properties spring-properties


【解决方案1】:

因为你已经在使用 Spring,你可以这样做

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

然后注入它,它的类型是java.util.properties

@Resource
private Properties properties;

【讨论】:

  • PropertiesFactoryBean 已经创建了一个java.util.Properties 对象,这就是FactoryBean 的重点。
  • 我知道......“转换”可能是错误的术语
猜你喜欢
  • 1970-01-01
  • 2013-11-28
  • 2015-01-03
  • 2021-09-30
  • 2015-05-28
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 1970-01-01
相关资源
最近更新 更多