Spring无法查找jdbc配置的数据源
我已经在 tomcat 5.5 上部署了我的应用程序,IDE 是带有 myeclipse 插件的 eclipse。当我尝试启动 tomcat 服务器时,出现以下错误:
错误 - ContextLoader.initWebApplicationContext(219) |上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建 ServletContext 资源 [/WEB-INF/xaconfig/daoJPAConfig.xml] 中定义的名为“dataSource”的 bean 时出错:调用 init 方法失败;嵌套异常是 javax.naming.NameNotFoundException:名称 java:comp 未在此上下文中绑定 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) 在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:546) 在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871) 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423) 在org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272) 在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:196) 在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) 在 org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3669) 在 org.apache.catalina.core.StandardContext.start(StandardContext.java:4104) 在 org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012) 在 org.apache.catalina.core.StandardHost.start(StandardHost.java:718) 在 org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012) 在 org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442) 在 org.apache.catalina.core.StandardService.start(StandardService.java:450) 在 org.apache.catalina.core.StandardServer.start(StandardServer.java:683) 在 org.apache.catalina.startup.Catalina.start(Catalina.java:537) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:271) 在 org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:409) 引起原因:javax.naming.NameNotFoundException:名称 java:comp 未在此上下文中绑定 在 org.apache.naming.NamingContext.lookup(NamingContext.java:778) 在 org.apache.naming.NamingContext.lookup(NamingContext.java:161) 在 javax.naming.InitialContext.lookup(InitialContext.java:392) 在 org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154) 在 org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) 在 org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152) 在 org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) 在 org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) 在 org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) 在org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201) 在org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409) ... 27 更多
我已将 Apache Software Foundation\Tomcat 5.5\conf 中的数据源配置为:
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
name="jdbc/Paymentsdb"
type="javax.sql.DataSource"
maxActive="20"
maxIdle="10"
username="root"
maxWait="20000"
validationQuery="SELECT=1"
driverClassName="com.mysql.jdbc.Driver"
password="root"
url="jdbc\:mysql\://devserver\:3306/payhub"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"/>
</GlobalNamingResources>
并且我添加了 Apache Software Foundation\Tomcat 5.5\conf\ context.xml 页面 我添加了我的 webInf\web.xml
<resource-ref>
<description>PaymentsDatabase</description>
<res-ref-name>jdbc/Paymentsdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
我正在使用 spring 框架,hybernate+jpa 。请告诉我,可能是什么问题?
I have deployed my application on tomcat 5.5 and the IDE is eclipse with myeclipse plugin. when I try to start the tomcat server I get the following error:
ERROR - ContextLoader.initWebApplicationContext(219) | Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/xaconfig/daoJPAConfig.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name java:comp is not bound in this Context
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:546)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:196)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3669)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4104)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:683)
at org.apache.catalina.startup.Catalina.start(Catalina.java:537)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:271)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:409)
Caused by: javax.naming.NameNotFoundException: Name java:comp is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:778)
at org.apache.naming.NamingContext.lookup(NamingContext.java:161)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201)
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
... 27 more
I have configured the datasource in Apache Software Foundation\Tomcat 5.5\conf as:
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
name="jdbc/Paymentsdb"
type="javax.sql.DataSource"
maxActive="20"
maxIdle="10"
username="root"
maxWait="20000"
validationQuery="SELECT=1"
driverClassName="com.mysql.jdbc.Driver"
password="root"
url="jdbc\:mysql\://devserver\:3306/payhub"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml"/>
</GlobalNamingResources>
and i added Apache Software Foundation\Tomcat 5.5\conf\ context.xml page
and i added my webInf\web.xml
<resource-ref>
<description>PaymentsDatabase</description>
<res-ref-name>jdbc/Paymentsdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
I am using spring framework,hybernate+jpa . Please let me know, what the problem could be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不建议将其放入 Tomcat 的全局配置中。无法保证您能够访问它。最好将其放在 META-INF/context.xml 中。
您没有显示 JNDI 数据源的 Spring 配置。请包括这一点。
这不应该是
java:comp/env
吗?I would not recommend putting that in the global config for Tomcat. There's no guarantee that you'll have access to it. Better to put it in META-INF/context.xml.
You didn't show your Spring config for the JNDI data source. Please include that.
Shouldn't this be
java:comp/env
?