In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "Spring bean life cycle knowledge point explanation", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "Spring bean life cycle knowledge point explanation" bar!
Preface
Everything has its own life cycle, the beginning of life, the end of life. What you are most familiar with is the life cycle of servlet. Like servlet, spring bean has its own life cycle.
Life cycle is a common term in development, and it can be found in almost every programming language. I have also found a lot about the life cycle of bean on the Internet, almost all of them are similar. Here I mainly want to verify through the code, after all, the knowledge learned is the same, are learning Java, the most important thing is hands-on practice, so more impressive.
The following is a description of its life cycle, which we validate through demo.
The following figure shows the order in which it is executed.
First, create a LiftCycle class to implement BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean,ApplicationContextAware5 interface methods
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 ("initialization method specified by init-method property called by [init-method]");} public void myDestory () {System.out.println ("initialization method specified by destroy-method property called by [destroy-method]") } public void setApplicationContext (ApplicationContext arg0) throws BeansException {/ / TODO Auto-generated method stub System.out.println ("ApplicationContextAware.setApplicationContext");}}
Second, 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;}}
Third, register the BeanPostProcessor interface
In fact, InstantiationAwareBeanPostProcessor inherits BeanPostProcessor, so I also implemented the method of 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 context
Here is still on the basis of the previous blog demo to modify, in order to have other interference, I first removed the service.
6. Use bean 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
You can find that the order of the output is basically the same as that of the figure above.
Thank you for your reading, the above is the content of "Spring bean Lifecycle knowledge Point explanation". After the study of this article, I believe you have a deeper understanding of Spring bean Lifecycle knowledge Point explanation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.