【问题标题】:Spring PropertyPlaceholderConfigurer loading properties from both file and databaseSpring PropertyPlaceholderConfigurer 从文件和数据库加载属性
【发布时间】:2018-07-21 15:27:42
【问题描述】:

我想知道是否可以从文件和数据库中加载属性,并在数据库中加载属性时使用文件中的属性。所以我想要实现的是将应用程序属性保存在数据库中,并将数据库信息保存在属性文件中。下面是我的配置,但是没有用,在读取数据库属性之前无法加载 jdbc.properties 中的值。谁可以帮我这个事?谢谢!

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="locations">  
        <list>  
            <value>classpath*:jdbc.properties</value> <!--loading properties from file first-->
        </list>  
    </property>  
    <property name="properties"  ref="dataBaseProperties"/> <!--loading properties from database using the properties from file-->
</bean>

<bean id="dataBaseProperties" class="common.spring.DatabaseProperties" >  
    <constructor-arg type="javax.sql.DataSource" ref="confDataSource"/>  
    <constructor-arg value="select key_s,value_s from app_conf where status>0"/>  
</bean>

<bean id="confDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
    <property name="driverClassName" value="${jdbc.driver}"/> <!--from properties file - jdbc.properties-->
    <property name="url" value="${jdbc.url}"/> <!--from properties file - jdbc.properties-->
    <property name="username" value="${jdbc.username}"/> <!--from properties file - jdbc.properties-->
    <property name="password" value="${jdbc.password}"/> <!--from properties file - jdbc.properties-->
</bean>

【问题讨论】:

    标签: spring properties placeholder


    【解决方案1】:
    > <beans:bean id="appProperties"
    > class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    >       <beans:property name="location"
    > value="classpath:META-INF/application.properties" />  </beans:bean>
    

    您应该提及它获取 *.properties 文件的位置

    【讨论】:

    • 我得到了同样的结果。
    • 移除那个 ref bean
    • @Jonny 顺便把 jdbc.properties 文件放哪里了
    • 位置是'WEB-INF\classes\jdbc.properties'
    猜你喜欢
    • 2014-12-10
    • 2013-08-21
    • 2014-07-29
    • 2014-10-18
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 2016-06-01
    • 2012-06-06
    相关资源
    最近更新 更多