1、启动中执行方法
1.1、方法上加注解@PostConstruct
1.2、xml配置init-method
1.3、实现InitializingBean接口,重写afterPropertiesSet方法
2、启动完成之后执行方法
@Service
public class StartupListner implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getApplicationContext().getParent() == null) {//保证只执行一次
//需要执行的方法
}
}
}
参考链接:
https://blog.csdn.net/u010742049/article/details/82686059
https://blog.csdn.net/jyxmust/article/details/80407798