Announcement Announcement Module
Collapse
No announcement yet.
Prototype Beans not creating as they should be. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      How it can be done???

      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.

      Comment


      • #4
        You would have to change the service factory, or use aop to override your own implementation. I suggest the reference guide.

        Comment


        • #5
          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

          Comment


          • #6
            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, 01:56 AM. Reason: small correction

            Comment

            Working...
            X