我有一个可执行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}]
我已完成以下操作以确认文件在类路径中正确定位
我不确定我可能会遗漏什么。我引用属性文件的方式有什么不对,或者我如何引用数据源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
答案 0 :(得分: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
答案 1 :(得分:0)
您的项目结构是什么样的?我知道你说你确认该文件已包含在类路径中,但让我们确定。
部署时,您是否有WEB-INF \ classes文件夹?在它中你看到你的db.properties文件?如果不是,我想这将表明你的类路径问题。
答案 2 :(得分:0)
我遇到了同样的问题,我发现问题是属性名称与servlet.xml中的属性名称不匹配 我有&#34; database.driver&#34;在servlet和&#34; database.driverClassName&#34;在属性文件上。 也许这也是你的问题。