# springboot 中初始化资源的方式
# CommandLineRunner
- 定义初始化类
MyCommandLineRunner - 实现
CommandLineRunner接口,并实现它的 run() 方法,在该方法中编写初始化逻辑 - 注册成 Bean,添加 @Component 注解即可
package cn.zh.controller;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("项目初始化---------------11");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
# ApplicationRunner
- 定义初始化类 MyApplicationRunner
- 实现 ApplicationRunner 接口,并实现它的 run() 方法,在该方法中编写初始化逻辑
- 注册成 Bean,添加 @Component 注解即可
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;
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("项目初始化二---------");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
可以看到,通过实现 ApplicationRunner 接口,和通过实现 CommandLineRunner 接口都可以完成项目的初始化操作,实现相同的效果。
两者之间唯一的区别是 run() 方法中自带的形参不相同,在 CommandLineRunner 中只是简单的String... args形参,而 ApplicationRunner 则是包含了 ApplicationArguments 对象,可以帮助获得更丰富的项目信息。
# @Order
如果项目中既有实现了 ApplicationRunner 接口的初始化类,又有实现了 CommandLineRunner 接口的初始化类,那么会是哪一个先执行呢?
测试告诉我们,答案是实现了 ApplicationRunner 接口的初始化类先执行
如果需要改变两个初始化类之间的默认执行顺序,那么使用 @Order 注解就可以帮助我们解决这个问题
@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初始化");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# @PostConstruct
使用 @PostConstruct 注解同样可以帮助我们完成资源的初始化操作,前提是这些初始化操作不需要依赖于其它Spring beans的初始化工作。
@PostConstruct 注解是用在方法上的
- 只有一个非静态方法能使用此注解
- 被注解的方法不得有任何参数
- 被注解的方法返回值必须为 void
- 被注解方法不得抛出已检查异常
- 此方法只会被执行一次
# 总结
使用 @PostConstruct 注解进行初始化操作的顺序是最快的,前提是这些操作不能依赖于其它 Bean 的初始化完成。通过添加 @Order 注解,我们可以改变同层级之间不同 Bean 的加载顺序。
# InitializingBean
InitializingBean 是 Spring 提供的一个接口,只包含一个方法 afterPropertiesSet()。
凡是实现了该接口的类,当其对应的 Bean 交由 Spring 管理后,当其必要的属性全部设置完成后,Spring 会调用该 Bean 的 afterPropertiesSet()。
@Component
public 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");
}
}
2
3
4
5
6
7
8
9
10
11
# ApplicationListener
ApplicationListener 就是 spring 的监听器,能够用来监听事件,典型的观察者模式。
如果容器中有一个 ApplicationListener Bean,每当 ApplicationContext 发布 ApplicationEvent 时,ApplicationListener Bean 将自动被触发。
这种事件机制都必须需要程序显示的触发。其中 spring 有一些内置的事件,当完成某种操作时会发出某些事件动作。
比如监听 ContextRefreshedEvent 事件,当所有的 bean 都初始化完成并被成功装载后会触发该事件,实现 ApplicationListener 接口可以收到监听动作,然后可以写自己的逻辑。同样事件可以自定义、监听也可以自定义,完全根据自己的业务逻辑来处理。所以也能做到资源的初始化加载。
@Component
public class MyListener1 implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
//打印出每次事件的名称
System.out.println(applicationEvent.toString());
if (applicationEvent instanceof ApplicationReadyEvent) {
System.out.println("项目启动OK");
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13