Preface
The previous article mainly learned about the configuration, injection, and custom property editor of beans. Today, let’s get familiar with the life cycle of beans.
Any thing has its own life cycle, the beginning, the end of life. What you are most familiar with is the life cycle of servlets. Like servlets, spring beans also have their own life cycle.
Lifecycle is a very common noun in development, and basically every programming language can find it associated with it. I have also looked for a lot of bean life cycles online, and they are basically the same. Here I mainly want to verify through code. After all, the knowledge I learned is the same, I learned Java, and the most important thing is to practice hands-on, so that I can remember it more.
Below is a description of its life cycle, which we verify through demo.
The following figure shows the order in which it is executed.
1. Create LiftCycle class to implement 5 interface methods: BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean, ApplicationContextAware
package Cuiyw.Spring.Service;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.beans.factory.BeanNameAware;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class LifeCycle implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware{ private String name; public String getName() { System.out.println("getName name="+name); return name; } public void setName(String name) { System.out.println("setName name="+name); this.name = name; } public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println("InitializingBean.afterPropertiesSet()"); } public void setBeanName(String arg0) { // TODO Auto-generated method stub System.out.println("BeanNameAware.setBeanName"); } public void setBeanFactory(BeanFactory arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("BeanFactoryAware.setBeanFactory"); } public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println("DisposableBean.destroy"); } public void myInit() { System.out.println("【init-method】calling the initialization method specified by the init-method property of <bean>"); } public void myDestory() { System.out.println("【destroy-method】calling the initialization method specified by the destroy-method property of <bean>"); } public void setApplicationContext(ApplicationContext arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("ApplicationContextAware.setApplicationContext"); } }2. Register the InstantiationAwareBeanPostProcessor interface
package Cuiyw.Spring.Service;import java.beans.PropertyDescriptor;import org.springframework.beans.BeansException;import org.springframework.beans.PropertyValues;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor{ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInitialization"); return bean; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization"); return bean; } public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation"); return true; } public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation"); return null; } public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues"); return pvs; }}3. Register the BeanPostProcessor interface
In fact, InstantiationAwareBeanPostProcessor inherits BeanPostProcessor, so I also implemented the method of the BeanPostProcessor interface above.
package Cuiyw.Spring.Service;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("BeanPostProcessor.postProcessAfterInitialization"); return bean; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println("BeanPostProcessor.postProcessBeforeInitialization"); return bean; }}4. Register the BeanFactoryPostProcessor interface
package Cuiyw.Spring.Service;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanFactoryPostProcessor;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("BeanFactoryPostProcessor.postProcessBeanFactory"); }}5. Configure in the context
I will modify it based on the demo of the previous blog. In order to have other interference, I will remove the service first.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="beanPostProcessor"></bean><bean id="instantiationAwareBeanPostProcessor"></bean><bean id="beanFactoryPostProcessor"></bean><bean id="lifeCycle" init-method="myInit" destroy-method="myDestory"><property name="name" value="cuiyw1"></property></bean></beans>
6. Use beans in main
package Cuiyw.SpringAop;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import Cuiyw.Spring.IService.IService;import Cuiyw.Spring.Service.LifeCycle;public class App { public static void main( String[] args ) { ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"}); BeanFactory factory=context; LifeCycle lifeCycle=factory.getBean("lifeCycle",LifeCycle.class); lifeCycle.setName("cuiyw2"); System.out.println("lifeCycle.name="+lifeCycle.getName()); ((ClassPathXmlApplicationContext)factory).registerShutdownHook(); /*service=(IService)factory.getBean("ServiceA"); service.service("Cuiyw ServiceA"); service=(IService)factory.getBean("ServiceImpl"); service.service("Cuiyw ServiceImpl"); */ }}7. Enter the print result
It can be found that the order of output is basically the same as the order of the figure above.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.