【问题标题】:Spring+Maven Property File not loadingSpring + Maven 属性文件未加载
【发布时间】:2014-04-08 22:00:51
【问题描述】:

我正在尝试访问包含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]

但是有些方法,值没有被加载。

Spring-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.xmlimportsgeneric.xml。

  • generic.xml imports Spring-DataSource.xml。

【问题讨论】:

  • ${appenv.deployment} 是什么?这一套怎么样?还要确保两者都在同一个应用程序上下文中。
  • @M.Deinum 更新了问题以澄清您的查询。
  • development.propertiessrc/main/resources 中吗?
  • @M.Deinum 不,它在src/resources。它基于 maven,甚至 applicationContext.xmllog4j.xml 都在同一位置。
  • 如果它是基于 maven 的,它必须在 src/main/resources 而不是 src/resources。第一个是 maven 默认(在src/main/java 旁边)。添加您的目录结构并扩展您如何加载applicationContext.xml 文件。

标签: java spring maven properties


【解决方案1】:

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

&lt;context:property-placeholder location="Path for .properties file"/&gt;

在 bean 之前添加这一行

【讨论】:

  • 我认为我所做的是按照您的建议做的另一种方式。请参阅 dbPropertyReader 的 bean 定义。
猜你喜欢
  • 2020-02-13
  • 2013-01-15
  • 2014-06-22
  • 2012-08-03
  • 2011-06-14
  • 1970-01-01
  • 2019-10-18
  • 2019-03-04
  • 2015-06-18
相关资源
最近更新 更多