wood burning stoves 2.0*
The moose likes Spring and the fly likes Bean getting initialised everytime, why ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Bean getting initialised everytime, why ?" Watch "Bean getting initialised everytime, why ?" New topic

Bean getting initialised everytime, why ?

Rahul Shivsharan
Ranch Hand

Joined: Jun 17, 2009
Posts: 83

Emplyee interface

PermanentEmpl.java ,







now when i run MainPractise15.java, the commandline output is

now if i add attribute as follows

and if i run the main class MainPractise15.java, the output is

Now my question is why ManagerSer is initialized 3 times in first scenario and 2 times in second scenario ?
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249

You have three beans of that type. You are also using Bean Definition inheritence in your configuration. In the first example, the parent <bean> tag get a bean instantiated as well as the other two. In your second version for the parent bean you used the attribute abstract="true", which tells Spring not to instantiate that bean, but just use the bean configuration for the other two. This is the same as creating a parent Java class as abstract or not.

public abstract class Car

public class Honda extends Car

public class Toyota extends Car

in that hierarchy I can only instantiate Honda and Toyota, not Car because it is abstract. It works the same way with <bean> tags if you have <bean abstract="true"> then Spring can't instantiate that bean.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Bean getting initialised everytime, why ?
Similar Threads
Not able to understant object initialisation
unable to call AOP method
Some help required for understanding spring lifecycle
Injecting the properties of object through Spring which is created outside the Spring container
No SessionFactory specified(IllegalArgumentException)