Friday, December 8, 2017

Spring Framework - 12 - init()/destroy() bean/beanfactory

init() /destroy() methods for bean creations


AbstractApplicationContext to configure and control various stages of bean (creation/before destroyed).

This can be done using AbstractApplicationContext  instead of Applicationcontext while defining the first getBean in the class file as below:

AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
context.registerShutdownHook(); /

NOTE:
  • Replacing of AbstractApplicationContext class and call to registerShutdownHook is required only if we need to implement the destroy methods.
  • init() and destroy() also done using annotations, covered later


Done is 2 ways,
1) implement default interfaces and implement their methods,
This needs the main Bean class, to implement 
  • (I)InitializingBean interface which implements afterPropertiesSet() method. This interface implemenetation on the bean indicates spring as a marker to indicate to perform certain actions after the bean is created.
  • (II)DisposableBean interface which implements destroy() method. This interface implemenetation on the bean indicates spring as a marker to indicate to perform certain actions before the bean is destroyed.

2) write custom init() or destroy() methods and configure them using the respective keywords within the <bean> tag in spring.xml

<bean id="triangle" class="com.springtest.dao.Triangle" autowire="byName" init-method="<<initmethodname>>" destroy-method="<<initmethodname>>">


init and destroy methods can also be configured as a common global init/destroy method for all beans configured in the spring.xml as below:

<beans default-init-method="<<initmethodname>>" default-destroy-method="<<initmethodname>>">
        <bean .............
        </bean>
</beans>

Note:If we do both, bean specific init and destroy.. and also include default init and destroy for all beans.. the priority in for the interface implementations and then the custom methods. However, both do get executed.

________________________________________________________

BeanPostProcessor interface.. Used to implement methods that performs actions before and after a Bean has been initialised
Steps:
1)create a class that implements BeanPostProcessor and implement methods for actions before and after bean creation(these methods get executed in the order of dependent bean first).
2)Configure in spring.xml as below:
<beans>
<bean>...<bean>
<bean>...<bean>
<bean class="<<packageInclusiveClassName from step 1) >>" />
</beans>

________________________________________________________
BeanFactoryPostProcessor interface..Used to implement methods that performs actions before and after a BeanFactory has been initialised
Steps:


1)create a class that implements BeanFactoryPostProcessor and implement methods for actions before bean factory is being initialised(this methods get executed just once as it is for all the beans on a whole in the bean factory).
2)Configure in spring.xml as below:
<beans>
<bean>...<bean>
<bean>...<bean>
<bean class="<<packageInclusiveClassName from step 1) >>" />
</beans>


_______________________________________________________________________

No comments:

Post a Comment