diff --git "a/springboot-source-code-analysis/(4)347円263円273円347円273円237円345円210円235円345円247円213円345円214円226円345円231円250円350円247円243円346円236円220円.md" "b/springboot-source-code-analysis/(4)347円263円273円347円273円237円345円210円235円345円247円213円345円214円226円345円231円250円350円247円243円346円236円220円.md" index 8e82986..b293a0e 100644 --- "a/springboot-source-code-analysis/(4)347円263円273円347円273円237円345円210円235円345円247円213円345円214円226円345円231円250円350円247円243円346円236円220円.md" +++ "b/springboot-source-code-analysis/(4)347円263円273円347円273円237円345円210円235円345円247円213円345円214円226円345円231円250円350円247円243円346円236円220円.md" @@ -208,3 +208,8 @@ public ConfigurableApplicationContext run(String... args) { + + +#### 5、推荐使用的系统初始化器【调用机制】 + +image-20210803143602922 \ No newline at end of file diff --git "a/springboot-source-code-analysis/(5)347円233円221円345円220円254円345円231円250円350円256円276円350円256円241円346円250円241円345円274円217円.md" "b/springboot-source-code-analysis/(5)347円233円221円345円220円254円345円231円250円350円256円276円350円256円241円346円250円241円345円274円217円.md" new file mode 100644 index 0000000..e283287 --- /dev/null +++ "b/springboot-source-code-analysis/(5)347円233円221円345円220円254円345円231円250円350円256円276円350円256円241円346円250円241円345円274円217円.md" @@ -0,0 +1,187 @@ +## 监听器 + +### 1、监听器模式介绍 + +image-20210803144505140 + +- 系统消息由广播器(Multicaster)发布出去,发布的内容统称为事件(Event),对关键事件产生订阅的是监听器(Listener); + + + +### 2、自定义事件-创建事件 + +- 创建天气事件抽象类 + + ```java + // 天气事件抽象类 + public abstract class WeatherEvent { + // 获取当前天气的事件 + public abstract String getWeather(); + } + ``` + +- 创建下雨事件实现类 + + ```java + // 下雨事件 + public class RainEvent extends WeatherEvent { + @Override + public String getWeather() { + return "rain"; + } + } + ``` + +- 创建下雪事件实现类 + + ```java + // 下雪事件 + public class SnowEvent extends WeatherEvent { + @Override + public String getWeather() { + return "snow"; + } + } + ``` + + + +#### 3、自定义事件监听器-创建监听器 + +- 创建天气事件监听器接口类 + + ```java + // 天气监听器 + public interface WeatherListener { + // 监听天气事件 + void onWeatherEvent(WeatherEvent event); + } + ``` + +- 创建下雨事件监听器实现类 + + ```java + // 下雨监听器 + public class RainListener implements WeatherListener { + @Override + public void onWeatherEvent(WeatherEvent event) { + if (event instanceof RainEvent) { + System.out.println("rain event ..." + event.getWeather()); + } + } + } + ``` + +- 创建下雪事件监听器实现类 + + ```java + // 下雪监听器 + public class SnowListener implements WeatherListener { + @Override + public void onWeatherEvent(WeatherEvent event) { + if (event instanceof SnowEvent) { + System.out.println("snow event.." + event.getWeather()); + } + } + } + ``` + + + +#### 4、自定义事件广播器-创建事件广播器 + +- 创建事件广播器接口 + + ```java + // 事件广播器 + public interface EventMulticaster { + // 广播事件 + void multicastEvent(WeatherEvent event); + // 添加监听器 + void addListener(WeatherListener listener); + // 删除监听器 + void removeListener(WeatherListener listener); + } + +- 创建事件广播器的抽象类 + + ```java + + // 抽象的事件广播器 + public abstract class AbstractEventMulticaster implements EventMulticaster { + // 事件监听器列表 + private List listenerList = new CopyOnWriteArrayList(); + + @Override + public void multicastEvent(WeatherEvent event) { + // 广播所有事件监听器 + listenerList.forEach(listener -> { + // 触发前-模版方法 + doStart(); + // 触发机制 + listener.onWeatherEvent(event); + // 触发后-模版方法 + doEnd(); + }); + } + + // 添加事件监听器 + @Override + public void addListener(WeatherListener listener) { + listenerList.add(listener); + } + + // 删除事件监听器 + @Override + public void removeListener(WeatherListener listener) { + listenerList.remove(listener); + } + + // 模版方法 + abstract void doStart(); + abstract void doEnd(); + } + ``` + +- 创建事件广播器的实现类 + + ```java + // 天气事件广播器 + public class WeatherEventMulticaster extends AbstractEventMulticaster { + @Override + public void doStart() { + System.out.println("multicaster event begin ..."); + } + @Override + public void doEnd() { + System.out.println("multicaster evnet end ...."); + } + } + ``` + + + +#### 5、测试 + +```java +public class Test { + + public static void main(String[] args) { + // 创建事件广播器 + WeatherEventMulticaster multicaster = new WeatherEventMulticaster(); + + // 添加事件监听器 + WeatherListener listener1 = new SnowListener(); + WeatherListener listener2 = new RainListener(); + multicaster.addListener(listener1); + multicaster.addListener(listener2); + + // 广播事件 + WeatherEvent event1 = new RainEvent(); + WeatherEvent event2 = new SnowEvent(); + multicaster.multicastEvent(event1); + multicaster.multicastEvent(event2); + } +} +``` + diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/AbstractEventMulticaster.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/AbstractEventMulticaster.java new file mode 100644 index 0000000..bd5dbb9 --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/AbstractEventMulticaster.java @@ -0,0 +1,50 @@ +package com.example.springboot.source.code.analysis.event; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + * Created by ipipman on 2021年8月3日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.event + * @Description: (用一句话描述该文件做什么) + * @date 2021年8月3日 3:20 下午 + */ + + +// 抽象的事件广播器 +public abstract class AbstractEventMulticaster implements EventMulticaster { + + // 事件监听器列表 + private List listenerList = new CopyOnWriteArrayList(); + + @Override + public void multicastEvent(WeatherEvent event) { + // 广播所有事件监听器 + listenerList.forEach(listener -> { + doStart(); + // 触发 + listener.onWeatherEvent(event); + doEnd(); + }); + } + + // 添加事件监听器 + @Override + public void addListener(WeatherListener listener) { + listenerList.add(listener); + } + + // 删除事件监听器 + @Override + public void removeListener(WeatherListener listener) { + listenerList.remove(listener); + } + + + // 模版方法 + abstract void doStart(); + + abstract void doEnd(); +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/EventMulticaster.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/EventMulticaster.java new file mode 100644 index 0000000..e94fc66 --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/EventMulticaster.java @@ -0,0 +1,23 @@ +package com.example.springboot.source.code.analysis.event; + +/** + * Created by ipipman on 2021年8月3日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.event + * @Description: (用一句话描述该文件做什么) + * @date 2021年8月3日 3:04 下午 + */ + +// 事件广播器 +public interface EventMulticaster { + + // 广播事件 + void multicastEvent(WeatherEvent event); + + // 添加监听器 + void addListener(WeatherListener listener); + + // 删除监听器 + void removeListener(WeatherListener listener); +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/RainEvent.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/RainEvent.java new file mode 100644 index 0000000..a6f127e --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/RainEvent.java @@ -0,0 +1,19 @@ +package com.example.springboot.source.code.analysis.event; + +/** + * Created by ipipman on 2021年8月3日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.event + * @Description: (用一句话描述该文件做什么) + * @date 2021年8月3日 2:52 下午 + */ + +// 下雨事件 +public class RainEvent extends WeatherEvent { + + @Override + public String getWeather() { + return "rain"; + } +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/RainListener.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/RainListener.java new file mode 100644 index 0000000..d7e72bf --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/RainListener.java @@ -0,0 +1,22 @@ +package com.example.springboot.source.code.analysis.event; + +/** + * Created by ipipman on 2021年8月3日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.event + * @Description: (用一句话描述该文件做什么) + * @date 2021年8月3日 3:00 下午 + */ + + +// 下雨监听器 +public class RainListener implements WeatherListener { + + @Override + public void onWeatherEvent(WeatherEvent event) { + if (event instanceof RainEvent) { + System.out.println("rain event ..." + event.getWeather()); + } + } +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowEvent.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowEvent.java new file mode 100644 index 0000000..c12c5ca --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowEvent.java @@ -0,0 +1,19 @@ +package com.example.springboot.source.code.analysis.event; + +/** + * Created by ipipman on 2021年8月3日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.event + * @Description: (用一句话描述该文件做什么) + * @date 2021年8月3日 2:51 下午 + */ + +// 下雪事件 +public class SnowEvent extends WeatherEvent { + + @Override + public String getWeather() { + return "snow"; + } +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowListener.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowListener.java new file mode 100644 index 0000000..2f01f4f --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/SnowListener.java @@ -0,0 +1,21 @@ +package com.example.springboot.source.code.analysis.event; + +/** + * Created by ipipman on 2021年8月3日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.event + * @Description: (用一句话描述该文件做什么) + * @date 2021年8月3日 3:02 下午 + */ + +// 下雪监听器 +public class SnowListener implements WeatherListener { + + @Override + public void onWeatherEvent(WeatherEvent event) { + if (event instanceof SnowEvent) { + System.out.println("snow event.." + event.getWeather()); + } + } +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/Test.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/Test.java new file mode 100644 index 0000000..0aca9cc --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/Test.java @@ -0,0 +1,32 @@ +package com.example.springboot.source.code.analysis.event; + +/** + * Created by ipipman on 2021年8月3日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.event + * @Description: (用一句话描述该文件做什么) + * @date 2021年8月3日 3:29 下午 + */ + + +public class Test { + + public static void main(String[] args) { + // 创建事件广播器 + WeatherEventMulticaster multicaster = new WeatherEventMulticaster(); + + // 添加事件监听器 + WeatherListener listener1 = new SnowListener(); + WeatherListener listener2 = new RainListener(); + multicaster.addListener(listener1); + multicaster.addListener(listener2); + + // 广播事件 + WeatherEvent event1 = new RainEvent(); + WeatherEvent event2 = new SnowEvent(); + multicaster.multicastEvent(event1); + multicaster.multicastEvent(event2); + } + +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherEvent.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherEvent.java new file mode 100644 index 0000000..ad2b98b --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherEvent.java @@ -0,0 +1,18 @@ +package com.example.springboot.source.code.analysis.event; + +/** + * Created by ipipman on 2021年8月3日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.event + * @Description: (用一句话描述该文件做什么) + * @date 2021年8月3日 2:50 下午 + */ + + +// 天气事件抽象类 +public abstract class WeatherEvent { + + // 获取当前天气的事件 + public abstract String getWeather(); +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherEventMulticaster.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherEventMulticaster.java new file mode 100644 index 0000000..642736b --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherEventMulticaster.java @@ -0,0 +1,24 @@ +package com.example.springboot.source.code.analysis.event; + +/** + * Created by ipipman on 2021年8月3日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.event + * @Description: (用一句话描述该文件做什么) + * @date 2021年8月3日 3:27 下午 + */ + +// 天气事件广播器 +public class WeatherEventMulticaster extends AbstractEventMulticaster { + + @Override + public void doStart() { + System.out.println("multicaster event begin ..."); + } + + @Override + public void doEnd() { + System.out.println("multicaster evnet end ...."); + } +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherListener.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherListener.java new file mode 100644 index 0000000..ff237ce --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/event/WeatherListener.java @@ -0,0 +1,17 @@ +package com.example.springboot.source.code.analysis.event; + +/** + * Created by ipipman on 2021年8月3日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.event + * @Description: (用一句话描述该文件做什么) + * @date 2021年8月3日 2:58 下午 + */ + +// 天气监听器 +public interface WeatherListener { + + // 监听天气事件 + void onWeatherEvent(WeatherEvent event); +}

AltStyle によって変換されたページ (->オリジナル) /