Announcement Announcement Module
Collapse
No announcement yet.
Singleton bean created twice? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Singleton bean created twice?

    Hello!

    I experience a very strange behaviour at application startup, for which i do not find an explanation.
    I have a singleton bean defined in the application context:
    Code:
    <bean id="eventMulticaster" class="org.springframework.context.event.ApplicationEventMulticasterImpl"/>
    When i start my application, i get the following output (note that "Creating shared instance of singleton bean 'eventMulticaster' " appears twice).
    Code:
    ...
    Loading XML bean definitions from resource &#91;/WEB-INF/bwz-servlet.xml&#93; of ServletContext 
    2004-09-06 15&#58;50&#58;56,872 INFO &#91;org.springframework.beans.factory.xml.XmlBeanDefinitionReader&#93; 
     Loading XML bean definitions from resource &#91;/WEB-INF/bwz-security.xml&#93; of ServletContext 
    2004-09-06 15&#58;50&#58;57,193 INFO &#91;org.springframework.beans.factory.xml.XmlBeanDefinitionReader&#93; 
     Loading XML bean definitions from resource &#91;/WEB-INF/bwz-db-hibernate.xml&#93; of ServletContext 
    2004-09-06 15&#58;50&#58;57,332 INFO &#91;org.springframework.web.context.support.XmlWebApplicationContext&#93; 
     Bean factory for application context &#91;Root XmlWebApplicationContext&#93;&#58; org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans &#91;&#40;...&#41;&#93;; Root of BeanFactory hierarchy 
    2004-09-06 15&#58;50&#58;57,362 INFO &#91;org.springframework.web.context.support.XmlWebApplicationContext&#93; 
     58 beans defined in ApplicationContext &#91;Root XmlWebApplicationContext&#93; 
    ...
    2004-09-06 15&#58;50&#58;57,624 INFO &#91;org.springframework.beans.factory.support.DefaultListableBeanFactory&#93; 
     Creating shared instance of singleton bean 'eventMulticaster' 
    ...
     Published root WebApplicationContext &#91;org.springframework.web.context.support.XmlWebApplicationContext&#58; displayName=&#91;Root XmlWebApplicationContext&#93;; startup date=&#91;Mon Sep 06 15&#58;50&#58;55 CEST 2004&#93;; root of ApplicationContext hierarchy; config locations=&#91;/WEB-INF/bwz-servlet.xml,/WEB-INF/bwz-security.xml,/WEB-INF/bwz-db-hibernate.xml&#93;; &#93; as ServletContext attribute with name &#91;interface org.springframework.web.context.WebApplicationContext.ROOT&#93; 
    2004-09-06 15&#58;52&#58;38,355 INFO &#91;org.springframework.web.servlet.DispatcherServlet&#93; 
     Initializing servlet 'bwz' 
    2004-09-06 15&#58;52&#58;38,423 INFO &#91;org.springframework.web.servlet.DispatcherServlet&#93; 
     Framework servlet 'bwz' init 
    2004-09-06 15&#58;52&#58;38,424 INFO &#91;org.springframework.web.servlet.DispatcherServlet&#93; 
     Servlet with name 'bwz' will try to create custom WebApplicationContext context of class 'org.springframework.web.context.support.XmlWebApplicationContext' using parent context &#91;org.springframework.web.context.support.XmlWebApplicationContext&#58; displayName=&#91;Root XmlWebApplicationContext&#93;; startup date=&#91;Mon Sep 06 15&#58;50&#58;55 CEST 2004&#93;; root of ApplicationContext hierarchy; config locations=&#91;/WEB-INF/bwz-servlet.xml,/WEB-INF/bwz-security.xml,/WEB-INF/bwz-db-hibernate.xml&#93;; &#93; 
    ...
    2004-09-06 15&#58;56&#58;24,735 INFO &#91;org.springframework.beans.factory.support.DefaultListableBeanFactory&#93; 
     Creating shared instance of singleton bean 'eventMulticaster'
    I do not know why this bean is created twice, and why some of my classes get bean 1 and others bean 2.
    I saw the lines which say that a context for servlet bwz with the root context as parent is constructed. But i do not know what this means... i only have one servlet with application contexts and do not try to build up parent-child-contexts (at least not to my knowledge...!).

    Does somebody have an idea? I am a bit lost after searching around for three hours...

    Thanks a lot!
    Sebastian

  • #2
    Can yuo post more of your code. Specifically can you post web.xml, the application context config for that bean in full and also any code you have that may create application contexts.

    Comment


    • #3
      Okay, i solved this problem today... just in case somebody else runs into the same situation: i was not aware that two contexts are created, one big "parent" application context and one servlet specific context.

      Unfortunately one of my config files was included in both contexts, and therfore certain beans were created twice. Events sent within one context did not reach beans in the other one.... this really took me a few hours to learn, but now it's working and i am happy again... ;-)

      Sebastian

      Comment

      Working...
      X