当前位置: 首页 > news >正文

中文域名网站骗局广州seo优化费用

中文域名网站骗局,广州seo优化费用,wordpress建站主机,网站设计套用模板目录 1、Spring监听器简介 2、事件(Event) 3、监听器(Listener) 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 ApplicationListener<E extends ApplicationEvent> extends EventListener {void onApplicationEvent(E event);default boolean supportsAsyncExecution() {return true;}static <T> ApplicationListener<PayloadApplicationEvent<T>> forPayload(Consumer<T> 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 ApplicationListener<CustomEvent> {@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 {@Async@EventListenerpublic 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还提供了注解方式和异步处理的支持,使得事件处理更加方便和高效。

http://www.hkea.cn/news/612458/

相关文章:

  • 视频微网站开发哪个公司网站设计好
  • 品网站建设智能搜索引擎
  • 怎样在百度建网站seo建设者
  • 四海网络网站建设咨询什么叫做网络营销
  • 安徽建设网官方网站优化分析
  • 网站根目录文件名游戏推广员是做什么的
  • 个体工商户怎么做网站西安网站seo技术
  • 报名网站制作2345网址导航官网下载安装
  • 图书购物网站开发总结百度发广告需要多少钱
  • 做网站 业务流程图站长统计性宝app
  • 长沙做网站大概多少钱万网域名注册教程
  • 成都网站建设网站产品推广计划书怎么写
  • 深圳个人网站建设大连网络推广公司哪家好
  • 建设工程教育appseo技术培训中心
  • 家教中介怎么利用网站来做的免费广告推广
  • wordpress仿制建设seo是什么平台
  • 商城网站建设分为几块seo臻系统
  • 网络营销对于个人而言有什么作用seo文章
  • 做书籍封皮的网站今日中国新闻
  • 东莞建设网站电工培训技术学校
  • 深圳聘请做网站人员成都排名seo公司
  • 网站备案之后东莞网站关键词优化公司
  • 多种专业网站建设潍坊网站排名提升
  • 网站投稿系统怎么做网站制作流程是什么
  • 交警网站建设整改百度推广怎么推广
  • 重庆网站建设哪里比较好呢网站下载
  • 网站运行速度慢的原因看b站二十四小时直播间
  • 电商网站开发服务全网营销骗局揭秘
  • 个人网站怎么做互联网营销师培训课程免费
  • 微信网站建设价格网站开发报价方案