Directory
- Objective
- Body
- 1. Custom initialization methods and destruction methods
- 2. How to use annotations
- 3.InitializingBean, Disposablebean
- 4. Implement the *aware interface
- 5.BeanPostProcessor Enhanced Processor
- Summarize
What is the life cycle of the bean in the reference link 1:spring?
Reference links 2:spring Bean life cycle
Objective
Recently bought a few books, read one of springboot
the books, see the Spring Bean life cycle This section, the book only to mention a general, feel not the result of their own, or do it yourself, although not well written, but remember it is good for yourself;
Body
On the internet to find a few pictures, feel that there is a more clear:
Now let's say a few ways, the project is built using, and the springboot
following code is done in the change project
1. Custom initialization methods and destruction methods
Create a new bean
class, note that the Bean class does not use annotations
public class CustomBean { private final static Logger logger = LoggerFactory.getLogger(CustomBean.class); public void init(){ logger.info("CustomBean-init"); } public void destroy(){ logger.info("CustomBean-destroy"); }}
The custom bean
initialization and destruction methods require the following code to be added to the configuration file:
@Configurationpublic class LifeCycleConfig { /** * 自定义bean的initMethod和destroyMethod * @return */ @Bean(initMethod = "init",destroyMethod = "destroy") CustomBean customBean(){ return new CustomBean(); }}
2. How to use annotations
The use @PostConstruct
and @PreDestroy
two annotations, which @PostConstruct
are the directivity after the execution of the constructor function, @PreDestroy
are bean
executed before the destruction;
Annotations are used on the Bean class, and of Component
course you can use one of the other three annotations.
@Componentpublic class AnnotationBean { private static final Logger logger= LoggerFactory.getLogger(AnnotationBean.class); @PostConstruct public void init(){ logger.info("AnnotationBean-init"); } @PreDestroy public void destroy(){ logger.info("AnnotationBean-destroy"); }}
3.InitializingBean, Disposablebean
can also inherit InitializingBean
, DisposableBean
interface
@Componentpublic class LifeCycleBean implements InitializingBean,DisposableBean { private static final Logger logger= LoggerFactory.getLogger(LifeCycleBean.class); @Override public void destroy() throws Exception { logger.info("LifeCycleBean-destroy"); } @Override public void afterPropertiesSet() throws Exception { logger.info("LifeCycleBean-afterPropertiesSet"); }}
4. Implement the *aware interface
As can be seen in the above figure, there are three first three Aware
interface classes, the functions of these interfaces will not have to say
@Componentpublic class LifeCycleBeanAware implements BeanNameAware,BeanFactoryAware,ApplicationContextAware { private static final Logger logger= LoggerFactory.getLogger(LifeCycleBeanAware.class); @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { logger.info("bean工厂"); } @Override public void setBeanName(String s) { logger.info("bean的默认的名字:"+s); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { logger.info("bean上下文"); }}
5.BeanPostProcessor Enhanced Processor
This class is not an interface class, there are two methods in this class postProcessBeforeInitialization
andpostProcessAfterInitialization
@Componentpublic class Lifecycleprocessor implements Beanpostprocessor {private static final L Ogger logger= Loggerfactory.getlogger (lifecycleprocessor.class); private static final String default_bean_name= "Annotationbean"; /** * Execute * @param bean before bean initialization * @param beanname * @return * @throws beansexception */@Overri De public Object Postprocessbeforeinitialization (object bean, String beanname) throws Beansexception {if (defaul T_bean_name.equals (Beanname)) {Logger.info ("execute before BEAN initialization:" +beanname); } return bean; }/** *bean executes after initialization * @param bean * @param beanname * @return * @throws beansexception */@Ov Erride public Object Postprocessafterinitialization (object bean, String beanname) throws Beansexception {if (DEF Ault_bean_name.equals (Beanname)) {Logger.info ("Execute after BEAN initialization:" +beanname); } return bean; }}
Results after startup:
2018-09-08 16:25:55.238 INFO 2584---[main] com.gyc.bean.LifeCycleProcessor:bean initialization before execution: annotation bean2018-09-08 16:25:55.238 INFO 2584---[main] Com.gyc.bean.annotationbean:annotationbean-ini t2018-09-08 16:25:55.238 INFO 2584---[main] after Com.gyc.bean.LifeCycleProcessor:bean initialization: Annotatio nbean2018-09-08 16:25:55.241 INFO 2584---[main] com.gyc.bean.lifecyclebean:lifecyclebean-aft erpropertiesset2018-09-08 16:25:55.246 INFO 2584---[main] com.gyc.bean.LifeCycleBeanAware:bean default Name: lifecyclebeanaware2018-09-08 16:25:55.246 INFO 2584---[main] com.gyc.bean.LifeCycleBeanAware: Bean Factory 2018-09-08 16:25:55.246 INFO 2584---[main] com.gyc.bean.LifeCycleBeanAware:bean context 2018-09-0 8 16:25:55.279 INFO 2584---[main] com.gyc.bean.custombean:custombean-init2018-09-08 16:25 : 55.506 INFO 2584---[ Main] o.s.j.e.a.annotationmbeanexporter:registering beans for JMX exposure on startup2018-09-08 16:25: 55.539 INFO 2584---[main] com.gyc.GycApplication:Started gycapplication in 1.922 seconds (JVM running for 3.01) 2018-09-08 16:25:55.544 INFO 2584---[Thread-6] s.c.a.annotationconfigapplicationcontext: Closing org.spring[email protected]7b227d8d:startup Date [Sat Sep 16:25:54 CST 2018]; Root of context hierarchy2018-09-08 16:25:55.548 INFO 2584---[Thread-6] o.s.j.e.a.annotationmbeanexporter : Unregistering jmx-exposed beans on shutdown2018-09-08 16:25:55.549 INFO 2584---[Thread-6] com.gyc.bean.Custom bean:custombean-destroy2018-09-08 16:25:55.549 INFO 2584---[Thread-6] Com.gyc.bean.LifeCycleB ean:lifecyclebean-destroy2018-09-08 16:25:55.549 INFO 2584---[Thread-6] Com.gyc.bean.AnnotationB Ean:annotationbean-destroy
Sample Code-github
Summarize
Comb The bean's life cycle, probably understand the spring to a bean processing process;
The top-most diagram is described in detail:
- Spring to instantiate a bean
- Spring injects values and bean references into the corresponding attributes of the bean;
- If the bean implements the Beannameaware interface, spring passes the Bean's ID to the Setbeanname method
- If the bean implements the Beanfactoryaware interface, spring calls the Setbeanfactory method, passing the Beanfactory container instance into the
- If the bean implements the Applicationcontextaware interface, the spring container calls the Setapplicationcontext method, passing in a reference to the application context where the Bean resides
- If the bean implements the Beanpostprocessor interface, spring will call their postprocessbeforeinitialization method;
- If the bean implements the Initializeingbean interface, spring will call their Afterpropertiesset method. Similarly, if the Bean uses Init-method to declare the initialization method, the method is also called;
- If the bean implements the Beanpostprocessor interface, spring will call their Postprocessafterinitialization method
- The bean is called by the application until the application context is destroyed;
- If the bean implements the Disposablebean interface, Spring will call his destroy method. If the Bean declares the destroy method using Destroy-method, the method is also called
Spring Bean life cycle