应用程序上下文加载两次 [英] Application context loading twice

查看:42
本文介绍了应用程序上下文加载两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 Eclipse 的 Tomcat 7 和 Struts 3 编写 Web 应用程序.当我启动 Tomcat 时,上下文似乎被加载了两次(日志文件显示 applicationContext.xml 被读取了两次).

I am writing a web application with Struts 3 using Tomcat 7 with Eclipse. When I launch Tomcat, it seems the context is loaded twice (log file shows that applicationContext.xml is read twice).

我在 src/main/webapp/META-INF 下有一个用于 Tomcat 的 context.xml 文件:

I have a context.xml file under src/main/webapp/META-INF for Tomcat :

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/dbname" docBase="dbname" reloadable="true" debug="1">
  <Resource name="jdbc/dbname"
            username="dbusername"
            password="dbpassword"
            auth="Container"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/dbname"
            type="javax.sql.DataSource"
            initialSize="5"
            maxActive="120"
            maxIdle="5"
            maxWait="5000"
            poolPreparedStatements="true"
            validationQuery="select 1" />
</Context>

我的 server.xml 也声明(在主机和引擎下):

My server.xml declares also (under Host and Engine) :

<Context docBase="dbname" path="/dbname" source="org.eclipse.jst.jee.server:dbname"/>

没有这个,Tomcat 不会启动应用程序.

Without this, Tomcat does not launch the application.

在 web.xml 中,我像这样声明 contextConfigLocation:

In web.xml, I declare the contextConfigLocation like this :

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>

applicationContext.xml 位于 src/main/resources/spring 下

applicationContext.xml is located under src/main/resources/spring

这是日志文件中显示重新加载的两行:

Those are the two lines from the log file showing the reload :

2012-10-14 00:17:08,191 INFO [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'name': initialization completed in 438 ms
2012-10-14 00:17:10,972 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - Closing WebApplicationContext for namespace 'name-servlet': startup date [Sun Oct 14 00:17:07 CEST 2012]; parent: Root WebApplicationContext

问题是在第二次重新加载时,它失败了,因为 Spring 找不到任何数据源(尽管肯定定义了一个,因为 Spring 在第一次启动时找到了它)

Issue is that on the second reload, it fails because Spring can't find any data source (although one is definitely defined since Spring finds it during the first boot)

推荐答案

我在 src/main/webapp/META-INF 下有一个用于 Tomcat 的 context.xml 文件:[...]

I have a context.xml file under src/main/webapp/META-INF for Tomcat: [...]

我的 server.xml 也声明(在主机和引擎下):

My server.xml declares also (under Host and Engine) :

因此,您已经部署了两次 Web 应用程序.你期待不同的东西吗?

So, you've deployed your webapp twice. Were you expecting something different?

从 server.xml 中删除 :它不属于那里.然后,您的网络应用程序应该只加载一次.

Remove the <Context> from server.xml: it doesn't belong there. Then, your webapp should only load once.

这篇关于应用程序上下文加载两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