Tuesday, December 25, 2012


A bean’s life (Spring Bean Life Cycle):


  1. Spring instantiates the bean.
  2. Spring injects values and bean references into the bean’s properties.
  3. If the bean implements BeanNameAware, Spring passes the bean’s ID to the  set-BeanName() method.
  4. If the bean implements BeanFactoryAware, Spring calls the setBeanFactory() method, passing in the bean factory itself.
  5. If the bean implements ApplicationContextAware, Spring will call the  set-ApplicationContext() method, passing in a reference to the enclosing  application context.
  6. If any of the beans implement the BeanPostProcessor interface, Spring calls their postProcessBeforeInitialization() method.
  7. If any beans implement the InitializingBean interface, Spring calls their afterPropertiesSet() method. Similarly, if the bean was declared with an init-method, then the specified initialization method will be called.
  8. If there are any beans that implement BeanPostProcessor, Spring will call their postProcessAfterInitialization() method.
  9. At this point, the bean is ready to be used by the application and will remain in the application context until the application context is destroyed.
  10. If any beans implement the DisposableBean interface, then Spring will call their destroy() methods. Likewise, if any bean was declared with a destroy-method, then the specified method will be called.

Ref:  Spring in Action By Craig Walls

Tuesday, December 18, 2012

Various ways to create object in java

1. Using new keyword
MyObject object = new MyObject();

2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object = (MyObject) Class.forName("package.MyClass").newInstance();

 3. Using clone()
The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject(); 
MyObject object = anotherObject.clone();

4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream ); 
MyObject object = (MyObject) inStream.readObject();

5. Using classloader
this.getClass().getClassLoader().loadClass(“package.MyClass”).newInstance();