spring boot中如何创建事件监听器呢?
下文笔者讲述SpringBoot中创建事件监听器的方法及示例分享
创建事件监听器的实现思路
方式1: 1.1.定义一个ApplicationEvent 1.2.定义Applicationlistener监听器 1.3.使用springApplication对象的add方法 将监听器添加到springboot的启动对象中 方式2: 使用@Component和@EventListener注解定义一个事件监听器 方式3: 配置文件中定义监听器例:创建监听器的示例
package com.java265.bootsample.listener; import org.springframework.context.ApplicationEvent; public class BlackListEvent extends ApplicationEvent { private String address; public BlackListEvent(Object source, String address){ super(source); this.address = address; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } //定义事件监听器 package com.java265.bootsample.listener; import org.springframework.context.ApplicationListener; public class BlackListListener implements ApplicationListener<BlackListEvent> { @Override public void onApplicationEvent(BlackListEvent blackListEvent) { String threaid = Thread.currentThread().getName(); System.out.println(threaid + "收到回调事件:" + blackListEvent.getAddress()); } } //监听器注册到spring boot中 package com.java265.bootsample; import com.java265.bootsample.listener.BlackListListener; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication public class BootsampleApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(BootsampleApplication.class); app.addListeners(new BlackListListener()); app.run(args); } } //测试代码 package com.java265.bootsample.service; import com.java265.bootsample.listener.BlackListEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.atomic.AtomicLong; @RestController public class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); @Autowired private ApplicationContext context; @GetMapping("/test") public Test test(@RequestParam(value = "name", defaultValue = "World") String name) { BlackListEvent event = new BlackListEvent(this, "greeting"); context.publishEvent(event); return new Test(counter.incrementAndGet(), String.format(template, name)); } } package com.java265.bootsample.service; public class Test { private final long id; private final String content; public Test(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } } http://localhost:8080/test
方式2:使用注解生成监听器
使用 @Component和@EventListener两个注解 //生成监听器 package com.java265.bootsample.listener; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.stereotype.Component; @Component public class AnnotationListener { @EventListener public void processBlacklistEvent(BlackListEvent event){ String threaid = Thread.currentThread().getName(); System.out.println("annotation listener------------------" + threaid); } }例2:
package com.java265.bootsample.listener; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.stereotype.Component; @Component public class AnnotationListener { @EventListener @Async public void processBlacklistEvent(BlackListEvent event){ String threaid = Thread.currentThread().getName(); System.out.println("annotation listener------------------" + threaid); } } package com.java265.bootsample; import com.java265.bootsample.listener.BlackListListener; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableAsync public class BootsampleApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(BootsampleApplication.class); app.addListeners(new BlackListListener()); app.run(args); } }
application.properties定义事件监听器
定义多个监听器的示例 context.listener.classes=\ com.java265.bootsample.listener.BlackListListener,\ com.java265.bootsample.listener.BlackListListener2
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。