首页 > 其他分享 >ApplicationListener的简单使用

ApplicationListener的简单使用

时间:2024-06-11 18:10:45浏览次数:12  
标签:ApplicationListener 17 org 简单 springframework 使用 import com public

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

4、日志

标签:ApplicationListener,17,org,简单,springframework,使用,import,com,public
From: https://www.cnblogs.com/dyaqi/p/18242522

相关文章

  • 基于.Net 框架实现WebSocket 简单通信——服务端
    新建项目创建一个.Net框架的控制台程序。添加包 项目→管理NuGet程序包打开包管理窗口,添加SuperWebSocket程序包。实现项目→添加类打开添加新项窗口,添加一个C#类。启动监听 WebSocketServersocket=newWebSocketServer();Console.WriteLine("IP:"+ip......
  • vue初使用实例之笔记本
    <!DOCTYPEhtml><html><head><metacharset="utf-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><title></title><metaname="des......
  • JavaScript中什么是类,如何使用?
    在JavaScript中,类是一种用于创建对象的模板。它定义了对象的属性和方法,并可以通过实例化来创建具体的对象。类提供了一种结构化的方式来组织和管理代码,使得代码更易于理解和维护。下面我将通过三个例子来详细说明JavaScript中类的概念和使用方法。例子1:创建一个表示人的类cl......
  • NET8中增加的简单适用的DI扩展库Microsoft.Extensions.DependencyInjection.AutoActiv
    这个库提供了在启动期间实例化已注册的单例,而不是在首次使用它时实例化。单例通常在首次使用时创建,这可能会导致响应传入请求的延迟高于平时。在注册时创建实例有助于防止第一次Request请求的SLA以往我们要在注册的时候启动单例可能会这样写://注册:services.AddSingleton<Fil......
  • 使用WPF 当程序已打开时第二次打开程序直接弹出第一次打开的程序
    在代码中增加[DllImport("user32.dll")]privatestaticexternboolSetForegroundWindow(IntPtrhWnd);[DllImport("user32.dll")]privatestaticexternboolShowWindowAsync(IntPtrhWnd,intnCmdShow);[DllImport("user32.dll"......
  • 使用使用rundll32 调用指定dll的方法
    使用使用rundll32调用指定dll的方法//顾名思义,"执行32位的DLL文件"。它的作用是执行DLL文件中的内部函数,这样在进程当中,只会有Rundll32.exe,而不会有DLL后门的进程,这样,就实现了进程上的隐藏。介绍一下Rundll32.exe这个文件,功能就是以命令行的方式调用动态链接程序库。系统中......
  • 51单片机数码管显示的计数器,按键按下暂定,再次按下继续。(按键功能使用中断实现)
    1、功能描述数码管显示的计数器,按键按下暂定,再次按下继续。(按键功能使用中断实现)2、实验原理·  按键与中断:使用单片机的外部中断功能来检测按键动作,实现非阻塞的按键检测。·  中断服务程序:编写中断服务程序来处理按键动作,切换暂停和继续的状态。·  动态显示:通过......
  • WPF阻止窗体被系统缩放,使用显示器DPI
    WPF默认是跟随系统DPI变化(缩放与布局)而缩放窗体的;微软把它称为默认DPI感知,当DPI发生变化时WPF感知到后缩放窗体,介绍链接:设置进程的默认DPI感知(Windows)-Win32apps|MicrosoftLearn如果我们不希望窗体被缩放,而是让窗体使用显示器DPI该怎么办呢?首先修改app.manifest,如......
  • 使用python处理excel数据
    使用python处理excel数据python处理excel数据时间差计算平均量计算excel处理后数据python处理excel数据excel数据有一列是开始时间,一列为结束时间,计算时间差,时间差>1h,将数据平均为1h。时间差>1h,总量也将平均到每个小时,如第三行数据,时间差为4h,数据为5.2,所以每小时......
  • 巧妙使用mapstruct来解决数据库entiy到实体dto的映射关系
    1.引入mapstruct<dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId></dependency><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct-p......