【问题标题】:Spring PropertyConfigurer - File Found but properties not getting resolvedSpring PropertyConfigurer - 找到文件但未解析属性
【发布时间】:2013-09-26 00:28:42
【问题描述】:

我有一个可执行 jar 包。我想将 db 属性外部化,因此我使用 appContext.xml 中对属性文件的引用。

我已启用 TRACE 级别的日志记录,我发现我没有收到任何针对此行的错误

<context:property-placeholder location="classpath*:db.properties" ignore-unresolvable="true"/>

但是,当我尝试访问在我的 dataSource bean 中的文件中配置的属性时,例如

<bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <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>

我收到以下错误。

Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${db.driverClassName}]

我已执行以下操作以确认文件在类路径中的位置正确

  1. 我故意更改了属性文件名,我可以看到我引发了 FileNotFound 异常。
  2. 我已验证类路径包含对此外部属性文件的引用。

我不确定我可能遗漏了什么。我引用属性文件的方式或引用数据源 bean 中的各个属性的方式有什么问题吗?

我不妨补充一下,我对大多数 bean 使用 Spring 注释,并且仅使用 appcontext 来声明数据源/事务管理器等。

请帮忙,因为我现在花了相当多的时间在这个看似微不足道的部分上,似乎已经走到了死胡同。

spring 配置文件在这里

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- Auto scan the components -->
    <context:component-scan base-package="patternengine" />
    <context:annotation-config />
    <tx:annotation-driven proxy-target-class="false"/>
    <aop:aspectj-autoproxy  proxy-target-class="false"/>

    <context:property-placeholder location="classpath*:db.properties" ignore-unresolvable="true"/>

    <bean id="dataSource"
             class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <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="serviceArgs" class="com.splunk.ServiceArgs">
        <property name="username" value="dashboardad" />
        <property name="password" value="splunkuat" />
        <property name="host" value="silrse.com" />
        <property name="port" value="8089" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>

    <property name="annotatedClasses">
    <list>
        <value>patternengine.entity.SplunkResult</value>
        <value>patternengine.entity.MinuteGroupedSplunkResult</value>
        <value>patternengine.entity.ApplicationComponent</value>
        <value>patternengine.entity.ApplicationHostSourceDetail</value>
        <value>patternengine.entity.ConfiguredApplication</value>
        <value>patternengine.entity.ConfiguredException</value>
        <value>patternengine.entity.IWatchTransaction</value>
        <value>patternengine.entity.MinuteGroupedIWatchTransaction</value>
        <value>patternengine.entity.AppMap</value>
    </list>
    </property>

    </bean>

    <bean id="transactionManager"
     class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

 </beans>

属性文件在这里

db.driverClassName=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@hdvash:1521:ECDDDDA01
db.username=rmt_perf_db
db.password=qwerty123

目录结构如下

启动脚本如下

