Announcement Announcement Module
Collapse
No announcement yet.
all beans being registered twice Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • all beans being registered twice

    Hi all

    All of my beans defined in my various xml files seem to be instantiated twice.

    I ran into this problem today when I created my first bean with an init-method defined which outputs a few lines to stoud. I noticed that the text was being written twice found that the init-method was being called twice.

    I've found a couple of posts containing similar problems but havent found a solution. Has anyone else experienced beans being loaded multiple times? If it makes any difference, I am using Weblogic 8.1.

    a snippet from my myapp-servlet.xml file is

    Code:
    <bean id="startupController" class="com.wdig.intl.tpi.mvc.StartupController" init-method="init">
        <property name="tpiService"><ref bean="tpiService"/></property>
    </bean>
    my web.xml file is
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http&#58;//java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
        <display-name>tpi-web</display-name>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/tpi-data.xml,/WEB-INF/tpi-service.xml,/WEB-INF/tpi-servlet.xml</param-value>
        </context-param>
    
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>/WEB-INF/log4j.properties</param-value>
         </context-param>
    
        <!--
        - Key of the system property that should specify the root directory of this
        - web app. Applied by WebAppRootListener or Log4jConfigListener.
        -->
        <context-param>
            <param-name>webAppRootKey</param-name>
            <param-value>tpi.root</param-value>
        </context-param>
    
        <!-- Define the basename for a resource bundle for I18N. Required to get the <fmt&#58;message> tag to work -->
        <context-param>
            <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
            <param-value>tpi-messages</param-value>
        </context-param>
        <context-param>
            <param-name>javax.servlet.jsp.jstl.fmt.fallbackLocale</param-name>
            <param-value>en</param-value>
        </context-param>
    
        <filter>
            <filter-name>requestlogfilter</filter-name>
            <filter-class>com.wdig.intl.tpi.web.RequestLogFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>requestlogfilter</filter-name>
            <url-pattern>*.spring</url-pattern>
        </filter-mapping>
    
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>context</servlet-name>
            <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet>
            <servlet-name>tpi</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>tpi</servlet-name>
            <url-pattern>*.spring</url-pattern>
        </servlet-mapping>
    
        <mime-mapping>
            <extension>xls</extension>
            <mime-type>application/vnd.ms-excel</mime-type>
        </mime-mapping>
    
    	<error-page>
    		<exception-type>java.lang.Exception</exception-type>
    		<location>/WEB-INF/jsp/uncaughtException.jsp</location>
        </error-page>
    
        <taglib>
            <taglib-uri>/spring</taglib-uri>
            <taglib-location>/WEB-INF/spring.tld</taglib-location>
        </taglib>
    
    
    </web-app>

  • #2
    Maybe you could extend your output to dump a stacktrace. So you will be able to find out from where the initializations are triggered. Maybe also an output from the constructor might be helpful to see if only initialization or also construction takes place twice.

    Regards,
    Andreas

    Comment


    • #3
      Thanks. Thats a damn good idea.

      The 2 stack traces are as below

      it seems that the first time the bean is loaded is by the line
      Code:
      org.springframework.web.context.ContextLoader.createWebApplicationContext&#40;ContextLoader.java&#58;230&#41;
      and the second time is by the line
      Code:
      org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext&#40;FrameworkServlet.java&#58;305&#41;
      Anyone have a clue why my beans are being loaded by two separate parts of the Spring framework?

      Code:
      java.lang.Exception&#58; constructor
              at com.wdig.intl.tpi.mvc.StartupController.<init>&#40;StartupController.java&#58;22&#41;
              at sun.reflect.NativeConstructorAccessorImpl.newInstance0&#40;Native Method&#41;
              at sun.reflect.NativeConstructorAccessorImpl.newInstance&#40;NativeConstructorAccessorImpl.java&#58;39&#41;
              at sun.reflect.DelegatingConstructorAccessorImpl.newInstance&#40;DelegatingConstructorAccessorImpl.java&#58;27&#41;
              at java.lang.reflect.Constructor.newInstance&#40;Constructor.java&#58;274&#41;
              at org.springframework.beans.BeanUtils.instantiateClass&#40;BeanUtils.java&#58;100&#41;
              at org.springframework.beans.BeanUtils.instantiateClass&#40;BeanUtils.java&#58;76&#41;
              at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate&#40;SimpleInstantiationStrategy
      .java&#58;49&#41;
              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapab
      leBeanFactory.java&#58;316&#41;
              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;226&#41;
              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;147&#41;
              at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons&#40;DefaultListable
      BeanFactory.java&#58;275&#41;
              at org.springframework.context.support.AbstractApplicationContext.refresh&#40;AbstractApplicationContext.java&#58;317&#41;
              at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh&#40;AbstractRefreshableW
      ebApplicationContext.java&#58;134&#41;
              at org.springframework.web.context.ContextLoader.createWebApplicationContext&#40;ContextLoader.java&#58;230&#41;
              at org.springframework.web.context.ContextLoader.initWebApplicationContext&#40;ContextLoader.java&#58;156&#41;
              at org.springframework.web.context.ContextLoaderServlet.init&#40;ContextLoaderServlet.java&#58;81&#41;
              at javax.servlet.GenericServlet.init&#40;GenericServlet.java&#58;258&#41;
              at weblogic.servlet.internal.ServletStubImpl$ServletInitAction.run&#40;ServletStubImpl.java&#58;993&#41;
              at weblogic.security.acl.internal.AuthenticatedSubject.doAs&#40;AuthenticatedSubject.java&#58;317&#41;
              at weblogic.security.service.SecurityManager.runAs&#40;SecurityManager.java&#58;118&#41;
              at weblogic.servlet.internal.ServletStubImpl.createServlet&#40;ServletStubImpl.java&#58;869&#41;
              at weblogic.servlet.internal.ServletStubImpl.createInstances&#40;ServletStubImpl.java&#58;848&#41;
              at weblogic.servlet.internal.ServletStubImpl.prepareServlet&#40;ServletStubImpl.java&#58;787&#41;
              at weblogic.servlet.internal.WebAppServletContext.preloadServlet&#40;WebAppServletContext.java&#58;3252&#41;
              at weblogic.servlet.internal.WebAppServletContext.preloadServlets&#40;WebAppServletContext.java&#58;3209&#41;
              at weblogic.servlet.internal.WebAppServletContext.preloadServlets&#40;WebAppServletContext.java&#58;3195&#41;
              at weblogic.servlet.internal.WebAppServletContext.preloadResources&#40;WebAppServletContext.java&#58;3174&#41;
              at weblogic.servlet.internal.HttpServer.preloadResources&#40;HttpServer.java&#58;688&#41;
              at weblogic.servlet.internal.WebService.preloadResources&#40;WebService.java&#58;483&#41;
              at weblogic.servlet.internal.ServletInitService.resume&#40;ServletInitService.java&#58;30&#41;
              at weblogic.t3.srvr.SubsystemManager.resume&#40;SubsystemManager.java&#58;131&#41;
              at weblogic.t3.srvr.T3Srvr.resume&#40;T3Srvr.java&#58;964&#41;
              at weblogic.t3.srvr.T3Srvr.run&#40;T3Srvr.java&#58;359&#41;
              at weblogic.Server.main&#40;Server.java&#58;32&#41;
      Code:
      java.lang.Exception&#58; constructor
              at com.wdig.intl.tpi.mvc.StartupController.<init>&#40;StartupController.java&#58;22&#41;
              at sun.reflect.NativeConstructorAccessorImpl.newInstance0&#40;Native Method&#41;
              at sun.reflect.NativeConstructorAccessorImpl.newInstance&#40;NativeConstructorAccessorImpl.java&#58;39&#41;
              at sun.reflect.DelegatingConstructorAccessorImpl.newInstance&#40;DelegatingConstructorAccessorImpl.java&#58;27&#41;
              at java.lang.reflect.Constructor.newInstance&#40;Constructor.java&#58;274&#41;
              at org.springframework.beans.BeanUtils.instantiateClass&#40;BeanUtils.java&#58;100&#41;
              at org.springframework.beans.BeanUtils.instantiateClass&#40;BeanUtils.java&#58;76&#41;
              at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate&#40;SimpleInstantiationStrategy
      .java&#58;49&#41;
              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean&#40;AbstractAutowireCapab
      leBeanFactory.java&#58;316&#41;
              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;226&#41;
              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean&#40;AbstractBeanFactory.java&#58;147&#41;
              at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons&#40;DefaultListable
      BeanFactory.java&#58;275&#41;
              at org.springframework.context.support.AbstractApplicationContext.refresh&#40;AbstractApplicationContext.java&#58;317&#41;
              at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh&#40;AbstractRefreshableW
      ebApplicationContext.java&#58;134&#41;
              at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext&#40;FrameworkServlet.java&#58;305&#41;
              at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext&#40;FrameworkServlet.java&#58;250&#41;
              at org.springframework.web.servlet.FrameworkServlet.initServletBean&#40;FrameworkServlet.java&#58;219&#41;
              at org.springframework.web.servlet.HttpServletBean.init&#40;HttpServletBean.java&#58;111&#41;
              at javax.servlet.GenericServlet.init&#40;GenericServlet.java&#58;258&#41;
              at weblogic.servlet.internal.ServletStubImpl$ServletInitAction.run&#40;ServletStubImpl.java&#58;993&#41;
              at weblogic.security.acl.internal.AuthenticatedSubject.doAs&#40;AuthenticatedSubject.java&#58;317&#41;
              at weblogic.security.service.SecurityManager.runAs&#40;SecurityManager.java&#58;118&#41;
              at weblogic.servlet.internal.ServletStubImpl.createServlet&#40;ServletStubImpl.java&#58;869&#41;
              at weblogic.servlet.internal.ServletStubImpl.createInstances&#40;ServletStubImpl.java&#58;848&#41;
              at weblogic.servlet.internal.ServletStubImpl.prepareServlet&#40;ServletStubImpl.java&#58;787&#41;
              at weblogic.servlet.internal.WebAppServletContext.preloadServlet&#40;WebAppServletContext.java&#58;3252&#41;
              at weblogic.servlet.internal.WebAppServletContext.preloadServlets&#40;WebAppServletContext.java&#58;3209&#41;
              at weblogic.servlet.internal.WebAppServletContext.preloadServlets&#40;WebAppServletContext.java&#58;3195&#41;
              at weblogic.servlet.internal.WebAppServletContext.preloadResources&#40;WebAppServletContext.java&#58;3174&#41;
              at weblogic.servlet.internal.HttpServer.preloadResources&#40;HttpServer.java&#58;688&#41;
              at weblogic.servlet.internal.WebService.preloadResources&#40;WebService.java&#58;483&#41;
              at weblogic.servlet.internal.ServletInitService.resume&#40;ServletInitService.java&#58;30&#41;
              at weblogic.t3.srvr.SubsystemManager.resume&#40;SubsystemManager.java&#58;131&#41;
              at weblogic.t3.srvr.T3Srvr.resume&#40;T3Srvr.java&#58;964&#41;
              at weblogic.t3.srvr.T3Srvr.run&#40;T3Srvr.java&#58;359&#41;
              at weblogic.Server.main&#40;Server.java&#58;32&#41;

      Comment


      • #4
        Is your controller defined as singleton or not?

        Comment


        • #5
          Originally posted by costin
          Is your controller defined as singleton or not?
          All beans are singleton by default arent they unless you specify singleton=false?

          My bean is defined as follows
          Code:
          <bean id="startupController" class="com.wdig.intl.tpi.mvc.StartupController" init-method="init"> 
              <property name="tpiService"><ref bean="tpiService"/></property> 
          </bean>

          Comment


          • #6
            You can be double sure by specifing singleton="true". If your bean gets loaded twice then make sure you don't have two definitions. If not then the context is loaded twice which means not just this bean but the whole set are loaded twice.

            Comment


            • #7
              I've done that and it still loads twice.

              I've triple checked and my controller is only specified once.

              Yes - it seems the whole context is loaded twice. I wonder why.

              Comment


              • #8
                This happens if the context is loaded declaratively (through some listener) and programatically inside some code. It might sound crazy but if you still can't solve it start tomcat inside a debugger - you'll get the problem for sure.

                Comment


                • #9
                  Doesn't this also happen if you include your servlet-context in your app-context as well?

                  Comment


                  • #10
                    Thanks for the replies costin and yatesco

                    I dont reference my webapp or app context at all programmatically

                    When I debug my bean constructor it gets hit twice on startup - you can see the stack traces from a previous post in this thread.

                    I'm using Weblogic 8.1, not Tomcat if that makes a difference.

                    I dont have an app-context.xml file at all, just myapp-servlet.xml, myapp-service.xml and myapp-data.xml

                    Comment


                    • #11
                      What happens if you comment out the spring listener in the web.xml?

                      Comment


                      • #12
                        The beans wont load if I comment out the ContextLoaderServlet reference in web.xml. I think ContextLoaderServlet must be needed so that Spring knows what xml files to read bean definitions in from?

                        I get an exception saying that a particular bean wont load as it references another bean in another xml file

                        Code:
                        <15/09/2005 02&#58;30&#58;35 PM EST> <Error> <HTTP> <BEA-101216> <Servlet&#58; "tpi" failed to preload on startup in Web application
                        &#58; "tpi-web".
                        javax.servlet.ServletException&#58; Error creating bean with name 'homeController' defined in ServletContext resource &#91;/WEB-
                        INF/tpi-servlet.xml&#93;&#58; Can't resolve reference to bean 'tpiService' while setting property 'tpiService'; nested exception
                         is org.springframework.beans.factory.NoSuchBeanDefinitionException&#58; No bean named 'tpiService' is defined&#58; org.springfr
                        amework.beans.factory.support.DefaultListableBeanFactory defining beans &#91;propertyConfigurer,messageSource,tilesConfigure
                        r,viewResolver,simpleUrlMapping,viewResolver2,sqlExecuteController,authoriseEventController,endEventController,startupCo
                        ntroller,listCustomerImportsController,listPaymentDeliveriesController,forcePaymentFileController,forceBadDebtorsCheckCo
                        ntroller,homeController,readPaymentFileController&#93;; root of BeanFactory hierarchy
                                at weblogic.servlet.internal.ServletStubImpl.createServlet&#40;ServletStubImpl.java&#58;884&#41;
                                at weblogic.servlet.internal.ServletStubImpl.createInstances&#40;ServletStubImpl.java&#58;848&#41;
                                at weblogic.servlet.internal.ServletStubImpl.prepareServlet&#40;ServletStubImpl.java&#58;787&#41;
                                at weblogic.servlet.internal.WebAppServletContext.preloadServlet&#40;WebAppServletContext.java&#58;3252&#41;
                                at weblogic.servlet.internal.WebAppServletContext.preloadServlets&#40;WebAppServletContext.java&#58;3197&#41;
                                at weblogic.servlet.internal.WebAppServletContext.preloadResources&#40;WebAppServletContext.java&#58;3174&#41;
                                at weblogic.servlet.internal.HttpServer.preloadResources&#40;HttpServer.java&#58;688&#41;
                                at weblogic.servlet.internal.WebService.preloadResources&#40;WebService.java&#58;483&#41;
                                at weblogic.servlet.internal.ServletInitService.resume&#40;ServletInitService.java&#58;30&#41;
                                at weblogic.t3.srvr.SubsystemManager.resume&#40;SubsystemManager.java&#58;131&#41;
                                at weblogic.t3.srvr.T3Srvr.resume&#40;T3Srvr.java&#58;964&#41;
                                at weblogic.t3.srvr.T3Srvr.run&#40;T3Srvr.java&#58;359&#41;
                                at weblogic.Server.main&#40;Server.java&#58;32&#41;

                        Comment


                        • #13
                          As far as I can tell you are getting your beans loaded twice because your DispatherServlet loads the whole context and not the child context of the context loaded normally through the listener.

                          Comment


                          • #14
                            Originally posted by dejanp
                            As far as I can tell you are getting your beans loaded twice because your DispatherServlet loads the whole context and not the child context of the context loaded normally through the listener.
                            I'm fairly new to Spring. What do you suggest I change so that my beans are only loaded once?

                            Comment


                            • #15
                              Take a look at samples using dispatcher servlets. If I remember correctly you will need to have one context file to be loaded through the listener and another by the dispatcher - there's a naming convention you need to follow for that - something like xyz-servletcontext.xml or something. Docs and samples should make it clear enough though.

                              Comment

                              Working...
                              X