SpringBoot比较重要的回调机制,事件,synchronized锁


一、SpringBoot比较重要的回调机制

演示项目:D:\javasrc\springboot学习笔记\sb-callback
参考课程:全方位深入解析最新版SpringBoot源码 D:\javastudy\全方位深入解析最新版SpringBoot源码\sb2
 
应用场景:
1.启动前环境检测?
2.启动时配置初始化?
3.启动后数据初始化?
 
类型
注入方式
回调时机
总体回调顺序
应用场景
ApplicationContextInitializer
spring.factories
等3种
IOC容器初始化时被回调
1
启动前环境检测
SpringApplicationRunListener
spring.factories
springboot启动过程中被多次回调
贯穿整个过程,最开始到最后
非常灵活,比如启动时配置初始化
ApplicationRunner
IOC容器,及类加注解@Component
容器启动完成后被回调
2
启动后数据初始化
CommandLineRunner
IOC容器,及类加注解@Component
ApplicationRunner后被回调
3
启动后数据初始化
ApplicationReadyEvent
IOC容器,及类加注解@Component
implements ApplicationListener<ApplicationReadyEvent>
在CommandLineRunner后
SpringApplicationRunListener...running前
4
启动后数据初始化
 
回调顺序:
SpringApplicationRunListener...starting...
SpringApplicationRunListener...environmentPrepared..Windows 10
ApplicationContextInitializer:...run....
SpringApplicationRunListener...contextPrepared...
SpringApplicationRunListener...contextLoaded...
SpringApplicationRunListener...started...
ApplicationRunner1...run....
ApplicationRunner2...run....
CommandLineRunner...run...[]
onApplicationEvent-ApplicationReadyEvent...run....
SpringApplicationRunListener...running...
 
 

二、自定义事件发布和监听

演示项目:D:\javasrc\springboot学习笔记\sb-event
 
  • 2种监听方法
1)实现接口
spring.factories 和 implements ApplicationListener<E extends ApplicationEvent>
2)基于注解
@Component 和 @EventListener
 
  • 异步监听
类注解:@EnableAsync
方法注解:@Async
 
3.项目改造建议
1)利用ApplicationRunner改造
main函数中的所有操作都可以用:ApplicationRunner方式初始化数据
好处:不用每个main函数去写重复代码,如有需要,不同环境可以用@ConditionalOnProperty来控制
 
2)利用事件改造
事件可以独立出来,使得事件不依赖于
不同环境不需要的事件,可以用@ConditionalOnProperty条件来控制
 
SpringBoot:回调,自定义事件发布和监听
synchronized锁

三、synchronized锁

D:\javastudy\并发相关\synchronizedDemo

1.三种锁

this,ObjectLock.class,自定义对象锁:Object lock = new Object()
三种锁不是同一把锁,任何一把锁锁住了,不影响其他锁的执行
 

2.synchronized (this)和synchronized void 共用同一把锁

method1和method4
 
public void method1_synchronized_this() {
//对象锁
synchronized (this) {
try {
System.out.println("do method1..synchronized (this)" + LocalDateTime.now());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
 
public synchronized void method4_synchronized_void() {
try {
System.out.println("do method4..synchronized void" + LocalDateTime.now());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

 

 

3.synchronized (ObjectLock.class)和static synchronized void共用同一把锁

method2和method5
 
public void method2_synchronized_ObjectLock_class() {
//类锁
synchronized (ObjectLock.class) {
try {
System.out.println("do method2..synchronized (ObjectLock.class)" + LocalDateTime.now());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
 
public static synchronized void method5_static_synchronized_void() {
//类锁
try {
System.out.println("do method5..static synchronized void" + LocalDateTime.now());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM