优化器,seo是什么意思 职业,月饼网站建设,阿里云搜索引擎目录
1、Spring监听器简介
2、事件#xff08;Event#xff09;
3、监听器#xff08;Listener#xff09;
3、事件发布器
4、监听器使用
4.1、自定义事件
4.2、自定义监听器
4.3、发布事件
4.4、测试 4.5、使用注解方式监听
4.6、异步事件处理
5、总结 1、Spri…目录
1、Spring监听器简介
2、事件Event
3、监听器Listener
3、事件发布器
4、监听器使用
4.1、自定义事件
4.2、自定义监听器
4.3、发布事件
4.4、测试 4.5、使用注解方式监听
4.6、异步事件处理
5、总结 1、Spring监听器简介
Spring 监听器Listener用于监听应用程序中的事件并在事件发生时执行特定操作。常见的事件包括应用启动、关闭、请求处理等。Spring 提供了多种监听器接口例如ApplicationListener、ServletRequestListener、HttpSessionListener等开发者可以通过实现这些接口或者添加对应的注解来监听和处理事件。
2、事件Event
在Spring事件顶层类为EventObject抽象类ApplicationEvent继承了EventObject是所有事件的基础类自定义事件需要继承此类。Spring中提供了SpringApplicationEvent在该类下提供一些实现类。 继承这些类的自定义类都可用作为事件类进行发布事件通常用于在系统中发生某些操作通知其他的模块进行一些相应的操作例如系统启动事件、客户注册系统等。
3、监听器Listener
Spring中的监听器ApplicationListener属于函数式接口实现该接口的类可用监听特点类型的事件当检查到特点类型事件时可用自动进行一些用户开发的操作。
FunctionalInterface
public interface ApplicationListenerE extends ApplicationEvent extends EventListener {void onApplicationEvent(E event);default boolean supportsAsyncExecution() {return true;}static T ApplicationListenerPayloadApplicationEventT forPayload(ConsumerT consumer) {return (event) - {consumer.accept(event.getPayload());};}
}
3、事件发布器
Spring中发布事件的接口ApplicationEventPublisher用于发布事件。ApplicationContext上下文接口继承了该类可以发布事件。
FunctionalInterface
public interface ApplicationEventPublisher {default void publishEvent(ApplicationEvent event) {this.publishEvent((Object)event);}void publishEvent(Object event);
}
4、监听器使用
4.1、自定义事件
创建自定义事件类自定义事件类需要继承ApplicationEvent。
package com.gs.listener;import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.context.ApplicationEvent;public class CustomEvent extends ApplicationEvent {private String message;public CustomEvent(Object source, String message) {super(source);this.message message;}public String getMessage(){return this.message;}
}4.2、自定义监听器
自定义监听器需要实现ApplicationListener接口。
package com.gs.listener;import org.springframework.context.ApplicationListener;Component
public class CustomEvenListener implements ApplicationListenerCustomEvent {Overridepublic void onApplicationEvent(CustomEvent event) {System.out.println(接受客户消息 event.getMessage());}
}4.3、发布事件
自定义事件发布器需要实现ApplicationEventPublisher接口
package com.gs.listener;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;Component
public class CustomEventPublisher {Autowiredprivate ApplicationEventPublisher publisher;public void publishCustomEvent(String message){System.out.println(发布自定义事件);publisher.publishEvent(new CustomEvent(this,这是一个自定义测试事件));}
}4.4、测试
package com.gs.listener;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;Component
public class CustomEventTest implements CommandLineRunner {Autowiredprivate CustomEventPublisher publisher;Overridepublic void run(String... args) throws Exception {publisher.publishCustomEvent(Hello Spring Event);}
}运行结果 4.5、使用注解方式监听
通过在方法上使用EventListener注解进行监听
package com.gs.listener;import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;Component
public class CustomEvenListener {EventListenerpublic void handleCustomEvent(CustomEvent event) {System.out.println(接受客户消息: event.getMessage());}
}4.6、异步事件处理
默认情况下Spring事件是同步处理的。如果希望事件处理异步进行可以使用Async注解。例如
package com.gs.listener;import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;Component
public class CustomEvenListener {AsyncEventListenerpublic void handleCustomEvent(CustomEvent event) {System.out.println(接受客户消息: event.getMessage());}
}启动类开启异步
EnableAsync
SpringBootApplication
public class DemoProjectApplication {public static void main(String[] args) {SpringApplication.run(DemoProjectApplication.class, args);}}
5、总结
Spring的监听器机制提供了一种灵活的方式来处理应用程序中的事件。通过自定义事件和监听器可以实现模块之间的解耦提高代码的可维护性和可扩展性。同时Spring还提供了注解方式和异步处理的支持使得事件处理更加方便和高效。