速读摘要文章源自JAVA秀-https://www.javaxiu.com/24138.html
假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?接口都可以完成项目的初始化操作,实现相同的效果。ApplicationListener就是spring的监听器,能够用来监听事件,典型的观察者模式。其中spring有一些内置的事件,当完成某种操作时会发出某些事件动作。同样事件可以自定义、监听也可以自定义,完全根据自己的业务逻辑来处理。文章源自JAVA秀-https://www.javaxiu.com/24138.html
原文约 1711 字 | 图片 9 张 | 建议阅读 4 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/24138.html
Spring Boot 中初始化资源的方式,你知道几种?
戳一戳→ 程序员的成长之路 文章源自JAVA秀-https://www.javaxiu.com/24138.html
文章源自JAVA秀-https://www.javaxiu.com/24138.html
程序员的成长之路文章源自JAVA秀-https://www.javaxiu.com/24138.html
互联网/程序员/技术/资料共享 文章源自JAVA秀-https://www.javaxiu.com/24138.html
关注文章源自JAVA秀-https://www.javaxiu.com/24138.html
阅读本文大概需要 3.5 分钟。文章源自JAVA秀-https://www.javaxiu.com/24138.html
作者:小破孩楼主文章源自JAVA秀-https://www.javaxiu.com/24138.html
来源:cnblogs.com/zouhong/p/14415713.html文章源自JAVA秀-https://www.javaxiu.com/24138.html
假设有这么一个需求,要求在项目启动过程中,完成线程池的初始化,加密证书加载等功能,你会怎么做?如果没想好答案,请接着往下看。今天介绍几种在Spring Boot中进行资源初始化的方式,帮助大家解决和回答这个问题。文章源自JAVA秀-https://www.javaxiu.com/24138.html
CommandLineRunner
定义初始化类
MyCommandLineRunner
文章源自JAVA秀-https://www.javaxiu.com/24138.html实现
CommandLineRunner
接口,并实现它的run()
方法,在该方法中编写初始化逻辑文章源自JAVA秀-https://www.javaxiu.com/24138.html注册成Bean,添加
@Component
注解即可文章源自JAVA秀-https://www.javaxiu.com/24138.html示例代码如下:文章源自JAVA秀-https://www.javaxiu.com/24138.html
package cn.zh.controller;import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Componentpublic class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("项目初始化---------------11"); }}
实现了 CommandLineRunner 接口的 Component 会在所有 Spring Beans 初始化完成之后, 在 SpringApplication.run() 执行之前完成。下面通过加两行打印来验证我们的测试。文章源自JAVA秀-https://www.javaxiu.com/24138.html
package cn.zh;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ProcApplication { public static void main(String[] args) { System.out.println("...start SpringApplication.run"); SpringApplication.run(ProcApplication.class,args); System.out.println("....end SpringApplication.run"); }}
文章源自JAVA秀-https://www.javaxiu.com/24138.html
ApplicationRunner
定义初始化类
MyApplicationRunner
文章源自JAVA秀-https://www.javaxiu.com/24138.html实现
ApplicationRunner
接口,并实现它的run()
方法,在该方法中编写初始化逻辑文章源自JAVA秀-https://www.javaxiu.com/24138.html注册成Bean,添加
@Component
注解即可文章源自JAVA秀-https://www.javaxiu.com/24138.html示例代码如下:文章源自JAVA秀-https://www.javaxiu.com/24138.html
package cn.zh.controller;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Componentpublic class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("项目初始化二---------"); } }
可以看到,通过实现 ApplicationRunner 接口,和通过实现 CommandLineRunner 接口都可以完成项目的初始化操作,实现相同的效果。两者之间唯一的区别是 run()
方法中自带的形参不相同,在 CommandLineRunner 中只是简单的String... args
形参,而 ApplicationRunner 则是包含了 ApplicationArguments 对象,可以帮助获得更丰富的项目信息。文章源自JAVA秀-https://www.javaxiu.com/24138.html
文章源自JAVA秀-https://www.javaxiu.com/24138.html
@Order
如果项目中既有实现了 ApplicationRunner
接口的初始化类,又有实现了 CommandLineRunner
接口的初始化类,那么会是哪一个先执行呢?测试告诉我们,答案是实现了 ApplicationRunner
接口的初始化类先执行,我想这点倒是不需要大家过分去关注为什么。但如果需要改变两个初始化类之间的默认执行顺序,那么使用 @Order
注解就可以帮助我们解决这个问题。文章源自JAVA秀-https://www.javaxiu.com/24138.html
文章源自JAVA秀-https://www.javaxiu.com/24138.html
@Component@Order(1)public class MyCommandLineRunner implements CommandLineRunner { /** * Callback used to run the bean. * * @param args incoming main method arguments * @throws Exception on error */ @Override public void run(String... args) throws Exception { System.out.println("项目初始化---------------11"); }}@Component@Order(2)public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("项目初始化二---------"); } @PostConstruct public void init(){ System.out.println("@PostConstruct初始化"); }}
文章源自JAVA秀-https://www.javaxiu.com/24138.html
文章源自JAVA秀-https://www.javaxiu.com/24138.html
文章源自JAVA秀-https://www.javaxiu.com/24138.html
@PostConstruct
使用 @PostConstruct
注解同样可以帮助我们完成资源的初始化操作,前提是这些初始化操作不需要依赖于其它Spring beans的初始化工作。文章源自JAVA秀-https://www.javaxiu.com/24138.html
文章源自JAVA秀-https://www.javaxiu.com/24138.html
可以看到 @PostConstruct
注解是用在方法上的,写一个方法测试一下吧。文章源自JAVA秀-https://www.javaxiu.com/24138.html
@PostConstruct public void init(){ System.out.println("@PostConstruct初始化"); }
注意:文章源自JAVA秀-https://www.javaxiu.com/24138.html
只有一个非静态方法能使用此注解文章源自JAVA秀-https://www.javaxiu.com/24138.html
被注解的方法不得有任何参数文章源自JAVA秀-https://www.javaxiu.com/24138.html
被注解的方法返回值必须为void文章源自JAVA秀-https://www.javaxiu.com/24138.html
被注解方法不得抛出已检查异常文章源自JAVA秀-https://www.javaxiu.com/24138.html
此方法只会被执行一次文章源自JAVA秀-https://www.javaxiu.com/24138.html
综上,使用
@PostConstruct
注解进行初始化操作的顺序是最快的,前提是这些操作不能依赖于其它Bean的初始化完成。通过添加@Order
注解,我们可以改变同层级之间不同Bean的加载顺序。文章源自JAVA秀-https://www.javaxiu.com/24138.html
InitializingBean
InitializingBean 是 Spring 提供的一个接口,只包含一个方法 afterPropertiesSet()。凡是实现了该接口的类,当其对应的 Bean 交由 Spring 管理后,当其必要的属性全部设置完成后,Spring 会调用该 Bean 的 afterPropertiesSet()。文章源自JAVA秀-https://www.javaxiu.com/24138.html
@Componentpublic class MyListener1 implements InitializingBean { @Autowired private ShopInfoMapper shopInfoMapper; @Override public void afterPropertiesSet() { //使用spring容器中的bean //System.out.println(shopInfoMapper.selectById("1").getShopName()); System.out.println("项目启动OK"); }}
ApplicationListener
ApplicationListener 就是spring的监听器,能够用来监听事件,典型的观察者模式。如果容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEvent时,ApplicationListener Bean将自动被触发。这种事件机制都必须需要程序显示的触发。文章源自JAVA秀-https://www.javaxiu.com/24138.html
其中spring有一些内置的事件,当完成某种操作时会发出某些事件动作。比如监听ContextRefreshedEvent事件,当所有的bean都初始化完成并被成功装载后会触发该事件,实现ApplicationListener接口可以收到监听动作,然后可以写自己的逻辑。文章源自JAVA秀-https://www.javaxiu.com/24138.html
同样事件可以自定义、监听也可以自定义,完全根据自己的业务逻辑来处理。所以也能做到资源的初始化加载。文章源自JAVA秀-https://www.javaxiu.com/24138.html
@Componentpublic class MyListener1 implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent applicationEvent) { //打印出每次事件的名称 System.out.println(applicationEvent.toString()); if (applicationEvent instanceof ApplicationReadyEvent) { System.out.println("项目启动OK"); } }}
<END>文章源自JAVA秀-https://www.javaxiu.com/24138.html
推荐阅读:文章源自JAVA秀-https://www.javaxiu.com/24138.html
为什么下载小电影时,经常会卡在99%?文章源自JAVA秀-https://www.javaxiu.com/24138.html
如何打日志才能方便排查问题?文章源自JAVA秀-https://www.javaxiu.com/24138.html
最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
文章源自JAVA秀-https://www.javaxiu.com/24138.html
获取方式:点个「在看」,点击上方小卡片,进入公众号后回复「面试题」领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/24138.html
朕已阅 文章源自JAVA秀-https://www.javaxiu.com/24138.html

评论