1

我仍然无法理解这件事:

这就是我的项目的样子:

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

现在我的 applicationContext.xml 应该放在哪里???Spring 的依赖项仅适用于 app-core ???? ...

更新

app-core (spring) 有 applicationContext.xml,知道我想将 JNDI 与嵌入式 tomcat (tomcat-maven-plugin) 一起使用。

在 webapp/META-INF 中创建了context.xml 它看起来像:-

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

如果我错过了什么,有什么建议吗?

4

5 回答 5

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>
于 2013-02-12T21:52:26.950 回答
0

尝试

  <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <configuration>
      <contextFile>webapp/META-INF/context.xml</contextFile>
    </configuration>
  </plugin>
于 2013-02-13T11:05:11.467 回答
0

您在这里有一个使用 JNDI 的应用程序。我一直在 PaaS 上使用 JNDI,他们解释了如何在您的应用程序和数据库之间进行绑定。您可以了解本教程的更多详细信息。

基本上你需要把它放在你的 datasource.xml 文件中:

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

然后,在您的 Tomcat 服务器上,您有多种可能性,正如您在我放置链接的 Spring 教程中看到的那样。最简单的方法是转到 $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"/>
于 2014-01-20T08:42:05.143 回答
0

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

于 2013-04-23T19:15:58.840 回答
0

如果您使用的是嵌入式 tomcat 7,那么您应该调用

tomcat.enableNaming();
于 2014-01-15T18:13:45.200 回答