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

  • Beans created twice

    Hi Everyone,

    i am getting crazy with following Problem and i struggle with this for 2 days now. I hope anyone can help me.

    I created a WebApplication based on following web.xml (i just changed some names)
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			/WEB-INF/context/a.xml,
    			/WEB-INF/context/b.xml, 			
    			/WEB-INF/context/c.xml,			
    			/WEB-INF/context/d.xml		
    		</param-value>
    	</context-param>
    
    	<context-param>
    		<param-name>contextClass</param-name>
    		<param-value>org.springframework.web.context.support.XmlWebApplicationContext
    </param-value>
    	</context-param>
    	
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	
    	<servlet>
    		<servlet-name>servicelayer</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>servicelayer</servlet-name>
    		<url-pattern>/services/*</url-pattern>
    	</servlet-mapping>
    
    </web-app>
    I run with this configuration for quite some time now, and i noticed that it seems that all my beans are created twice.Thats not problem for stateless beans, but i had to add a cache to a service bean and so, i just noticed the problem.

    Whats wrong here and how can i set up the web.xml to load the Beans only once?

    thank you in advance
    Kai

  • #2
    And why do you think they are created twice?

    My guess, you are using classproxies and thus use cglib and haev logging in your constructor. This setup makes it that the constructor is called twice, once unproxied and second proxied. This MIGHT make it look like you have 2 instances.

    If not my guess is that you are importing files into your servicelayer-servlet.xml file or one of your a,b,c,d xml files is actually the servicelayer-servlet.xml file... (Changing names to something else doesn't really help here because you might have obscured the actual problem).
    Last edited by Marten Deinum; Aug 16th, 2010, 04:32 AM.

    Comment


    • #3
      I added a breakpoint into an init method of a bean and noticed that it was called twice when the web application starts (i am using a BEA 10.3 Application Server)

      And one of the xml files is my servicelayer-service.xml file. The other xml files define some beans for quartz scheduling etc.
      Code:
      /WEB-INF/servicelayer-servlet.xml, 
      /WEB-INF/context/quartz-schedule-context.xml,
      /WEB-INF/context/jms.xml, 
      /WEB-INF/context/persistence_dao.xml,
      /WEB-INF/context/web-service-client.xml

      I am not using cglib.
      Last edited by kajo; Aug 16th, 2010, 07:26 AM.

      Comment


      • #4
        And hence your beans are loaded twice. Your servlet.xml shouldn't/mustn't be loaded by the contextloaderlistener. That is already being loaded by the DispatcherServlet. I suggest a read of the web chapter of the reference guide which explains that.

        Comment

        Working...
        X