sti*_*vlo 7 spring jndi tomcat7
我试图在Spring的测试项目中定义JNDI数据库连接.我用Spring Roo引导了这个项目,因此是Mavenized.这是Roo脚本供参考(Roo 1.2.1)
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
Run Code Online (Sandbox Code Playgroud)
在src/main/resources/META-INF/spring/applicationContext.xml我有以下的(由小豆创建):
<?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"/>
...
Run Code Online (Sandbox Code Playgroud)
我创建src/main/resources/META-INF/context.xml了以下内容:
<?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>
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在Tomcat 7.0中运行该应用程序时,我收到以下错误:
错误org.springframework.web.context.ContextLoader - 上下文初始化失败org.springframework.beans.factory.BeanCreationException:创建名为'dataSource'的bean时出错:init方法的调用失败; 嵌套异常是javax.naming.NameNotFoundException:名称jdbc未绑定在此Context中
我该怎么做才能正确定义数据源?
JB *_*zet 11
context.xml文件必须位于war文件的META-INF目录中.它不能位于classes目录或jar文件中.
将META-INF目录和context.xml放在源文件夹树中包含webapp根目录的目录中.
| 归档时间: |
|
| 查看次数: |
33730 次 |
| 最近记录: |