What are singleton beans and how can you create prototype beans?

What are singleton beans and how can you create prototype beans?

The singleton property of <bean> tells the context whether or not a bean is to be defined as a singleton. 
This attribute in bean tag named \"singleton\" if specified true then bean becomes singleton and if set to false then the bean becomes a prototype bean. By default it is set to true.

So, all the beans in spring framework are by default singleton beans.
    <beans>
       <bean id=\"bar\" class=\"com.act.Foo\" singleton=\"false\"/>
   </beans> 

Prototyped beans are useful when you want the container to give a unique instance of a bean each time it is asked for, but you still want to configure one or more properties  of that bean through Spring.
  <bean id=\"student\"  class=\"com.StudentImpl\"
    singleton=\"false\">

A new instance of a prototype bean will be created each time getBean() is invoked with the bean\'s name.

By:Jalees
Date:2031-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher