Monday, 13 February 2017

Spring Bean LifeCycle

Before looking into Bean Life Cycle let us try to understand IOC-:

A brief introduction to IOC-:

"IOC is a design patter which is used to remove dependency from your code."

For Example-:

Say you application has a text editor component and you want to provide spell checking your standard code will look like this.

public class TextEditor{
        private SpellChecker spellChecker;
        public TextEditor(){
            this.checker = new SpellChecker();
   }
}

What we have done here is create a dependency between the TextEditor and the SpellChecker. In a IOC Scenario we would do something like this

 public class TextEditor{
        private ISpellChecker checker;
        public TextEditor(ISpellChecker checker){
            this.checker = checker;
   }
}

In the first code snippet we are instantiating SpellChecker(this.checker = new SpellChecker();)
which means that TextEditor class directly depends on SpellChecker Class.

In the second one we are creating an abstraction by having the TextEditor constructor signature as the SpellChecker dependency class (not initializing dependency in class) This allow us to call the dependency then pass it to the TextEditor class like so

SpellChecker sc = new SpellChecker(); //dependency
TextEditor text = new TextEditor(sc);

Now the client creating the TextEditor class has the control over which SpellChecker implementation to use. We are injecting the TextEditor with dependency.

===============================================Bean Life Cycle-:

Spring Beans are managed by IOC Containers. Spring bean exist within the container as long as they are needed by the application. There are various life-cycle interfaces and methods that will be called by the IoC Container.

A Spring bean represent a POJO component performing some useful operation. 

List of activity that takes place between the time of Bean Instantiation and hand over of the bean reference to the client application-:

  1. The bean container finds the definition of the Spring Bean in the configuration file.
  2. The bean container creates an instance of the Bean using java reflection API.
  3. After instance creation dependency will be injected(DI)
     "If Bean class implements any of the below highlighted interface then corresponding method will be invoked in below order point"
  1. If the bean class implements the BeanNameAware interface then the setBeanName() method will be called by passing the name of the bean.
  2. If the bean class implements the BeanClassLoaderAware interface then the method setBeanClassLoader() method will be called by passing an instance of the ClassLoader object that loaded this bean.
  3. If the bean class implements the BeanfactoryAware interface then the method setBeanfactory() will be called by passing and instance of a BeanFactory object.
  4. If there are any BeanPostProcessors object associated with the BeanFactory that loaded the bean then the method postProcessBeforeInitialization() will be called even before properties of that bean are called.
  5. If the bean class implements InitializingBean interface then the method afterPropertiesSet() will be called once all the bean defined in the properties files has been set.
  6. If the bean definition in the configuration file contains 'init-method' attribute then the values for the attribute will be resolved to a method name in the bean class and that method will be called.
  7. The postProccessAfterInitilization() method will be called if there are any Bean Post Processors attached for the bean factory object.
  8. If the bean class implements the DisposableBean interface then the method destroy()will be called when the application no longer needs the bean reference.
  9. If the bean definition in the configuration file contains a 'destroy-method' attribute then the corresponding method definition in the bean class will be called.

No comments:

Post a Comment