在 springbeans.xml 中使用 Java util 属性而不是 propertyplaceholder 配置器 [英] Use Java util properties instead of propertyplaceholder configurator in springbeans.xml

查看:57
本文介绍了在 springbeans.xml 中使用 Java util 属性而不是 propertyplaceholder 配置器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Java.util.properties 而不是 Spring Eg 中的 PropertyPlaceHolderConfigurator 在 springbeans.xml 中动态占位属性值.

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>

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

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>

推荐答案

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

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

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

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

@Resource
private Properties properties;

这篇关于在 springbeans.xml 中使用 Java util 属性而不是 propertyplaceholder 配置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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