Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39eefb6

Browse files
committed
fix spring listener component modification in spring boot code analysis
1 parent 9e2a5c6 commit 39eefb6

File tree

8 files changed

+69
-3
lines changed

8 files changed

+69
-3
lines changed

springboot-source-code-analysis/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.1.7.RELEASE</version>
8+
<version>2.4.0</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.ipman.source.code.analysis.springboot</groupId>

springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/AbstractEventMulticaster.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.example.springboot.source.code.analysis.event;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
36
import java.util.List;
47
import java.util.concurrent.CopyOnWriteArrayList;
58

@@ -14,10 +17,12 @@
1417

1518

1619
// 抽象的事件广播器
20+
@Component
1721
public abstract class AbstractEventMulticaster implements EventMulticaster {
1822

1923
// 事件监听器列表
20-
private List<WeatherListener> listenerList = new CopyOnWriteArrayList<>();
24+
@Autowired
25+
private List<WeatherListener> listenerList;
2126

2227
@Override
2328
public void multicastEvent(WeatherEvent event) {

springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/RainListener.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.example.springboot.source.code.analysis.event;
22

3+
import org.springframework.stereotype.Component;
4+
35
/**
46
* Created by ipipman on 2021/8/3.
57
*
@@ -11,6 +13,7 @@
1113

1214

1315
// 下雨监听器
16+
@Component
1417
public class RainListener implements WeatherListener {
1518

1619
@Override

springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowListener.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.example.springboot.source.code.analysis.event;
22

3+
import org.springframework.stereotype.Component;
4+
35
/**
46
* Created by ipipman on 2021/8/3.
57
*
@@ -10,6 +12,7 @@
1012
*/
1113

1214
// 下雪监听器
15+
@Component
1316
public class SnowListener implements WeatherListener {
1417

1518
@Override

springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherEvent.java

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ public abstract class WeatherEvent {
1515

1616
// 获取当前天气的事件
1717
public abstract String getWeather();
18+
1819
}

springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherEventMulticaster.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.example.springboot.source.code.analysis.event;
22

3+
import org.springframework.stereotype.Component;
4+
35
/**
46
* Created by ipipman on 2021/8/3.
57
*
@@ -10,6 +12,7 @@
1012
*/
1113

1214
// 天气事件广播器
15+
@Component
1316
public class WeatherEventMulticaster extends AbstractEventMulticaster {
1417

1518
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.springboot.source.code.analysis.event;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* Created by ipipman on 2021/8/9.
8+
*
9+
* @version V1.0
10+
* @Package com.example.springboot.source.code.analysis.event
11+
* @Description: (用一句话描述该文件做什么)
12+
* @date 2021/8/9 3:46 下午
13+
*/
14+
15+
@Component
16+
public class WeatherRunListener {
17+
18+
@Autowired
19+
private WeatherEventMulticaster weatherEventMulticaster;
20+
21+
// 下雪事件
22+
public void snow(){
23+
weatherEventMulticaster.multicastEvent(new SnowEvent());
24+
}
25+
26+
// 下雨事件
27+
public void rain(){
28+
weatherEventMulticaster.multicastEvent(new RainEvent());
29+
}
30+
31+
// 天津监听器
32+
public void addListener(WeatherListener listener){
33+
weatherEventMulticaster.addListener(listener);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
package com.example.springboot.source.code.analysis;
22

3-
import org.junit.Test;
3+
import com.example.springboot.source.code.analysis.event.RainListener;
4+
import com.example.springboot.source.code.analysis.event.WeatherRunListener;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
47
import org.springframework.boot.test.context.SpringBootTest;
58

69
@SpringBootTest
710
class SpringbootSourceCodeAnalysisApplicationTests {
811

12+
// 引入事件监听器
13+
@Autowired
14+
private WeatherRunListener weatherRunListener;
15+
16+
// 测试事件监听器
17+
@Test
18+
public void testEvent(){
19+
weatherRunListener.addListener(new RainListener());
20+
weatherRunListener.snow();
21+
weatherRunListener.rain();
22+
}
23+
924
@Test
1025
void contextLoads() {
1126
}
1227

28+
1329
}

0 commit comments

Comments
 (0)