Tôi đang cố gắng xác định kết nối JNDI DB trong một dự án thử nghiệm với Spring. Tôi đã khởi động dự án với Spring Roo và do đó được Mavenized. Đây là kịch bản Roo để tham khảo (Roo 1.2.1)NameNotFoundException: Tên jdbc không bị ràng buộc trong Bối cảnh này
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
Trong src/main/resources/META-INF/spring/applicationContext.xml
Tôi đã sau (tạo ra bởi Roo):
<?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"/>
...
Tôi đã tạo src/main/resources/META-INF/context.xml
với nội dung sau:
<?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>
Tuy nhiên, khi tôi cố gắng để chạy các ứng dụng trong Tomcat 7.0, tôi nhận được lỗi sau:
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
Tôi nên làm gì để xác định đúng nguồn dữ liệu?
ah ah! Sai thư mục 'META-INF'! Vì vậy, tôi đã đặt 'context.xml' trong' src/main/webapp/META-INF' và nó được đóng gói trong thư mục '/ META-INF' của tệp chiến tranh và mọi thứ hoạt động, cảm ơn bạn rất nhiều! – stivlo
có hoạt động không nếu chiến tranh phải được triển khai trên cầu cảng? – Sikorski
@Sikorski: no. context.xml là một tệp độc quyền Tomcat. –