Spring + Maven属性文件未加载

时间:2014-03-06 10:24:13

标签: java spring maven properties

我正在尝试在Maven + Spring项目中访问包含数据库配置的属性文件。

我收到以下错误:

Cannot load JDBC driver class '${db_driver}'

我的属性文件放在src/resources文件夹中。

以下是加载属性文件的标记:

<bean id="dbPropertyReader" 
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1" />
        <property name="locations">
            <value>classpath:${appenv.deployment}.properties</value>
        </property>
    </bean>

以下标记使用加载的属性:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="url" value="${db_url}" />
        <property name="driverClassName" value="${db_driver}" />
        <property name="username" value="${db_username}" />
        <property name="password" value="${db_password}" />
</bean>

以下是属性文件的内容:

#JDBC Properties
db_driver=com.mysql.jdbc.Driver
db_url=jdbc\:mysql\://hostname\:3306/xxx_dbxxx?useUnicode\=true
db_username=abcdefgh
db_password=ijklmnopq
db_removeabadoned=true
db_initialsize=1
db_maxactive=2

${appenv.deployment}是一个VMArgument集,如下所示:

-Dappenv.deployment=development

我已经检查过,这个值正在填充正确。

我在日志中获得以下行:

Found key 'appenv.deployment' in [systemProperties] with type [String] and value 'development'

然后在此之后我也开始关注:

Loading properties file from class path resource [development.properties]

但有些方面,这些值没有得到加载。

弹簧Datasource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="dbPropertyReader" 
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1" />
        <property name="locations">
            <value>classpath:${appenv.deployment}.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="url" value="${db_url}" />
        <property name="driverClassName" value="${db_driver}" />
        <property name="username" value="${db_username}" />
        <property name="password" value="${db_password}" />
        <property name="initialSize" value="${db_initialsize}" />
        <property name="maxActive" value="${db_maxactive}" />
    </bean>

    <bean id="firstConfigDataFromDB" class="org.apache.commons.configuration.DatabaseConfiguration">
        <constructor-arg type="javax.sql.DataSource" ref="dataSource" />
        <constructor-arg index="1" value="tablename1" />
        <constructor-arg index="2" value="propertyname2" />
        <constructor-arg index="3" value="propertyvalue2" />
    </bean>

    <bean id="firstConfigDataFromDBFactory"
        class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
        <constructor-arg ref="firstConfigDataFromDB" />
    </bean>

    <!-- DB Properties Initialization -->

    <bean id="firstConfigurationPlaceHolder"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="2" />
        <property name="ignoreUnresolvablePlaceholders" value="false"/>
        <property name="properties" ref="firstConfigDataFromDBFactory" />
    </bean>

    <bean id="secondConfigurationFromDB"
        class="org.apache.commons.configuration.DatabaseConfiguration">

        <constructor-arg type="javax.sql.DataSource" ref="dataSource" />
        <constructor-arg index="1" value="tablename2" />
        <constructor-arg index="2" value="propertyname2" />
        <constructor-arg index="3" value="propertyvalue2" />
    </bean>

    <bean id="secondConfigurationFromDBFactory"
        class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
        <constructor-arg ref="secondConfigurationFromDB" />
    </bean>

    <!-- 
    Error Map Initialization 
    Subtype of org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
    -->

    <bean id="secondConfigurationPlaceHolder"
        class="com.application.SecondConfigurationPlaceHolder">
        <property name="order" value="3" />
        <property name="ignoreUnresolvablePlaceholders" value="false"/>
        <property name="properties" ref="secondConfigurationFromDBFactory" />
    </bean>

</beans>

Generic.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>
    <!-- Enable annotation scanning -->
    <context:annotation-config/>
    <!-- Initialise connection to Database -->
    <import resource="Spring-Datasource.xml"/>
    <!-- Initialize mail connection -->
    <import resource="Spring-Mail.xml"/>
    <!-- Inject database connection to DAO -->
    <import resource="Spring-DAO.xml"/>

    <!-- Other Beans Below -->

</beans>

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
>
    <import resource="generic.xml" />

    <bean id="applicationBean" class="com.application.ApplicationBean" scope="singleton" >
        <property .. />
        <property .. />
    </bean>

</beans>

我在代码中加载applicationContext.xml以及以下语句:

`appContext = new ClassPathXmlApplicationContext("applicationContext.xml");`
  • applicationContext.xml imports generic.xml。

  • generic.xml imports Spring-DataSource.xml。

1 个答案:

答案 0 :(得分:0)

您是否已将此添加到您的应用程序上下文文件

<context:property-placeholder location="Path for .properties file"/>

在bean

之前添加此行