gpt4 book ai didi

java - 无法创建 JDBC 数据源实例 - Spring 4.x 和 Tomcat 8

转载 作者:行者123 更新时间:2023-11-28 23:28:14 25 4
gpt4 key购买 nike

我正在使用 Spring 4.x 库、Java 8 和 Tomcat 8

下面是我的数据源配置。

my_app/META_INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/my_app" swallowOutput="true">
<Resource name="jdbc/paymentDB" auth="Container" type="org.apache.tomcat.jdbc.pool.DataSource"
driverClassName="${driverClassName}"
url="${url}"
username="${username}"
password="${password}"
initialSize="10"
maxIdle="30"
validationQuery="select 1 from dual"
maxActive="30"
maxWait="5000"
testWhileIdle="true"
validationQueryTimeout="1"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.QueryTimeoutInterceptor(queryTimeout=2);org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1000,maxQueries=200)"
testOnBorrow="true"
timeBetweenEvictionRunsMillis="5000"/>
</Context>

我的 web.xml 条目

<resource-ref>
<res-ref-name>
jdbc/paymentDB
</res-ref-name>
<res-type>
org.apache.tomcat.jdbc.pool.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>

在我的 applicationContext.xml 中输入

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/paymentDB"></property>
</bean>

我在 tomcat lib/文件夹中添加了必要的驱动程序 jar。当我开始我的应用程序时,我遇到了异常。

Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5162)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

有没有人知道我在这里缺少什么。

更新:我在 eclipse maven 项目的 src/main/webapp 文件夹中创建了 context.xml 文件

更新

我在我的 context.xml 中使用变量,即 ${driverClassName},它从 envrionment 特定的属性文件中 Spring 加载,我的问题是这是否有效,因为一旦应用程序加载到容器中,它就会尝试加载 web.xml它引用了我的 spring applicationContext.xml 以及我在 context.xml 中定义的数据源

用实际值替换变量后更新

看起来它正在做某事但不是我得到不同的异常

Invocation of init method failed; nested exception is javax.naming.NamingException: Could not create resource factory instance [Root exception is java.lang.ClassCastException: org.apache.tomcat.jdbc.pool.DataSource cannot be cast to javax.naming.spi.ObjectFactory]

最终更新

我将出厂值从 org.apache.tomcat.jdbc.pool.DataSource 更改为 org.apache.tomcat.jdbc.pool.DataSourceFactory

它奏效了。

最佳答案

编辑:

在你的context.xml中,

<Resource name="jdbc/paymentDB" auth="Container" type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSource"
driverClassName="${driverClassName}"
url="${url}"
username="${username}"
password="${password}"
initialSize="10"
maxIdle="30"
validationQuery="select 1 from dual"
maxActive="30"
maxWait="5000"
testWhileIdle="true"
validationQueryTimeout="1"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.QueryTimeoutInterceptor(queryTimeout=2);org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport(threshold=1000,maxQueries=200)"
testOnBorrow="true"
timeBetweenEvictionRunsMillis="5000"/>

关于java - 无法创建 JDBC 数据源实例 - Spring 4.x 和 Tomcat 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34184984/

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