【问题标题】:Properties not being substituted in applicationContext.xmlapplicationContext.xml 中未替换的属性
【发布时间】:2012-03-16 20:29:27
【问题描述】:

我正面临一个非常奇怪的问题。我正在尝试通过 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 容器能够加载属性文件(使用无效属性文件检查并引发 FNF 异常)但不能用它们的值替换属性占位符。

我正在使用一个 tomcat 7 WS,CATALINA.BASE 指向我的运行时。以前有人遇到过这个问题吗?有什么解决办法吗?

【问题讨论】:

    标签: spring properties applicationcontext


    【解决方案1】:

    试试下面的配置。

    <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 名称通常是基于对象类型自动生成的名称。

    【讨论】:

      【解决方案2】:

      从值中去掉前导斜线:

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

      【讨论】:

      • 是的,试过了。当我尝试在我的代码中访问 jdbctemplate 时得到一个 java.lang.ClassNotFoundException: ${db.driverClassName}。
      • 是的,但这意味着读取 WORKED 属性。该异常表明您的 CLASSPATH 中没有 JDBC 驱动程序 JAR。
      • 嗯,我试过用驱动类名直接替换驱动类名的属性占位符。它按预期工作。因此,驱动程序正在加载到类路径中。
      • 驱动类中的键名是否正确输入到您的属性占位符中?
      • 是的,如果您指的是属性文件中的键。我最初从属性文件中复制粘贴了密钥,当这不起作用时,复制了密钥的值。那里没有问题。
      猜你喜欢
      • 1970-01-01
      • 2010-10-02
      • 2017-04-17
      • 1970-01-01
      • 2015-04-13
      • 2018-05-06
      • 2012-09-11
      • 1970-01-01
      • 2015-04-04
      相关资源
      最近更新 更多