gpt4 book ai didi

java - Spring 数据源 bean 定义失败

转载 作者:搜寻专家 更新时间:2023-11-01 01:23:00 25 4
gpt4 key购买 nike

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

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

<?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 找到,我可以注入(inject)其他依赖项。

对于可能导致此问题的任何建议,我将不胜感激。我使用的是 Spring 3.1.1 和 Tomcat 7.0.26。

最佳答案

您可能有多个 <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>

关于java - Spring 数据源 bean 定义失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10450240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com