2

我有一个可执行 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 来声明数据源/事务管理器等。

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

弹簧配置文件在这里

<?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
4

3 回答 3

4

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

 <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

于 2013-09-20T15:53:21.490 回答
0

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

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

于 2013-09-20T16:35:49.813 回答
0

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

于 2014-08-28T04:08:49.727 回答