2012-02-19 131 views
7

我想在Spring的测试项目中定义JNDI DB连接。我用Spring Roo引导了这个项目,因此是Maven化的。这里是Roo的脚本供参考(Roo的1.2.1)NameNotFoundException:在此上下文中未绑定名称jdbc

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 

src/main/resources/META-INF/spring/applicationContext.xml我以下(由小豆创建):我创建src/main/resources/META-INF/context.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"/> 
    ... 

<?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&amp;characterEncoding=UTF-8" 
     maxActive = "100" 
     maxIdle = "4" 
     maxWait = "20000" 
     removeAbandoned = "true" 
     removeAbandonedTimeout="600" 
     logAbandoned="true"/> 
</Context> 

然而,当我尝试给Tomcat 7.0运行应用程序,我得到以下错误:

ERROR org.springframework.web.context.ContextLoader - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

如何正确定义数据源?

回答

11

context.xml文件必须位于war文件的META-INF目录中。它不能在classes目录或jar文件中。

将带有context.xml的META-INF目录放入源文件夹树中包含webapp根目录的目录中。

+1

啊啊!错误的'META-INF'文件夹!所以我把'context.xml'放在'src/main/webapp/META-INF'中,并将它打包到war文件的'/ META-INF'文件夹中,一切正常,非常感谢! – stivlo 2012-02-19 16:44:59

+0

如果必须在码头部署战争,这是否行得通? – Sikorski 2012-07-31 13:38:43

+1

@Sikorski:没有。 context.xml是一个Tomcat专有文件。 – 2012-07-31 13:53:51

相关问题