我正在使用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},其中spring加载形成envrionment特定的属性文件,我的问题是这样可行,因为只要应用程序被加载到容器中,它就会尝试加载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
并且有效。
答案 0 :(得分:0)
编辑:
在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"/>