A Spring Bean represents a POJO component performing some useful operation. All Spring Beans reside within a Spring Container also known as IOC Container. The Spring Framework is transparent and thereby hides most of the complex infrastructure and the communication that happens between the Spring Container and the Spring Beans. This section lists the sequence of activities that will take place between the time of Bean Instantiation and hand over of the Bean reference to the Client Application.
- The Bean Container finds the definition of the Spring Bean in the Configuration file.
- The Bean Container creates an instance of the Bean using Java Reflection API.
- If any properties are mentioned, then they are also applied. If the property itself is a Bean, then it is resolved and set.
If the Bean class implements
- the
BeanNameAwareinterface, then the method setBeanName() will be called by passing the name of the Bean. - the
BeanClassLoaderAwareinterface, then the methodsetBeanClassLoader()will be called by passing an instance of theClassLoaderobject that loaded this bean. - the
BeanFactoryAwareinterface, then the methodsetBeanFactory()will be called by passing an instance ofBeanFactoryobject.
- If there are any
BeanPostProcessorsobject associated with theBeanFactorythat loaded the Bean, then
- The method
postProcessBeforeInitialization()will be called even before the properties for the Bean are set.
- If the Bean class implements the
InitializingBeaninterface, then the methodafterPropertiesSet()will be called once all the Bean properties defined in the Configuration file are set. - If the Bean definition in the Configuration file contains a
'init-method'attribute, then the corresponding method definition in the Bean class will be called.
- The
postProcessAfterInitialization()method will be called if there are any Bean Post Processors attached for the Bean Factory object.
- If the Bean class implements the
DisposableBeaninterface, then the methoddestroy()will be called when the Application no longer needs the bean reference. - 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