How to Inject OneScope bean into another ( PrototypeBean into SingletonBean injection )





spring   injection   of one bean in to other is known as  autowiring .   

Autowiring   of   the   beans are   five    types 





        1.   no 

        2.  bytheName 

        3.  bytheType

         4. constructor

         5.autodetect
                      

 you  can inject  bean    using   @Autowire   annotation  or  using   bean configuration  in Context  file . 



       Here  is  general  example of the   injection 





      <bean id="a" class="A">

    <property name="b" ref="b"/>
    </bean>

<bean id="b" class="B" scope="session"/> or

 <bean id="b" class="B" scope="prototype"/>

  So here   by  default  bean   (a ) is   singleton  scope  and  we are   injecting 
   (b)  bean  with scope  of  session  or prototype . 



       We   inject   singleton   into  prototype  using   annotation . 

     Here   is   example 


  Bean with scope  singleton 

 @Scope(value = "singleton")
 @Component
 public class Name {
private String firstName = " mak";
private String lastName = " com";

       getter and setter
 }

 Bean  with  scope  prototype




@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS))
@Component
public class Language {
private String language = "English";
   getter  and  setter 
 }


  Bean  injections

@Component

public class Customer {



@Autowired
private Language language;
 @Autowired
private Name name;


  }


 Again  Customer  default  scope  is  singleton  and  we  are  injecting  two beans 
  with scope singleton and prototype.







How to Inject OneScope bean into another ( PrototypeBean into SingletonBean injection ) How to  Inject   OneScope  bean  into  another ( PrototypeBean into SingletonBean injection ) Reviewed by Mukesh Jha on 8:38 PM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.