2012-02-19 1 views
7

Spring과 함께 테스트 프로젝트에서 JNDI DB 연결을 정의하려고합니다. Spring Roo로 프로젝트를 부트 스트랩 했으므로 Mavenized입니다.NameNotFoundException : 이름 jdbc가이 컨텍스트에 바인드되지 않았습니다.

<?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을 만든 : 여기가 (루에 의해 생성) 다음 한 src/main/resources/META-INF/spring/applicationContext.xml에서 참조 할 수 있도록 루 스크립트 (루 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 

입니다 : 그러나

<?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> 

, 나는 톰캣 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 파일에 있어서는 안됩니다.

소스 폴더 트리의 webapp 루트가 포함 된 디렉토리에 context.xml과 함께 META-INF 디렉토리를 두십시오.

+1

아 아! 잘못된'META-INF' 폴더! 그래서'src/main/webapp/META-INF'에'context.xml'을 넣었고, WAR 파일의'/ META-INF' 폴더에 패키지되어 모든 것이 작동합니다. 정말 고마워요! – stivlo

+0

전쟁이 부두에 배치되어야한다면이 방법이 효과가 있습니까? – Sikorski

+1

@Sikorski : no. context.xml은 Tomcat 독점 파일입니다. –