当前位置:网站首页>Observer mode -- ApplicationContext
Observer mode -- ApplicationContext
2022-04-22 08:00:00 【Small planes love traveling】
One 、 Observer mode definition :
Refers to the existence of one to many dependencies among multiple objects , When the state of an object changes , All objects that depend on it are notified and automatically updated . This pattern is sometimes called Publishing - A subscription model 、 Model - View mode , It's an object behavior pattern
advantage :
- It reduces the coupling between the target and the observer , There is an abstract coupling between the two . It conforms to the principle of dependency inversion .
- There is a trigger mechanism between the target and the observer .
shortcoming :
- The dependence between the target and the observer is not completely relieved , And it's possible to have circular references .
- When there are many observers , The announcement conference took a lot of time , Affect the efficiency of the program .
Two 、 spring in ApplicationContext An example of an application observer is as follows :
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class Test1Controller {
@Resource
private ApplicationContext applicationContext;
@GetMapping(value = "/test")
public void test(){
String message = " Xiao Li, your black s The needle doesn't poke , Come to my office ";
applicationContext.publishEvent(new TestEvent(this,message));
}
}
import lombok.Getter;
import org.springframework.context.ApplicationEvent;
@Getter
public class TestEvent extends ApplicationEvent {
private final String message;
public TestEvent(Object source, String message){
super(source);
this.message = message;
}
}
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class TestEventListener {
@EventListener
public void receiveMessage(TestEvent testEvent){
System.out.print(testEvent.getMessage());
}
}
A simple code for you , Official account will be developed later. , If you have any questions, please add me wechat , Learning together ~

版权声明
本文为[Small planes love traveling]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220623382299.html
边栏推荐
- Elmentui table style customization (row color, background color, content scrolling, de scrolling bar)
- [TCP / IP link layer 2]
- mysql查询数据库中所有字段的属性
- 基于卷积神经网络LeNet-5模型的mnist手写数字识别
- VC call control
- Configuration list of raspberry pie (entry reference)
- Autoware displays user interface details
- [GPS - NMEA-0183 protocol]
- 【技术】西门子软PLC与组态软件如何通讯?
- Callable结合CountDownLatch实战应用
猜你喜欢

培训赋能 | 打造专业技术服务团队

ActiveX control usage summary

Grail layout and double wing layout

如何保护个人信息安全?@每个人,一定不要错过这份干货分享

XSS靶场过关

OBD external test equipment initialization judgment protocol type (SAE J1939 / iso15765 / iso27145)

树莓派:模拟iSCSI存储

C#自制一个简单的树莓派IP寻找工具

使用sss 框架搭建简单毕设, 宿舍管理系统

MNIST handwritten numeral recognition based on convolutional neural network lenet-5 model
随机推荐
2021-10-08 Selenium无头模式打开浏览器
About XSS Cross Station
朴素的CSRF漏洞
Grail layout and double wing layout
mysql中查询遇到longtext类型,查询效率很低。
Call another function within a shell function (without return value and with return value)
[TCP / IP overview]
C-10 age issues
Raspberry Pie: access BitLocker to go encrypted disk
Mysql索引的探索
文件安全笔记
如何保护个人信息安全?@每个人,一定不要错过这份干货分享
Bert模型系列一:输入到底是什么东西呢?
【世平信息】刻录内容保密检查与合规管控解决方案
js动态合并单元格
树莓派:模拟iSCSI存储
Unity mask click the lower UI game to start guiding Click
树莓派:使用mono和C#调用MSSQL数据库
Plain CSRF vulnerability
shell函数内调用另一个函数(不带返回值和带返回值)