属性未在applicationContext.xml中替换

时间:2012-02-27 10:55:00

标签: spring properties applicationcontext

我面临一个非常奇怪的问题。我想通过PropertyPlaceholderConfigurer类来配置我的spring applicationcontext.xml文件中声明的数据源的属性。应用程序上下文文件如下所示: -

<?xml version="1.0" encoding="UTF-8"?>

<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" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.4.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--  Spring configurations used by LCM Impl classes -->
<bean id="lcmPropertyConfigurer"
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/activemq.properties</value>
            <value>classpath:/jdbc.properties</value>
        </list>
    </property>
</bean>


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

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>
</beans>

我尝试过使用context:property-placeholder标签,但似乎没有任何效果。上面提到的属性文件也在WEB-INF / classes目录下成功部署。

由于某种原因,spring容器能够加载属性文件(使用invalide属性文件检查并抛出FNF异常)但不能用属性占位符替换它们的值。

我正在使用tomcat 7 WS和CATALINA.BASE指向我的运行时。以前有人遇到过这个问题吗?任何解决方案?

2 个答案:

答案 0 :(得分:1)

从值中取出前导斜杠:

        <value>classpath:activemq.properties</value>
        <value>classpath:jdbc.properties</value>

答案 1 :(得分:0)

尝试以下配置。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:activemq.properties</value>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

问题可能是你甚至命名了PropertyPlaceholderConfigurer bean。大多数Spring内部bean都没有id,因为框架被配置为查找某些bean名称,并且默认的bean名称通常是基于对象类型的自动生成的名称。