Java JBoss中的Spring启动了2个上下文

Java JBoss中的Spring启动了2个上下文,java,spring,spring-mvc,jboss,Java,Spring,Spring Mvc,Jboss,我在JBoss中部署了一个基于spring的应用程序。我有一些@PostConstruct方法的bean。当我启动应用程序时,这些bean被初始化两次。似乎正在加载两种不同的上下文。我可以在我的日志中看到以下内容: 2014-04-09 16:56:49 INFO [ServerService Thread Pool -- 58] web.context.ContextLoader:285 - Root WebApplicationContext: initialization started

我在JBoss中部署了一个基于spring的应用程序。我有一些@PostConstruct方法的bean。当我启动应用程序时,这些bean被初始化两次。似乎正在加载两种不同的上下文。我可以在我的日志中看到以下内容:

2014-04-09 16:56:49 INFO  [ServerService Thread Pool -- 58] web.context.ContextLoader:285 - Root WebApplicationContext: initialization started
2014-04-09 16:56:49 INFO  [ServerService Thread Pool -- 58] context.support.AbstractApplicationContext:513 - Refreshing Root WebApplicationContext: startup date [Wed Apr 09 16:56:49 EST 2014]; root of context hierarchy
2014-04-09 16:56:49 INFO  [ServerService Thread Pool -- 58] factory.xml.XmlBeanDefinitionReader:316 - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
.........
2014-04-09 16:56:49 INFO  [ServerService Thread Pool -- 58] com.router.SingleSocketMessageTransporter:150 - START : Initializing MessageTransporter [Host: localhost, Port: 8787]
2014-04-09 16:56:49 INFO  [ServerService Thread Pool -- 58] com.router.SocketFactoryImpl:54 - Sockect connection established with host localhost at port 8787
2014-04-09 16:56:49 INFO  [ServerService Thread Pool -- 58] com.router.SingleSocketMessageTransporter:165 - END : Successfully initialized MessageTransporter [Host: localhost, Port: 8787]
2014-04-09 16:56:49 DEBUG [ServerService Thread Pool -- 58] com.router.MessageBrokerImpl:157 - START: Message broker initialization.
2014-04-09 16:56:49 DEBUG [ServerService Thread Pool -- 58] com.router.MessageBrokerImpl:182 - END: Message Broker Initialization
2014-04-09 16:56:50 INFO  [ServerService Thread Pool -- 58] com.service.NetworkMgmtServiceImp:115 - Network Post construct.
.........
2014-04-09 16:56:50 INFO  [ServerService Thread Pool -- 58] web.context.ContextLoader:325 - Root WebApplicationContext: initialization completed in 1748 ms 
.........
2014-04-09 16:56:50 INFO  [ServerService Thread Pool -- 58] web.servlet.FrameworkServlet:479 - FrameworkServlet 'mvc-dispatcher': initialization started
2014-04-09 16:56:50 INFO  [ServerService Thread Pool -- 58] context.support.AbstractApplicationContext:513 - Refreshing WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Wed Apr 09 16:56:50 EST 2014]; parent: Root WebApplicationContext
2014-04-09 16:56:50 INFO  [ServerService Thread Pool -- 58] factory.xml.XmlBeanDefinitionReader:316 - Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
..........
2014-04-09 16:56:51 INFO  [ServerService Thread Pool -- 58] com.router.SingleSocketMessageTransporter:150 - START : Initializing MessageTransporter [Host: localhost, Port: 8787]
2014-04-09 16:56:51 INFO  [ServerService Thread Pool -- 58] com.router.SocketFactoryImpl:54 - Sockect connection established with host localhost at port 8787
2014-04-09 16:56:51 INFO  [ServerService Thread Pool -- 58] com.router.SingleSocketMessageTransporter:165 - END : Successfully initialized MessageTransporter [Host: localhost, Port: 8787]
2014-04-09 16:56:51 DEBUG [ServerService Thread Pool -- 58] com.router.MessageBrokerImpl:157 - START: Message broker initialization.
2014-04-09 16:56:51 DEBUG [ServerService Thread Pool -- 58] com.router.MessageBrokerImpl:182 - END: Message Broker Initialization
2014-04-09 16:56:51 INFO  [ServerService Thread Pool -- 58] com.service.NetworkMgmtServiceImp:115 - Network Post construct.
我的web.xml配置为:

 <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

mvc调度器
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/applicationContext.xml
1.
谁能解释一下这里的行为,以及如何避免我的bean双重初始化


感谢您在web.xml中指定了ContextLoader/ContextLoaderListener,但没有指定上下文参数“contextConfigLocation”(根上下文)。如果未明确指定“contextConfigLocation”,则根上下文实现应使用默认位置(使用XmlWebApplicationContext:“/WEB-INF/applicationContext.xml”)。因此,这将从applicationContext文件加载bean。 同样,这是在mvc dispatcher的init param contextConfigLocation(servlet名称)中明确指定的。这将导致第二次加载bean

注: 如果未指定mvc dispatcher的init param contextConfigLocation(servlet名称),它将在/WEB-INF/mvc-dispatcher-servlet.xml中查找文件。
根据经验,将公共服务层移动到根上下文。如果其ear模块具有多个war类型结构,则您需要在父上下文中配置公共服务。

可能的重复项您需要详细介绍您的环境以及如何部署应用程序。通过提供的日志,我们可以看到两个组件(
WebApplicationContext
FrameworkServlet
),已正确初始化。要让我们相信确实存在问题,您必须提供有关应用程序的更多信息和更多日志。现在我们没有看到一个
初始化两次的问题。如果您认为它加载了两次,请在您的一个类中输出
System.out.println()
,然后查看它是否被实例化了两次。我的猜测是其他人是对的,而事实并非如此。从给定的web.xml来看,似乎我没有定义任何应用程序上下文(context-param-contextConfigLocation),我的servlet上下文首先作为应用程序上下文加载,然后作为servlet上下文加载。因此,bean在不同的上下文中被初始化两次。通过在上下文参数中定义my applicationContext.xml并在dispatcher servlet上下文中添加空的servletContext.xml修复了此问题。通过在上下文参数中定义my applicationContext.xml并在dispatcher servlet上下文中添加空的servletContext.xml修复了此问题