2012-02-19 18 views
7

SpringでテストプロジェクトでJNDI DB接続を定義しようとしています。私はSpring Rooでプロジェクトをブートストラップしたので、Mavenizedです。ここでは、参照用Rooのスクリプト(ルー1.2.1)src/main/resources/META-INF/spring/applicationContext.xmlNameNotFoundException:名前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 

は、私は(ルーによって作成された)次のようましです:

<?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"/> 
    ... 

私は以下の内容を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&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ディレクトリに存在する必要があります。クラスディレクトリまたはjarファイル内にあってはいけません。

ソースフォルダツリー内のwebappのルートを含むディレクトリに、context.xmlとともにMETA-INFディレクトリを配置します。

+1

ああ!間違った 'META-INF'フォルダ!だから、 'src/main/webapp/META-INF'に' context.xml'を入れて、WARファイルの '/ META-INF'フォルダにパッケージ化されています。 – stivlo

+0

これは戦争が桟橋に配備されなければならない場合に有効ですか? – Sikorski

+1

@Sikorski:いいえ。 context.xmlはTomcat独自のファイルです。 –

関連する問題