6

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
  • 2
    We can't answer this without seeing your config. Commented Feb 15, 2011 at 21:46

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

4 Comments

I get a bunch of "Could not autowire field" error when I start the server without ContextLoaderListener. I think it is not loading the applicationContext.xml at all anymore... =(
I noticed this behavior because my jms listener would start when server starts. BUT spring will try to create/connect another listener once I run my controller that does nothing but return a view...
still stuck on this one, any other ideas?
nevermind, I got it, I split the jms part into another xml, so the dispatcher won't know about it =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.