ApplicationListener的简单使用
ApplicationListener在Spring框架中的作用是监听并处理应用程序中的事件。
ApplicationListener接口定义了一个onApplicationEvent方法,当监听器监听到事件发布后,会执行这个方法。这使得开发者能够灵活地响应应用程序中的各种事件,实现发布-订阅模式的功能。通过这种方式,Spring框架实现了应用程序内部不同组件之间的松耦合通信,提高了系统的可扩展性和可维护性。
代码结构:
1、定义监听实体
根据发布和订阅的实体不同,进行区分不同的监听。
People
package com.dyaqi.domain;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.context.ApplicationEvent;
/**
* @author: dongyq
* @date: 2023/4/17 17:34
* @since:
* @功能描述:
*/
@Getter
@Setter
@ToString
public class People extends ApplicationEvent {
public String name;
public Integer age;
public People(Object source, String name, Integer age) {
super(source);
this.name = name;
this.age = age;
}
}
User
package com.dyaqi.domain;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.context.ApplicationEvent;
/**
* @author: dongyq
* @date: 2023/4/17 17:34
* @since:
* @功能描述:
*/
@Getter
@Setter
@ToString
public class User extends ApplicationEvent {
public String name;
public Integer age;
public User(Object source, String name, Integer age) {
super(source);
this.name = name;
this.age = age;
}
}
2、定义发布者
package com.dyaqi.publisher;
import com.dyaqi.domain.People;
import com.dyaqi.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
/**
* @author: dongyq
* @date: 2023/4/17 17:40
* @since:
* @功能描述:
*/
@Component
public class Publisher {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void publis() {
System.out.println("发布事件");
applicationEventPublisher.publishEvent(new People(this,"张三", 29));
applicationEventPublisher.publishEvent(new User(this,"李四", 18));
}
}
3、定义订阅者
Listener1 监听 People
package com.dyaqi.listener;
import com.dyaqi.domain.People;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @author: dongyq
* @date: 2023/4/17 17:37
* @since:
* @功能描述:
*/
@Component
public class Listener1 implements ApplicationListener<People> {
@Override
public void onApplicationEvent(People people) {
System.out.println("Listener1 监听到事件");
System.out.println(people.toString());
}
}
Listener2监听 User
package com.dyaqi.listener;
import com.dyaqi.domain.People;
import com.dyaqi.domain.User;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
* @author: dongyq
* @date: 2023/4/17 17:37
* @since:
* @功能描述:
*/
@Component
public class Listener2 implements ApplicationListener<User> {
@Override
public void onApplicationEvent(User user) {
System.out.println("Listener2 监听到事件");
System.out.println(user.toString());
}
}
使用注解监听
package com.dyaqi.listener;
import com.dyaqi.domain.User;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
* @author: dongyq
* @date: 2023/4/17 17:37
* @since:
* @功能描述:
*/
@Component
public class Listener3 {
@EventListener
public void onApplicationEvent(User user) {
System.out.println("Listener3 监听到事件");
System.out.println(user.toString());
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义