SpringBoot项目启动之前操作,启动之后操作

1、在Bean对象初始化之前可以做的操作

@Component
public class InitBean implements BeanDefinitionRegistryPostProcessor {
    private static final Logger logger = LoggerFactory.getLogger(InitBean.class);


    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        logger.info(" init bean1..");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        logger.info(" init bean2..");
    }
}

 

2、在Bean对象初试完之后可以做的操作

@Component
public class InitBean implements InitializingBean {
    private static final Logger logger = LoggerFactory.getLogger(InitBean.class);

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info(" init bean..");
    }
}

3、在项目启动之前可以做的操作

@Service
public class StartAddDataListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event){
        System.out.println("=========================初始化");
    }

}

4、在项目启动之后可以做的操作(项目已经启动成功)

Component
public class InitProject implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(InitProject.class);

    @Override
    public void run(String... args) {
        logger.info("==========init project===========");
    }
}

 

posted @ 2019-03-14 13:08  iviv  阅读(726)  评论(0编辑  收藏  举报