春数据源bean定义失败(Spring dataSource bean definition fai

2019-09-17 00:40发布

我试图使用Spring的JdbcTemplate来简化在部署在Tomcat和连接到Postgres的Java Web服务我的DAO。

我下面Spring的机制的文档 ,我试图配置应用程序上下文文件数据源,如下所示:

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

  <context:component-scan base-package="com.manta" />

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

  <context:property-placeholder location="/WEB-INF/db.properties"/>

</beans>

我有以下db.properties中的适当位置的文件:

driverClassName=org.postgresql.Driver 
url=jdbc:postgresql://pgprod.ecnext.com:5432/manta
username=my_user_name
password=my_password

当我尝试部署,我发现下面的堆栈跟踪catalina.out

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: 
Invalid bean definition with name 'dataSource' defined in ServletContext resource [/WEB-INF/context.xml]: 
Could not resolve placeholder 'driverClassName'
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:220)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:84)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:656)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4765)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5260)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:866)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:842)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:958)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1599)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:679)

事情是不是这个问题:

  1. db.properties文件是在正确的位置。
  2. 在凭据db.properties是正确的,并且可以手动读取连接到数据库。
  3. context.xml文件正在由ContextLoaderListener发现,和我能够注入其他依赖。

我非常感激任何建议,这是什么原因。 我使用Spring 3.1.1和Tomcat 7.0.26。

Answer 1:

你可能有多个<context:property-placeholder ... >项目中的每个创建基础对象的一个新实例,是门口的痛苦......

我喜欢用下面的声明加载属性文件:

<bean id="propertyConfigurer"     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:db-config.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>


Answer 2:

从您的位置掉落的前导斜杠(即location="WEB-INF/db.properties" )或者更好的将其更改为类路径:

location="classpath:db.properties"


Answer 3:

use org.springframework.jdbc.datasource.DriverManagerDataSource代替org.apache.commons.dbcp.BasicDataSource



Answer 4:

使用忽视,不可解析=“真”在你的应用程序上下文。 默认值是“费尔斯”你需要设置“真”,所以通过在尚未访问过上下文的关键在于任何人。

<context:property-placeholder ignore-unresolvable="true" location="/WEB-INF/application.properties" />
<context:property-placeholder ignore-unresolvable="true" location="/WEB-INF/dbcp.properties"/>


Answer 5:

请确保您有maven的依赖正常,自旋微观核和Spring上下文的依赖应该是出现在您的项目。

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version></version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version></version>
        </dependency>


文章来源: Spring dataSource bean definition failing