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());
}
}