## Notes Steps for creation and destruction of a bean when managed by a Spring container. ## Phases 1. Spring IoC Container is started 2. Container creates an instance of the bean 3. Container injects dependencies into the bean 4. If bean implements `InitializingBean` interface or custom initialization method specified by `@PostConstruct` 5. Bean is ready to be used 6. Custom destruction method, either by implementing `@DisposableBean` or `@PreDestroy` annotation or other `destroy` method ## Implement Bean Lifecycle There are 3 ways ### XML Configuration First, define the bean ```java package beans; public class HelloWorld { public void init() throws Exception { Syso("Bean hello world"); } public void destroy() throws Exception { Syso("Container closed - bean destroyed"); } } ``` Then define XML configuration ```xml <!-- spring.xml --> <beans xmlns="http://www.springframework.org/schema/beans//////" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans////// http://www.springframework.org/schema/beans///////spring-beans.xsd"> <bean id="hw" class="beans.HelloWorld" init-method="init" destroy-method="destroy" /> </beans> ``` Driver class ```java package test; import beans.HelloWorld; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Driver { public static void main(String[] args) throws Exception { ConfigurableApplicationContext cap = new ClassPathXmlApplicationContext("resources/spring.xml"); cap.close(); } } ``` ### Programmatic - Interface Implement the `InitializingBean` and `DisposableBean` interfaces and override their methods: `afterPropertiesSet` and `destroy`. ```java package beans; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class HelloWorld implements InitializingBean, DisposableBean { @Override public void afterPropertiesSet() throws Exception { Syso("Bean has been instantiated"); } @Override public void destroy() throws Exception { Syso("Bean has been destroyed"); } } ``` Configure XML file ```xml <!-- spring.xml --> <beans ...> <bean id="hw" class="beans.HelloWorld" /> </beans> ``` Driver class ```java package test; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import beans; public class Driver { public static void main(String[] args) throws Exception { ConfigurableApplicationContext cap = new ClassPathXmlApplicationContext("resources/spring.xml"); cap.close(); } } ``` ### Annotations The bean needs to use the `@PostConstruct` and `@PreDestroy` annotations. ```java package beans; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class HelloWorld { @PostConstruct public void init() throws Exception { Syso("Bean init"); } @PreDestroy public void destroy() throws Exception { Syso("Bean destroy"); } } ``` ```xml <!-- spring.xml --> <beans ...> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="hw" class="beans.HelloWorld" /> </beans> ``` ```java package test; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import beans; public class Driver { public static void main(String[] args) throws Exception { ConfigurableApplicationContext cap = new ClassPathXmlApplicationContext("resources/spring.xml"); cap.close(); } } ```