Announcement Announcement Module
Collapse
No announcement yet.
ContextLoaderServlet loading rwice Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ContextLoaderServlet loading rwice

    Hi folks!

    Usecase:
    spring 2.0.5
    websphere 5.0.2.x (only supports servlet-api up to 2.3)
    ear-application with one MDB, one EJB and one WAR module.
    One common classloader for the whole ear (e.g. all the modules are sharing the same classloader).
    Classloadermode for ear: parent last (to be able to use newer versions of libraries which are also contained in the appserver - but to load my own from the ear).

    What I want to achieve:
    Load a beanRefContext.xml (containing several contexts) - and share this with the ejb, mdb and the war.

    The war contains a DispatcherServlet - which references several of the beans in the beanRefContext.xml' contexts.


    Excerpt from web.xml:


    <web-app id="web-app_1">
    <distributable/>

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:beanRefContext.xml</param-value>

    </context-param>
    <context-param>
    <param-name>locatorFactorySelector</param-name>
    <param-value>classpath*:beanRefContext.xml</param-value>

    </context-param>
    <context-param>
    <param-name>parentContextKey</param-name>
    <param-value>main-context</param-value>

    </context-param>

    <!--
    To use non XDoclet filters, create a filters.xml file that
    contains the additional filters (eg Sitemesh) and place it in your
    project's merge dir. Don't include filter-mappings in this file,
    include them in a file called filter-mappings.xml and put that in
    the same directory.
    -->

    <!--
    To use non XDoclet filter-mappings, create a filter-mappings.xml file that
    contains the additional filter-mappings and place it in your
    project's merge dir.
    -->

    <!--
    To use non XDoclet listeners, create a listeners.xml file that
    contains the additional listeners and place it in your
    project's merge dir.
    -->

    <!-- initializes log4j with configureAndWatch -->
    <servlet>
    <servlet-name>Log4JInitializer</servlet-name>
    <servlet-class>mypackage.Log4JInitializer</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- MUST be loaded before the next servlet to activate the root context -->
    <servlet>
    <servlet-name>Spring ContextLoaderServlet</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
    </servlet>

    <!-- a real-life controller for running queries agains TM -->
    <servlet>
    <servlet-name>Dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <init-param>contextConfigLocation</init-param>
    <value>classpath*ispatcher-servlet.xml</init-param>
    <load-on-startup>3</load-on-startup>
    </servlet>


    In the MDB and the EJB I do this in setSessionContext / setMessageDrivenContext:

    setBeanFactoryLocator( ContextSingletonBeanFactoryLocator.getInstance( "classpath*:beanRefContext.xml" ) );

    setBeanFactoryLocatorKey( "main-context" );

    my beanRefContext.xml contains a list of context's to load, and it's id is "main-context".


    Still - stuff is being initialized twice!!!:

    fgrep beanRefContext.xml app.log:

    2007-06-15 19:42:04,133 [SoapConnectorThreadPool : 1] INFO org.springframework.beans.factory.xml.XmlBeanDefin itionReader - Loading XML bean definitions from URL [file:/common/api-U/properties/beanRefContext.xml] (XmlBeanDefinitionReader.java:293)
    2007-06-15 19:42:05,434 [SoapConnectorThreadPool : 1] DEBUG org.springframework.beans.factory.xml.XmlBeanDefin itionReader - Loaded 1 bean definitions from location pattern [classpath*:beanRefContext.xml] (AbstractBeanDefinitionReader.java:149)
    2007-06-15 19:42:05,484 [SoapConnectorThreadPool : 1] DEBUG org.springframework.beans.factory.support.DefaultL istableBeanFactory - Creating instance of bean 'main-context' with merged definition [Root bean: class [org.springframework.context.support.ClassPathXmlAp plicationContext]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/common/api-U/properties/beanRefContext.xml]] (AbstractAutowireCapableBeanFactory.java:345)
    2007-06-15 19:42:10,865 [SoapConnectorThreadPool : 1] INFO org.springframework.beans.factory.xml.XmlBeanDefin itionReader - Loading XML bean definitions from URL [file:/common/api-U/properties/beanRefContext.xml] (XmlBeanDefinitionReader.java:293)
    2007-06-15 19:42:10,960 [SoapConnectorThreadPool : 1] DEBUG org.springframework.beans.factory.xml.XmlBeanDefin itionReader - Loaded 1 bean definitions from location pattern [classpath*:beanRefContext.xml] (AbstractBeanDefinitionReader.java:149)
    2007-06-15 19:42:10,997 [SoapConnectorThreadPool : 1] DEBUG org.springframework.beans.factory.support.DefaultL istableBeanFactory - Creating instance of bean 'main-context' with merged definition [Root bean: class [org.springframework.context.support.ClassPathXmlAp plicationContext]; scope=singleton; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/common/api-U/properties/beanRefContext.xml]] (AbstractAutowireCapableBeanFactory.java:345)




    And I see this at the end of the log when starting:
    2007-06-15 19:53:47,108 [SoapConnectorThreadPool : 2] DEBUG org.springframework.web.context.support.XmlWebAppl icationContext - Publishing event in context [org.springframework.web.context.support.XmlWebAppl icationContext@17e4ff]: org.springframework.context.event.ContextRefreshed Event[source=org.springframework.web.context.support.Xml WebApplicationContext@2145e6: display name [WebApplicationContext for namespace 'Dispatcher-servlet']; startup date [Fri Jun 15 19:53:46 CEST 2007]; parent: org.springframework.web.context.support.XmlWebAppl icationContext@17e4ff] (AbstractApplicationContext.java:244)
    2007-06-15 19:53:47,110 [SoapConnectorThreadPool : 2] DEBUG org.springframework.context.support.ClassPathXmlAp plicationContext - Publishing event in context [org.springframework.context.support.ClassPathXmlAp plicationContext@7cad10]: org.springframework.context.event.ContextRefreshed Event[source=org.springframework.web.context.support.Xml WebApplicationContext@2145e6: display name [WebApplicationContext for namespace 'Dispatcher-servlet']; startup date [Fri Jun 15 19:53:46 CEST 2007]; parent: org.springframework.web.context.support.XmlWebAppl icationContext@17e4ff] (AbstractApplicationContext.java:244)
    2007-06-15 19:53:47,111 [SoapConnectorThreadPool : 2] DEBUG org.springframework.web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'Dispatcher' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.C ONTEXT.Dispatcher] (FrameworkServlet.java:279)
    2007-06-15 19:53:47,113 [SoapConnectorThreadPool : 2] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'Dispatcher': initialization completed in 1076 ms (FrameworkServlet.java:250)
    2007-06-15 19:53:47,114 [SoapConnectorThreadPool : 2] DEBUG org.springframework.web.servlet.DispatcherServlet - Servlet 'Dispatcher' configured successfully (HttpServletBean.java:129)
    2007-06-15 19:55:00,034 [QuartzScheduler_Worker-0] DEBUG org.quartz.core.JobRunShell - Calling execute on job defaultGroup.mail statistics job (JobRunShell.java:194)
    2007-06-15 19:55:00,845 [QuartzScheduler_Worker-0] DEBUG org.quartz.core.JobRunShell - Calling execute on job defaultGroup.mail statistics job (JobRunShell.java:194)

    (Context initialized twice results in a quartz job being scheduled twice).

    I've done another application on was6.1 - but using a context listener instead - and that works OK - so why doesn't the loader work when using a servlet?



    Anybody got an idea?

  • #2
    From what I understand you ask it two load the context twice - but not the ContextLoaderServlet. You have set up the locatorFactorySelector to the same XML config file. This means you end up with a root application context (locatorFactorySelector) and as child of it a root web application context. They should not point to the same XML config file.

    Jörg

    Comment


    • #3
      contextConfigLocation

      OK - I've now set the param to:
      <param-name>contextConfigLocation</param-name>
      <param-value></param-value>


      that would be correct?

      I thougth I could load the classpath*ispatcher-servlet.xml there - but it seems it has to be an init-param of the DispatcherServlet.

      The beanRefContext's are only loaded once now - but I'm unsure of the web-part.

      Comment


      • #4
        If that works ... looks indeed a bit strange now. I have no experience with packaging in EAR, so I can't really say what has to be placed where.

        You're correct about the DispatcherServlet's init-param.

        Jörg

        Comment

        Working...
        X