【问题标题】:JNDI spring embedded tomcat : maven multi module projectJNDI spring嵌入式tomcat:maven多模块项目
【发布时间】:2013-02-12 20:42:39
【问题描述】:

我仍然无法理解这件事:

这就是我的项目的样子:

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

如果遗漏了什么,有什么建议吗?

【问题讨论】:

    标签: spring maven jndi multi-module maven-tomcat-plugin


    【解决方案1】:

    通常你会在你的 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>
    

    【讨论】:

    • 好的,已经做了一些改变..你能检查我更新的 qn 吗?
    【解决方案2】:

    试试

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

    【讨论】:

      【解决方案3】:

      试试&lt;property name="jndiName" value="java:comp/env/TestDS"&gt;&lt;/property&gt;

      【讨论】:

        【解决方案4】:

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

        tomcat.enableNaming();
        

        【讨论】:

          【解决方案5】:

          您有一个使用 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 服务器上,您有多种可能性,正如您在我放置链接的 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"/>
          

          【讨论】:

            猜你喜欢
            • 2020-10-18
            • 2017-09-20
            • 2022-01-24
            • 2018-10-07
            • 2015-03-23
            • 1970-01-01
            • 2015-10-10
            • 2022-01-08
            • 1970-01-01
            相关资源
            最近更新 更多