Can you talk about the life cycle of beans in the Spring framework? 1. Instantiate a Bean– new; 2. Configure the instantiated Bean according to the Spring context — that is, IOC injection; 3. If the Bean already implements the BeanNameAware interface, its setBeanName(String) method is called. 4. If the Bean already implements the BeanFactoryAware interface, The setBeanFactory it implements is called (setBeanFactory(BeanFactory) passes the Spring factory itself (you can get other beans this way by simply configuring a normal Bean in your Spring configuration file); 5. If the Bean already implements the ApplicationContextAware interface, the setApplicationContext(ApplicationContext) method is called, passing in the Spring context. But it is better than 4 because ApplicationContext is a subinterface of the BeanFactory and has more implementation methods); 6, if the Bean associated the BeanPostProcessor interface, will call postProcessBeforeInitialization (Object obj, String s) method, BeanPostProcessor is often used as a change to the Bean content, and since this is a method that calls that at the end of Bean initialization, it can also be applied to memory or caching techniques; 7. If the Bean has the init-method property configured in the Spring configuration file, it automatically calls its configured initialization method. 8, if the Bean associated the BeanPostProcessor interface, will call postProcessAfterInitialization (Object obj, String s) method,; Note: After the above work is done, we can apply the Bean, which is a Singleton, so normally we will call the Bean with the same ID on the instance with the same content address. Of course, we can also configure the non-Singleton in the Spring configuration file, so we won’t go into details here. 9. When the Bean is no longer needed, it passes through the DisposableBean stage. If the Bean implements the interface DisposableBean, its implementation destroy() method will be called; 10. Finally, if the Bean has the destroy-method property configured in its Spring configuration, its configured destruction method is automatically called. Spring usually defines beans through configuration files. Such as:

Public void init() {MSG = “HelloWorld”; date=new Date(); }… } Then, set the init-mothod property in the configuration file: 2, implement org. Springframwork. Beans. Factory. The InitializingBean interface Bean implementation InitializingBean interface, and increases the afterPropertiesSet () method: public class HelloWorld implement InitializingBean { public String msg=null; public Date date=null;

Public void afterPropertiesSet() {MSG =” Hello to the world!” ; date=new Date(); }… } afterPropertiesSet() is automatically called to initialize the Bean after all of its properties have been set by Spring’s BeanFactory, so the configuration file does not specify init-method properties. BeanWrapper HelloWorld hw=new HelloWorld(); BeanWrapper bw=new BeanWrapperImpl(hw); Bw. SetPropertyvalue (” MSG “, “HelloWorld”); System. The out. Println (bw. GetPropertyCalue (” MSG “)); BeanFactory InputStream is=new FileInputStream(” config.xml “); XmlBeanFactory factory=new XmlBeanFactory(is); The HelloWorld hw = (HelloWorld) factory) getBean (” HelloWorld “); system.out.println(hw.getMsg()); 3, use ApplicationConttext ApplicationContext actx = new FleSystemXmlApplicationContext (” config. The XML “); The HelloWorld hw = (HelloWorld) actx) getBean (” HelloWorld “); System.out.println(hw.getMsg()); Destory-method = destory-method = destory-method = destory-method = destory-method = destory-method = method Spring will then automatically invoke the specified destruction method when the bean is destroyed. 2, implement org. Springframwork. Beans. Factory. DisposebleBean interface If realized DisposebleBean interface, then Spring will automatically call of Destory bean methods are destroyed, so, The Destory method must be provided in the Bean. The illustration