Spring Lifecycle Callbacks

官方文档:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle

三种方式(如果三种方式同时修饰一个方法,则按照如下顺序):

  1. Methods annotated with @PostConstruct
  2. afterPropertiesSet() as defined by the InitializingBean callback interface
  3. A custom configured init() method
    测试代码:
  • 通过InitializingBean
@Service
public class EatServiceImpl implements EatService, InitializingBean {
    public void afterPropertiesSet() {
        System.out.println("afterPropertiesSet");
    }
}
  • 通过@PostConstruct
@Service
public class SleepServiceImpl implements SleepService {

    @PostConstruct
    public void postFunction(){
        System.out.println("PostConstruct....");
    }
}

  • 通过自定义方法
public class DrinkServiceImpl implements DrinkService {
    public void init(){
        System.out.println("drink init...");
    }
}
@Configuration
@ComponentScan("com.good.demo3") // 为了引入AnimalService
public class AppConfig3 {

    @Bean(initMethod = "init")
    public DrinkServiceImpl eatService(){
        return new DrinkServiceImpl();
    }
	
	// 这个是为了下面三个同事修饰
    @Bean(initMethod = "init")
    public ActionServiceImpl actionService(){
        return new ActionServiceImpl();
    }

    @Bean(initMethod = "afterPropertiesSet")
    public ShitServiceImpl shitService(){
        return new ShitServiceImpl();
    }
}

  • 三种同时定义给同一个bean,而且用了不同的方法
public class ActionServiceImpl implements ActionService, InitializingBean {

    @PostConstruct
    public void postFunction(){
        System.out.println("Action PostConstruct....");
    }

    public void afterPropertiesSet() {
        System.out.println("Action afterPropertiesSet");
    }

    public void init(){
        System.out.println("Action init...");
    }
}

三种方式同时定义一个bean,而且在一个方法上

public class ShitServiceImpl implements ShitService, InitializingBean {
    @PostConstruct
    public void afterPropertiesSet() {
        System.out.println("Shit afterPropertiesSet...");
    }
}

测试方法:

public class TestDemo3 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(AppConfig3.class);
        context.getBean(EatService.class);
        context.getBean(DrinkService.class);
        context.getBean(SleepService.class);
        context.getBean(ActionService.class);
    }
}

测试结果:

afterPropertiesSet...
PostConstruct....
drink init...
Action PostConstruct....
Action afterPropertiesSet...
Action init...
Shit afterPropertiesSet...
  • 总结下上面三个生命周期回调方法init-methodInitializingBean接口、@PostConstruct注解
    1. 都是针对单个类的实例化后处理
    2. 执行时间都是在类实例化完成,且成员变量完成注入之后调用的
    3. 对于init-method,还可以在Spring配置文件的beans元素下配置默认初始化方法,配置项为default-init-method
    4. 若以上三种方式配置的初始化方法都不一样,则执行顺序为:@PostConstruct注解方法 –> InitializingBean的afterPropertiesSet –> init-method方法 ;若三种方式配置的方法一样,则方法只执行一次 (参照:Spring官方文档beans-factory-lifecycle-combined-effect)
    5. 有初始化回调方法,对应的也有销毁的回调方法。@PostConstruct注解方法 –> InitializingBean的afterPropertiesSet –> init-method方法 分别对应 @PreDestroy注解方法 –> DisposableBean的destroy –> destroy-method方法

参考:https://blog.csdn.net/liuxigiant/article/details/54296578

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章