Spring NameNotFoundException:名称jdbc在此上下文中未绑定
我正试图用Spring在一个测试项目中定义一个JNDIDB连接。我已经用SpringRoo引导了这个项目,因此是Mavenized。以下是供参考的Roo脚本(Roo1.2.1) 在Spring NameNotFoundException:名称jdbc在此上下文中未绑定,spring,jndi,tomcat7,Spring,Jndi,Tomcat7,我正试图用Spring在一个测试项目中定义一个JNDIDB连接。我已经用SpringRoo引导了这个项目,因此是Mavenized。以下是供参考的Roo脚本(Roo1.2.1) 在src/main/resources/META-INF/spring/applicationContext.xml中,我有以下内容(由Roo创建): 但是,当我尝试在Tomcat 7.0中运行应用程序时,出现以下错误: 错误org.springframework.web.context.ContextLoader-上下
src/main/resources/META-INF/spring/applicationContext.xml中,我有以下内容(由Roo创建):
但是,当我尝试在Tomcat 7.0中运行应用程序时,出现以下错误:
错误org.springframework.web.context.ContextLoader-上下文初始化失败
org.springframework.beans.factory.BeanCreationException:创建名为“dataSource”的bean时出错:调用init方法失败;嵌套异常为javax.naming.NameNotFoundException:名称jdbc在此上下文中未绑定
如何正确定义数据源?context.xml文件必须位于war文件的META-INF目录中。它不能在classes目录或jar文件中
将带有context.xml的META-INF目录放在源文件夹树中包含webapp根目录的目录中。ah!错误的META-INF
文件夹!所以我把context.xml
放在src/main/webapp/META-INF
中,它被打包在war文件的/META-INF
文件夹中,一切正常,非常感谢@西科斯基:不是。context.xml是Tomcat的专有文件。
project --topLevelPackage org.obliquid.cpool
jpa setup --database MYSQL --provider HIBERNATE --jndiDataSource /jdbc/cpool
web mvc setup
entity jpa --class org.obliquid.cpool.entity.Person
field string --fieldName name
web mvc scaffold --class ~.entity.Person
web mvc all --package ~.web
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
...
<jee:jndi-lookup id="dataSource" jndi-name="/jdbc/cpool" resource-ref="true"/>
...
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/myapp" docBase="cpool" reloadable="true" debug="1">
<Resource name = "jdbc/cpool"
auth = "Container"
type = "javax.sql.DataSource"
username = "dbusername"
password = "dbpassword"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/dbname?DateTimeBehavior=convertToNull&characterEncoding=UTF-8"
maxActive = "100"
maxIdle = "4"
maxWait = "20000"
removeAbandoned = "true"
removeAbandonedTimeout="600"
logAbandoned="true"/>
</Context>