Results 1 to 6 of 6

Thread: Prototype Beans not creating as they should be.

  1. #1
    Join Date
    Sep 2009
    Posts
    12

    Question Prototype Beans not creating as they should be.

    Dear friends,

    Why is my bean not creating new instantace evey time in this case.

    Code:

    Code:
    <bean id="serviceFactory" class="helpers.ServiceFactoryImpl"   autowire="byType" autowire-candidate="true">
        		<property name="_userService" ref="userService" ></property>
    </bean>
    
    
    <bean id="userService" class="cursive.company.service.UserService" scope="prototype"  autowire="byType" autowire-candidate="true">
    			
    </bean>
    This is where I want new bean in every place.


    Code:

    Code:
    public String signin() 
    {
        	try 
            {
    		userService = serviceFactory.get_userService() ;
    	
              	userService1 = serviceFactory.get_userService();
    	  
                    // UserService & UserService1 are same, I want them different objects everytime
    	} 
            catch (Exception e) 
            {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    	}
        	return ERROR;
    }
    Please help me doing this.

    I cannot remove service factory to instantiate it as it is part of legacy application. But want o use spring also.

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

    Default

    I suggest the reference guide...

    A prototype bean is created as soon as getBean is called on the applpicationcontext so your ServiceFactory gets an instance injected and that instance is used through out the application. If you want a new instance each time let your service factory call getBean on the application context EACH TIME you call get_userService.
    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
    Sep 2009
    Posts
    12

    Question How it can be done???

    Quote Originally Posted by Marten Deinum View Post
    I suggest the reference guide...

    If you want a new instance each time let your service factory call getBean on the application context EACH TIME you call get_userService.
    Interesting to know more on it, how can my service factory call context each time. ????

    Is there any way to do it using xml or will I have to change the service factory itself.

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

    Default

    You would have to change the service factory, or use aop to override your own implementation. I suggest the reference guide.
    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

  5. #5

    Default Re:

    Reference the document section 3.4.1. The singleton scope - where the problem is detailed and solution in the section
    3.3.7.1. Lookup method injection - link http://static.springsource.org/sprin...thod-injection

  6. #6
    Join Date
    Sep 2009
    Posts
    12

    Thumbs up Its working

    I have used the look-up part and its working for now..............

    But I am greedy and looking forward to use Service Locator Factory Bean and other parts. As currently I have to remove all constructors to use it as it is giving me circular dependencies error.

    So more than 500 classes first i have to remove constructor instantiation then will look in more details.

    If I come across any issues surely will get back to this forum.

    Thank you for your help and support.
    Last edited by cursivetech; Apr 26th, 2010 at 01:56 AM. Reason: small correction

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
  •