接rocketMQ的consumerMessageHook时遇到了一个生命周期位置的问题

这里写了一个小demo验证下生命周期


打印顺序

  1. LifeCycleTest 实例化

  2. LifeCycleTest setName:lifeCycleTest

  3. LifeCycleTest setApplicationContext

  4. LifeCycleTest afterPropertiesSet

  5. LifeCycleTest afterSingletonsInstantiated

  6. LifeCycleTest isRunning

  7. LifeCycleTest start

停止服务时

  1. LifeCycleTest isRunning

  2. LifeCycleTest stop

  3. LifeCycleTest destroy



@Component
public class LifeCycleTest implements SmartInitializingSingleton, SmartLifecycle, InitializingBean, DisposableBean,
        BeanNameAware, ApplicationContextAware {
    boolean isRunning = false;
    public LifeCycleTest() {
        System.out.println("LifeCycleTest 实例化");
    }
    @Override
    public void setBeanName(String s) {
        System.out.println("LifeCycleTest setName:" + s);
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("LifeCycleTest destroy");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("LifeCycleTest afterPropertiesSet");
    }
    @Override
    public void afterSingletonsInstantiated() {
        System.out.println("LifeCycleTest afterSingletonsInstantiated");
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("LifeCycleTest setApplicationContext");
    }
    @Override
    public void start() {
        isRunning = true;
        System.out.println("LifeCycleTest start");
    }
    @Override
    public void stop() {
        System.out.println("LifeCycleTest stop");
    }
    @Override
    public boolean isRunning() {
        System.out.println("LifeCycleTest isRunning");
        return isRunning;
    }
}



乐享:知识积累,快乐无限。