zoukankan      html  css  js  c++  java
  • Spring Bean的生命周期学习

    1、Spring Bean的整个生命周期图:

     

    2、@PostConstructor@PreDestroy注解:

    1 @PostConstruct
    2 public void afterConstructor() {
    3     System.out.println("after constructor..............");
    4 }
    5 
    6 @PreDestroy
    7 public void beforeDestroy() {
    8     System.out.println("before destroy..............");
    9 }

    3、InitializingBean, DisposableBean 接口:

     1 public class SpringLifeCycleService implements InitializingBean,DisposableBean{
     2     
     3     @Override
     4     public void afterPropertiesSet() throws Exception {
     5         System.out.println("SpringLifeCycleService start");
     6     }
     7 
     8     @Override
     9     public void destroy() throws Exception {
    10         System.out.println("SpringLifeCycleService destroy");
    11     }
    12 }

    4、指定init-method方法:

     1 public class SpringLifeCycleService implements InitializingBean,DisposableBean{
     2     
     3     @Override
     4     public void start() {
     5         System.out.println("starting..........");
     6     }
     7 
     8     @Override
     9     public void stop() {
    10         System.out.println("stopping...............");
    11     }
    12 }

    并且在SpringMVC配置文件中添加:

    <bean class="*.*.SpringLifeCycleService" init-method="start" destroy-method="stop"></bean>

    5、BeanPostProcessor 接口:

     1 public class MyTestImpl implements BeanPostProcessor {
     2 
     3     @Override
     4     public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
     5         System.out.println("BeanPostProcessor before init");
     6         return o;
     7     }
     8 
     9     @Override
    10     public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
    11         System.out.println("BeanPostProcessor after init");
    12         return o;
    13     }
    14 }
  • 相关阅读:
    RabbitMq环境搭建
    Springboot集成quartz
    java8时间工具类
    AngularJS学习笔记之directive——scope选项与绑定策略
    理解$watch ,$apply 和 $digest --- 理解数据绑定过程
    AngularJS中service,factory,provider的区别
    AngularJS的Filter用法详解
    Angular.js中使用$watch监听模型变化
    history
    data-*
  • 原文地址:https://www.cnblogs.com/laoxia/p/11681130.html
Copyright © 2011-2022 走看看