Spring dataSource bean定义失败

时间:2012-05-04 13:53:07

标签: java spring jdbc datasource

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

我正在关注Spring's documention,我正在尝试在应用程序上下文文件中配置DataSource,如下所示:

<?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. ContextLoaderListener正在找到context.xml文件,我可以注入其他依赖项。
  4. 我非常感谢有关可能导致这种情况的任何建议。我使用的是Spring 3.1.1和Tomcat 7.0.26。

5 个答案:

答案 0 :(得分:6)

您的项目中可能有多个<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> 创建一个基础对象的新实例,并且是通往痛苦的通道......

我更喜欢使用以下声明来加载属性文件:

{{1}}

答案 1 :(得分:4)

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

location="classpath:db.properties"

答案 2 :(得分:1)

use org.springframework.jdbc.datasource.DriverManagerDataSource 而不是org.apache.commons.dbcp.BasicDataSource

答案 3 :(得分:0)

在您的应用程序上下文中使用 ignore-unresolvable =&#34; true&#34; 。默认值是&#39; fales&#39;你需要设置它真实&#39;所以将密钥传递给尚未访问过的上下文中的任何其他密钥。

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

答案 4 :(得分:0)

确保您正确拥有maven依赖关系,Sping-core和Spring-context依赖关系应该存在于您的项目中。

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version></version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version></version>
        </dependency>
相关问题