2013-02-12 20 views
1

我仍然不能讓這件事在我的腦海:JNDI春天嵌入的Tomcat:maven的多模塊項目

這是我的項目的外觀:

--app 
--app-core  (spring) 
--app-model (pojo) 
--app-service (jersey) >> final package as war (dependencies (appcore+appmodel)) 

現在,這裏我應該坐的applicationContext.xml哪裏? ???春天的依賴關係只適用於app-core? ...

UPDATE

應用核心(春季)具有applicationContext.xml的,知道我想用JNDI與嵌入式的Tomcat(Tomcat的Maven的插件)。

已創建context.xml的webapp/META-INF裏面的樣子: -

<?xml version='1.0' encoding='utf-8'?> 
<Context docBase="nweb" path="/nweb" reloadable="true"> 
<WatchedResource>WEB-INF/web.xml</WatchedResource> 
<Resource name="jdbc/TestDS" auth="Container" 
     type="javax.sql.DataSource" 
     driverClass="net.sourceforge.jtds.jdbc.Driver" 
     url="jdbc:sqlserver://localhost:1433;DatabaseName=TestData" 
     username="sa" password=""/>  
    </Context> 

..

我的applicationContext.xml: -

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> 

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName" value="java:/TestDS"></property> 
</bean>  
</beans> 

上述我得到這個以下錯誤:

at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352) 
    Caused by: javax.naming.NameNotFoundException: Name TestDSis not bound in this 
    Context 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:770) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:153) 
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152) 
    at javax.naming.InitialContext.lookup(InitialContext.java:392) 
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154) 
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) 
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152) 
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) 
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) 
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) 
    at 

任何建議,如果我錯過了什麼?

回答

2

通常你需要配置你的web.xml中的ContextLoaderListener爲你的根應用上下文:

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

該類使用參數值的方式contextConfigLocation的 - 從中​​你可以明確地指定你的applicationContext.xml中應該是:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value> 
</context-param> 
+0

確定已經做了一些改變..請您檢查我更新的qn嗎? – 2013-02-13 07:30:51

+0

Aww沒關係必須使用'' – 2013-02-13 08:45:14

0

嘗試

<plugin> 
    <groupId>org.apache.tomcat.maven</groupId> 
    <artifactId>tomcat7-maven-plugin</artifactId> 
    <configuration> 
     <contextFile>webapp/META-INF/context.xml</contextFile> 
    </configuration> 
    </plugin> 
0

<property name="jndiName" value="java:comp/env/TestDS"></property>

0

,如果你使用的是嵌入式的Tomcat 7,那麼你應該叫

tomcat.enableNaming(); 
0

你有一個應用程序使用JNDI here工作。我一直在PaaS上使用JNDI,他們在這裏解釋瞭如何在應用程序和數據庫之間進行綁定。你可以在這個tutorial有更多的細節。

基本上你需要把這個在你的datasource.xml文件:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/mydb" resource-ref="true" 
    expected-type="javax.sql.DataSource"/> 

然後,Tomcat服務器上,你有幾種可能性,你可以在春天的教程,我把鏈接查看。最簡單的方法是到$ CATALINA_HOME/conf/context.xml並放置這樣的東西。

<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" maxActive="20" maxIdle="10" minIdle="1" name="jdbc/mydb" password="dbUserPassword" testOnBorrow="true" testWhileIdle="true" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/dbName" username="dbUserName" validationInterval="5000" validationQuery="select 1"/>