SpringBoot 初始化资源

打印 上一主题 下一主题

主题 917|帖子 917|积分 2751

1 、使用接口ApplicationRunner和CommandLineRunner

这两个接口都是在容器运行后执行的,如下图示

 如果项目需要在体系启动时,初始化资源,可以继续这两个接口,实现诸如缓存预热、DB毗连等。
实现ApplicationRunner接口
  1. @Component
  2. public class MyApplicationRunner implements ApplicationRunner {
  3.     @Override
  4.     public void run(ApplicationArguments args) throws Exception {
  5.         System.out.println("缓存预热~");
  6.         System.out.println("DB连接~");
  7.         System.out.println("日志系统初始化~");
  8.         System.out.println("资源文件初始化~");
  9.         System.out.println("常驻后台线程的资源初始化~");
  10.     }
  11. }
复制代码
View Code实现CommandLineRunner接口
  1. @Component
  2. public class MyCommandLineRunner implements CommandLineRunner {
  3.     @Override
  4.     public void run(String... args) throws Exception {
  5.         System.out.println("缓存预热~");
  6.         System.out.println("DB连接~");
  7.         System.out.println("日志系统初始化~");
  8.         System.out.println("资源文件初始化~");
  9.         System.out.println("常驻后台线程的资源初始化~");
  10.     }
  11. }
复制代码
View Code2、监听spring变乱ApplicationListener

ApplicationContext变乱的实现机制是观察者模式,通过两个接口ApplicationEvent和ApplicationListener来实现。
spring容器在初始化时,会在不同的阶段,发布ApplicationEvent变乱,因此,可以通过ApplicationListener监听各个阶段的变乱来实现不同的功能。
spring提供了以下ApplicationEvent变乱:

  • ContextStartedEvent 上下文启动变乱,此 Start 变乱是当Spring容器启动时发布,即调用 start() 方法是执行,意味着所有Lifecyc Bean 都监听到了 start 变乱。
  • ContextRefreshedEvent 上下文刷新变乱 此 Refreshed 变乱是当容器实例化时发布,即执行 refresh() 方法,此时所有的 Bean 都已加载,后置处置惩罚器被激活,容器中所有的对象就可以使用,如果容器支持热重载,则 refresh 可以被触发多次(XmlWebApplicatonContext支持热刷新,而GenericApplicationContext则不支持)。
  • ContextStoppedEvent  上下文停止变乱 当使用 ConfigurableApplicationContext 接口中的 stop() 停止 ApplicationContext 时,发布这个变乱。你可以在担当到这个变乱后做须要的清理的工作。
  • ContextClosedEvent 上下文关闭变乱 当使用 ConfigurableApplicationContext 接口中的 close() 方法关闭 ApplicationContext 时,该变乱被发布。一个已关闭的上下文到达生命周期末端;它不能被刷新或重启。
  • RequestHandledEvent 请求处置惩罚完成变乱 此 Request handled 变乱只在使用spring的DispatcherServlet时有用,当一个请求被处置惩罚完成时发布。
基于以上变乱范例,我们可以监听ContextRefreshedEvent变乱,初始化体系资源,以及监听ContextStoppedEvent变乱来清理资源。
初始化资源:
  1. @Component
  2. public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
  3.     @Override
  4.     public void onApplicationEvent(ContextRefreshedEvent event) {
  5.         System.out.println("做一些初始化的工作,比如缓存预热~");
  6.         ApplicationContext ct= event.getApplicationContext();
  7.         System.out.println(ct);
  8.     }
  9. }
复制代码
View Code清理资源:
  1. @Component
  2. public class MyApplicationStopListener implements ApplicationListener<ContextStoppedEvent> {
  3.     @Override
  4.     public void onApplicationEvent(ContextStoppedEvent event) {
  5.         System.out.println("做一些清理资源的工作,比如缓存清理,DB连接清理~");
  6.         ApplicationContext ct= event.getApplicationContext();
  7.         System.out.println(ct);
  8.     }
  9.     @Override
  10.     public boolean supportsAsyncExecution() {
  11.         return ApplicationListener.super.supportsAsyncExecution();
  12.     }
  13. }
复制代码
View Code3、@PostConstruct、@PreDestroy注解

@PostConstruct注解时针对Bean初始化完成后,要执行的方法,@PreDestroy注解时Bean销毁时执行。
  1. @Component
  2. public class RedisInit {
  3.     @PostConstruct
  4.     public void init(){
  5.         System.out.println("缓存预热~");
  6.     }
  7.     @PreDestroy
  8.     public void destroy(){
  9.         System.out.println("缓存清理~");
  10.     }
  11. }
复制代码
View Code 
4、InitializingBean、DisposableBean接口

InitializingBean、DisposableBean 接口也是针对的Bean的。
  1. @Component
  2. public class RedisInitializingBean implements InitializingBean, DisposableBean {
  3.     @Override
  4.     public void afterPropertiesSet() throws Exception {
  5.         System.out.println("缓存预热~");
  6.     }
  7.     @Override
  8.     public void destroy() throws Exception {
  9.         System.out.println("缓存清理~");
  10.     }
  11. }
复制代码
View Code5、配置Bean时,指定Bean的init和destroy方法

  1. @Bean(name = "redisInit",initMethod = "init",destroyMethod = "")
  2.     public RedisInit redisInit(){
  3.         return new RedisInit();
  4.     }
复制代码
View Code 
[code][/code]
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莫张周刘王

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表