Spring 3 MVC dispatcher xml and applicationContext

2020-04-05 07:40发布

I am creating a Spring MVC application for the first time.

It seems like when I start up the server, the applicationContext.xml loads the first time even before I run any mvc controller; This is what I want.

BUT once I run a controller that is loaded with context:component-scan in the dispatcher.xml ....IT SEEMS that the applicationContext.xml gets loaded again... Why is this happening and how do I disable this? I only want my applicationContext.xml to run once.

Right after I run a controller, I see the logs below...

ClassPathXmlA I org.springframework.context.support.AbstractApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@65cb65cb: startup date [Tue Feb 15 16:29:21 EST 2011]; root of context hierarchy
XmlBeanDefini I org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions Loading XML bean definitions from class path resource [WEB-INF/applicationContext.xml]

I think this is also causing my jms DefaultMessageListenerContainer to be created twice...

thanks

xxxdispatcher-servlet.xml

<context:component-scan base-package="com.something.web" />
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<mvc:interceptors>
<bean class="com.something.SomeInterceptor" />
</mvc:interceptors>

<mvc:resources mapping="/js/**" location="/js/" />

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
    <props>
        <prop key="java.lang.Exception">common/error</prop>
    </props>
    </property>
    <property name="warnLogCategory" value="abcdefg"/>  
</bean>

applicationContext.xml

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/application.properties" />
</bean>

<!-- Local Data Holder -->
<bean id="propertyHolder" class="com.common.PropertyHolder">
<property name="baseURL" value="${url.base}" />
</bean>

<bean id="messageListener" class="com.something.SomeListener" />

<bean id="xxxDAO"
    class="com.XXXDAOImpl"
    scope="prototype">
    <property name="dataSource" ref="dataSourceQA" />
</bean>

<bean id="xxxServiceTarget" class="com.XXXServiceImpl">
    <property name="xxxDAO" ref="xxxDAO"/>
</bean>

<bean id="xxxService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="txManager"/>
    <property name="target" ref="xxxServiceTarget"/>
    <property name="transactionAttributes">
        <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
        </props>
    </property>
</bean>

<!-- and this is the message listener container -->
<bean id="jmsContainer"
    class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="xxxCF" />
    <property name="destination" ref="xxxInboundQueue" />
    <property name="messageListener" ref="messageListener" />
</bean>

WEB.xml

<servlet>
    <servlet-name>xxxdispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>xxxdispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

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

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

Controller

  @Controller
    public class XXXController {

        @Autowired
        private IXXXService xxxService;

        @RequestMapping("/xxx")
        public String xxxHandler() throws Exception {

            return "xxxView";
        }

1条回答
beautiful°
2楼-- · 2020-04-05 08:33

Please remove ContextLoaderListener from your Web.xml I beleive this is the reason why your context is created twice.

查看更多
登录 后发表回答