1、前言
观察者模式是一种很常见的设计模式,在java jdk里或者Spring框架里都有实现,并提供api给开发者
比如在jdk中提供的:
- EventObject:所有事件状态对象的根类;
- EventListener:所有事件侦听器接口必须扩展的接口;
2、观察者模式
观察者模式也可以称之为发布-订阅(Publish/Subscribe)模式,是一种行为型的设计模式。 观察者模式包含如下角色:
- Subject:目标,或者说是被观察者
- ConcreteSubject:具体目标,,Subject的具体实现类,在这里实现通知事件
- Observer:观察者,抽象的观察者,可以一个或者多个
- ConcreteObserver:具体观察者,在这里维护观察对象的具体操作
3、Spring框架中监听机制
在Spring框架中使用ApplicationEvent
和ApplicationListener
来实现事件监听机制
创建一个类,实现ApplicationEvent
import lombok.Data;
import org.springframework.context.ApplicationEvent;
@Data
public class UserRegisterEvent extends ApplicationEvent {
private Long userId;
private String name;
public UserRegisterEvent(Object source){
super(source);
}
}
实现监听可以使用注解@EventListener
import com.example.eventlistener.event.UserRegisterEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class UserRegisterSmsListener {
@EventListener
public void eventListener(UserRegisterEvent event) {
log.info("监听到用户注册,准备发送短信...");
log.info("user info:{}" , event.toString());
}
}
可以实现ApplicationListener
接口或者 SmartApplicationListener
接口等方法,重写getOrder
方法,返回值越小,优先级越高
import com.example.eventlistener.event.UserRegisterEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class UserRegisterMessageListener implements SmartApplicationListener {
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType){
return eventType == UserRegisterEvent.class;
}
@Override
public boolean supportsSourceType(Class<?> sourceType){
return true;
}
@Async("asyncThreadPool")
public void onApplicationEvent(ApplicationEvent event) {
log.info("监听到用户注册,准备给新用户发送站内信...");
log.info("user info:{}" , event.toString());
}
@Override
public int getOrder(){
return -1;
}
}
实现,可以直接使用@Autowired
注解或者实现ApplicationEventPublisherAware
接口
import com.example.eventlistener.event.UserRegisterEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
@Service
public class UserService implements ApplicationEventPublisherAware {
private ApplicationEventPublisher applicationEventPublisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
public void register() {
UserRegisterEvent userDto = new UserRegisterEvent(this);
userDto.setUserId(10001L);
userDto.setName("管理员");
applicationEventPublisher.publishEvent(userDto);
}
}
代码语言:javascript复制2022-10-16 05:18:15.987 INFO 11348 --- [nio-8080-exec-1] c.e.e.listener.UserRegisterSmsListener : 监听到用户注册,准备发送短信...
2022-10-16 05:18:15.988 INFO 11348 --- [nio-8080-exec-1] c.e.e.listener.UserRegisterSmsListener : user info:UserRegisterEvent(userId=10001, name=管理员)
2022-10-16 05:18:15.992 INFO 11348 --- [nio-8080-exec-1] c.e.e.l.UserRegisterEmailListener : 监听到用户注册,准备发送邮件...
2022-10-16 05:18:15.993 INFO 11348 --- [nio-8080-exec-1] c.e.e.l.UserRegisterEmailListener : user info:UserRegisterEvent(userId=10001, name=管理员)
2022-10-16 05:18:15.994 INFO 11348 --- [ thread-0] c.e.e.l.UserRegisterMessageListener : 监听到用户注册,准备给新用户发送站内信...
2022-10-16 05:18:15.994 INFO 11348 --- [ thread-0] c.e.e.l.UserRegisterMessageListener : user info:UserRegisterEvent(userId=10001, name=管理员)
异步支持
代码语言:javascript复制import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class SpringbootEventlistenerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootEventlistenerApplication.class, args);
}
}
代码语言:javascript复制import com.example.eventlistener.event.UserRegisterEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class UserRegisterSmsListener {
@EventListener
@Async
public void eventListener(UserRegisterEvent event) {
log.info("监听到用户注册,准备发送短信...");
log.info("user info:{}" , event.toString());
}
}
https://javamana.com/2020/12/20201221010401904A.html https://www.cnblogs.com/admol/p/14036564.html