言成言成啊 | Kit Chen's Blog

简单实现一个Java事件

发布于2021-12-19 22:04:35,更新于2022-01-17 20:57:35,标签:java  文章会持续修订,转载请注明来源地址:https://meethigher.top/blog

我负责的模块,在执行我这边逻辑的时候,要给别人预留一个口,可以通知别人,我这边的逻辑执行了,就这么简单的一个功能,不能用springboot,很惭愧,我居然不会做了。

springboot的高度封装,已经让我变成了一个废物。

这个口,其实就是一个事件。下面举例三种Java事件的实现方式

  1. 抽象类
  2. jdk util中的Event,这个方式感觉并不那么友好
  3. springboot Event

模拟身体要撒尿、拉屎,大脑监听到信号的操作。

一、抽象类

先实现一个抽象类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public abstract class EventHandler {
public void beforePee() {

}

public void afterPee() {

}

public void beforeShit() {

}

public void afterShit() {

}
}

Body

1
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
28
29
30
31
32
33
34
35
36
37
38
39
public class Body {

private EventHandler eventHandler=new EventHandler() {

};

public void setEventHandler(EventHandler eventHandler) {
this.eventHandler = eventHandler;
}


public void bodyActivity() {
eventHandler.beforePee();
pee();
eventHandler.afterPee();

eventHandler.beforeShit();
shit();
eventHandler.afterShit();
}

private void pee() {
System.out.println("撒尿");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void shit() {
System.out.println("拉屎");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

大脑实现监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class HeadListener extends EventHandler {

@Override
public void beforePee() {
System.out.println("大脑监听到身体想撒尿");
}

@Override
public void afterPee() {
System.out.println("大脑监听到身体撒完尿");
}

@Override
public void beforeShit() {
System.out.println("大脑监听到身体想拉屎");
}

@Override
public void afterShit() {
System.out.println("大脑监听到身体拉完屎");
}
}

启动入口类

1
2
3
4
5
6
7
public class AbstractClassMain {
public static void main(String[] args) {
Body body =new Body();
body.setEventHandler(new HeadListener());
body.bodyActivity();
}
}

输出结果

1
2
3
4
5
6
大脑监听到身体想撒尿
撒尿
大脑监听到身体撒完尿
大脑监听到身体想拉屎
拉屎
大脑监听到身体拉完屎

不一定要用抽象类,其实接口本身也是一种实现方式

二、SpringBoot中的Event

事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Event extends EventObject {

/**
* Constructs a prototypical Event.
*
* @param source the object on which the Event initially occurred
* @throws IllegalArgumentException if source is null
*/
public Event(Object source) {
super(source);
}
}
public class BeforePee {
}
public class AfterPee {
}
public class BeforeShit {
}
public class AfterShit {
}

Body

1
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
28
29
30
31
32
33
34
35
36
37
38
39
@Component
public class Body {

@Autowired
private ApplicationContext context;



public void bodyActivity() {
context.publishEvent(new Event(new BeforePee()));
pee();
context.publishEvent(new Event(new AfterPee()));



context.publishEvent(new Event(new BeforeShit()));
shit();
context.publishEvent(new Event(new AfterShit()));

}

private void pee() {
System.out.println("撒尿");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void shit() {
System.out.println("拉屎");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

大脑监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Component
public class HeadListener {
@EventListener
public void headListener(Event event) {
Object source = event.getSource();
if(source instanceof BeforePee) {
System.out.println("大脑监听到身体想撒尿");
}
if(source instanceof AfterPee) {
System.out.println("大脑监听到身体撒完尿");
}
if(source instanceof BeforeShit) {
System.out.println("大脑监听到身体想拉屎");
}

if(source instanceof AfterShit) {
System.out.println("大脑监听到身体拉完屎");
}
}
}

配置Runner让Springboot项目启动后执行

1
2
3
4
5
6
7
8
9
10
@Component
public class ExecuteRunner implements CommandLineRunner {

@Autowired
private Body body;
@Override
public void run(String... args) throws Exception {
body.bodyActivity();
}
}

运行结果跟上面相同

发布:2021-12-19 22:04:35
修改:2022-01-17 20:57:35
链接:https://meethigher.top/blog/2021/achieve-java-event/
标签:java 
付款码 打赏 分享
Shift+Ctrl+1 可控制工具栏