JAVA_HOME=/home/a_cfp_asp/tomcat/tc7/7.0.39/java
echo 'JAVA_HOME IS ' $JAVA_HOME
PATH=$PATH:$java_home/bin
#BASE_DIR='../'
BASE_DIR='/home/a_cfp_asp/pattern_engine_2b/jobs/dataprocessor/'
CONFIG_ROOT=$BASE_DIR'config'
LIB_ROOT=$BASE_DIR'lib'
jar_path=$LIB_ROOT/antlr-2.7.7.jar:$LIB_ROOT/aopalliance-1.0.jar:$LIB_ROOT/asm-3.1.jar:$LIB_ROOT/aspectjrt-1.6.11.jar:$LIB_ROOT/aspectjweaver-1.6.11.jar:$LIB_ROOT/avalon-framework-4.1.3.jar:$LIB_ROOT/cglib-2.2.jar:$LIB_ROOT/commons-lang3-3.0.jar:$LIB_ROOT/commons-logging-1.1.jar:$LIB_ROOT/dom4j-1.6.1.jar:$LIB_ROOT/hibernate-commons-annotations-4.0.1.Final.jar:$LIB_ROOT/hibernate-core-4.2.3.Final.jar:$LIB_ROOT/hibernate-entitymanager-3.6.8.Final.jar:$LIB_ROOT/hibernate-envers-4.2.3.Final.jar:$LIB_ROOT/hibernate-jpa-2.0-api-1.0.1.Final.jar:$LIB_ROOT/javassist-3.12.0.GA.jar:$LIB_ROOT/javassist-3.15.0-GA.jar:$LIB_ROOT/jboss-logging-3.1.0.GA.jar:$LIB_ROOT/jboss-transaction-api_1.1_spec-1.0.1.Final.jar:$LIB_ROOT/log4j-1.2.14.jar:$LIB_ROOT/logkit-1.0.1.jar:$LIB_ROOT/oracle-11.1.0.6.jar:$LIB_ROOT/pe-common-1.0.jar:$LIB_ROOT/pe-dataprocessor.jar:$LIB_ROOT/slf4j-api-1.6.1.jar:$LIB_ROOT/splunk-1.1.0.jar:$LIB_ROOT/spring-aop-3.1.1.RELEASE.jar:$LIB_ROOT/spring-asm-3.1.1.RELEASE.jar:$LIB_ROOT/spring-beans-3.1.1.RELEASE.jar:$LIB_ROOT/spring-context-3.1.1.RELEASE.jar:$LIB_ROOT/spring-context-support-3.1.1.RELEASE.jar:$LIB_ROOT/spring-core-3.1.1.RELEASE.jar:$LIB_ROOT/spring-expression-3.1.1.RELEASE.jar:$LIB_ROOT/spring-jdbc-3.1.1.RELEASE.jar:$LIB_ROOT/spring-orm-3.1.1.RELEASE.jar:$LIB_ROOT/spring-tx-3.1.1.RELEASE.jar
config_path=$CONFIG_ROOT/db.properties
MYCLASSPATH=.:$config_path:$jar_path

java -cp$MYCLASSPATH com.patternengine.service.impl.DataProcessorService "Dashboard Application" 30

【问题讨论】:

  • xml sn-ps 是否在同一个上下文中?
  • 检查它们是否与上述评论处于相同的上下文中。还发布您的属性文件的内容。
  • 是的,只有一个 spring 配置文件,并且上下文属性占位符和数据源都定义在同一个上下文文件中。我将属性和 spring 文件附加到原始问题
  • 你在文件db.driverClassName 中有这个属性吗?您能否也添加您有问题的属性文件,看起来您指向正确的文件,但属性有问题。
  • 您的配置对我有用。你能展示你的项目结构吗?即使您进行了所有测试,我仍然感觉找不到该文件。

标签: java spring properties-file


【解决方案1】:

我几乎可以肯定您的属性文件不在您想要的类路径中。

 <context:property-placeholder location="classpath*:db.properties" ignore-unresolvable="true"/>

请注意,如果您删除 ignore-unresolvable,Spring 将失败并显示

Invalid bean definition with name 'dataSource' defined in class path resource [<yourfile>.xml]: Could not resolve placeholder 'db.driverClassName' in string value "${db.driverClassName}"

如果无法解决,可能发生了两件事:您的文件不包含该属性或找不到您的文件。根据您提供的详细信息,前者更有可能。

编辑:根据您的配置,db.properties 文件位于相对于类路径根目录的 config 文件夹中。您需要使用config/db.properties 访问它。此外,这可能是相关的:Spring classpath prefix difference

【讨论】:

  • 现在将项目结构和启动脚本添加到主要问题中
  • @Chetya 查看我的编辑。 db.properties 不在类路径的根目录中。
  • Thx v much.I just 将其更改为 并且有效。
【解决方案2】:

您的项目结构是什么样的?我知道您说过您已验证该文件已包含在类路径中,但让我们确定一下。

部署时,是否有 WEB-INF\classes 文件夹?在其中你看到你的 db.properties 文件了吗?如果不是,我想这将表明您的类路径问题。

【讨论】:

  • 现在将项目结构和启动脚本添加到主要问题中
【解决方案3】:

我刚刚遇到了同样的问题,我发现问题是属性名称与 servlet.xml 中的名称不匹配 我在 servlet 上有“database.driver”,在属性文件上有“database.driverClassName”。 也许这也是你的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-28
    • 2013-06-16
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多