IOC’s Bean Lifecycle Experiment 22: Create a Bean with a lifecycle method
public class Person { private Integer id; private String name; Public void init() {system.out.println (" This is the initialization method of the person object "); } public void destroy() {system.out.println (" this is the person object destroy method "); }Copy the code
Configuration information:
<! <bean id="p24" class=" com.pojo.person "class=" com.pojo.person" init-method="init" destroy-method="destroy"> <property name="id" value="24"></property> </bean>Copy the code
Test code:
@Test
public void test13() throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(applicationContext.getBean("p24"));
applicationContext.close();
}
Copy the code
BeanPostProcessor The BeanPostProcessor Bean, which can do some work before or after the Bean object is initialized. To use the bean’s back-end processor, you need to implement and configure this interface.
Experiment 23: Test the bean’s post-processor
The Person object must have an initialization method
public class Person { private Integer id; private String name; private Car car; Public void init() {system.out.println (" This is the initialization method of the person object "); }Copy the code
The post-processor object
public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String ID) throws BeansException {system.out.println (" Execute bean after initialization => "+ bean + ", id =>" + ID); return bean; * * *} / bean is currently being initialized Object * id is the id of the current Object is initialized values * / @ Override public Object postProcessBeforeInitialization (Object bean, String ID) throws BeansException {system.out.println (" Execute bean before initialization => "+ bean + ", ID =>" + ID); return bean; }}Copy the code
Configuration information:
<! <bean id="p24" class=" com.pojo.person "class=" com.pojo.person" init-method="init" destroy-method="destroy"> <property name="id" value="24"></property> </bean> <! - configure beans rear processor - > < bean class = "com. Pojo. MyBeanPostProcessor" / >Copy the code
Test code:
@Test
public void test13() throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean2.xml");
System.out.println(applicationContext.getBean("p24"));
applicationContext.close();
}
Copy the code
For singleton beans, the life cycle has 11 steps: 1. Instantiate bean objects, which are instantiated when the configuration file is loaded. That is, when we start the Spring container, we load the configuration file and instantiate the bean. If the Bean implements BeanNameAware, go to setBeanName 4. If the Bean implements BeanFactoryAware or ApplicationContextAware, set the factory setBeanFactory or context object setApplicationContext 5. If there is a class implements BeanPostProcessor (Bean) post-processing, perform postProcessBeforeInitialization (this point is often used to enhance the Bean) 6. Perform afterPropertiesSet 7 if the Bean implements InitializingBean. Call specifying initialization method init 8. If there is a class implements BeanPostProcessor (Bean) post-processing, perform postProcessAfterInitialization (this point is often used to enhance the Bean) 9. 10. If the Bean implements DisposableBean, execute Destroy 11. Calls the specified destruction method