Results 1 to 4 of 4

Thread: Beans created twice

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    4

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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 at 04:32 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2010
    Posts
    4

    Default

    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 at 07:26 AM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •