基礎支援
先來看看支援哪些預設事件
程式1(Service)
先看看程式
ApplicationEventPublisher這個是spring的東西,需要先看看程式
ApplicationEventPublisher這個是嗎方法會自動幫我們調用,拿到廣播發送者
/** * @author Carl * @date 2016/8/28 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 */public class EmailService implements ApplicationEventPublisherAware { private List<String> blackList; private ApplicationEventPublisher publisher; public void setBlackList(List<String> blackList) { this.blackList = blackList; } public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; } /** * 具体广播类 * @param address * @param text */ public void sendEmail(String address, String text) { if (blackList.contains(address)) { BlackListEvent event = new BlackListEvent(this, address, text); publisher.publishEvent(event); return; } // send email... } }
程式2(Event)
這裡也是需要繼承ApplicationEvent,並且裡面可以實現自己的一些必要參數等等,讓在收到廣播時進行獲取,當然透過source也可以的
/** * @author Carl * @date 2016/8/28 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 */public class BlackListEvent extends ApplicationEvent { private String address; private String test; public BlackListEvent(Object source, String address, String test) { super(source); this.address = address; this.test = test; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTest() { return test; } public void setTest(String test) { this.test = test; } }
程式3(receiver)
用spring還是得遵循他一套規範,那麼接收者的,還得實現ApplicationListener接口,那麼所有收到泛型廣播的對象,都會轉發onApplicationEvent裡面來的
當然了spring想得很周全,不一定透過實作ApplicationListener這個類,在bean類裡面加入註解@EventListener
/** * @author Carl * @date 2016/8/28 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 */public class BlackListNotifier implements ApplicationListener<BlackListEvent> { private String notificationAddress; public void setNotificationAddress(String notificationAddress) { this.notificationAddress = notificationAddress; } @EventListener public void onApplicationEvent(BlackListEvent event) { // notify appropriate parties via notificationAddress... System.out.println("onApplicationEvent, some thing I receive:" + event.getAddress() + ",text:" + event.getTest()); } @EventListener(condition = "#event.test == 'foo'") public void onApplicationCustomerEvent(BlackListEvent event) { System.out.println("onApplicationCustomerEvent,some thing I receive:" + event.getAddress() + ",text:" + event.getTest()); // notify appropriate parties via notificationAddress... } @EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class}) public void handleContextStart() { System.out.println("-------------handleContextStart"); } /** * 参数可以给BlackListEvent 可以不给 */ @EventListener(classes = {BlackListEvent.class}) public void handleBlackListEvent() { System.out.println("-------------handleBlackListEvent"); } }
@EventListener
解析一下這個註解怎麼用,猶如上面的程式,除了實現介面外,可以透過@EventListener註解來實作
condition可以使用SpEL表達式,就是當滿足條件才執行
classes當觸發event物件是這個class才會被執行
程式4(config bean)
這裡主要對一些服務以及接受廣播bean的註冊,以便接受
/** * 配置 * @author Carl * @date 2016/8/28 * @modify 版权所有.(c)2008-2016.广州市森锐电子科技有限公司 */@Configurationpublic class AppConfig { @Bean public EmailService emailService() { EmailService s = new EmailService(); List<String> emails = new ArrayList<>(3); emails.add("known.spammer@example.org"); emails.add("known.hacker@example.org"); emails.add("john.doe@example.org"); s.setBlackList(emails); return s; } @Bean public BlackListNotifier notifier() { BlackListNotifier notifier = new BlackListNotifier(); notifier.setNotificationAddress("blacklist@example.org"); return notifier; } }🎜🎜

java实现定时任务Jdk自带的库中,有两种方式可以实现定时任务,一种是Timer,另一种是ScheduledThreadPoolExecutor。Timer+TimerTask创建一个Timer就创建了一个线程,可以用来调度TimerTask任务Timer有四个构造方法,可以指定Timer线程的名字以及是否设置为为守护线程。默认名字Timer-编号,默认不是守护线程。主要有三个比较重要的方法:cancel():终止任务调度,取消当前调度的所有任务,正在运行的任务不受影响purge():从任务队

一、@RequestParam注解对应的axios传参方法以下面的这段Springjava代码为例,接口使用POST协议,需要接受的参数分别是tsCode、indexCols、table。针对这个Spring的HTTP接口,axios该如何传参?有几种方法?我们来一一介绍。@PostMapping("/line")publicList

SpringBoot和SpringCloud都是SpringFramework的扩展,它们可以帮助开发人员更快地构建和部署微服务应用程序,但它们各自有不同的用途和功能。SpringBoot是一个快速构建Java应用的框架,使得开发人员可以更快地创建和部署基于Spring的应用程序。它提供了一个简单、易于理解的方式来构建独立的、可执行的Spring应用

随着技术的更新迭代,Java5.0开始支持注解。而作为java中的领军框架spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制spring框架。

作为一名Java开发者,学习和使用Spring框架已经是一项必不可少的技能。而随着云计算和微服务的盛行,学习和使用SpringCloud成为了另一个必须要掌握的技能。SpringCloud是一个基于SpringBoot的用于快速构建分布式系统的开发工具集。它为开发者提供了一系列的组件,包括服务注册与发现、配置中心、负载均衡和断路器等,使得开发者在构建微

1.Spring项目的创建1.1创建Maven项目第一步,创建Maven项目,Spring也是基于Maven的。1.2添加spring依赖第二步,在Maven项目中添加Spring的支持(spring-context,spring-beans)在pom.xml文件添加依赖项。org.springframeworkspring-context5.2.3.RELEASEorg.springframeworkspring-beans5.2.3.RELEASE刷新等待加载完成。1.3创建启动类第三步,创

SpringBean的生命周期管理一、SpringBean的生命周期通过以下方式来指定Bean的初始化和销毁方法,当Bean为单例时,Bean归Spring容器管理,Spring容器关闭,就会调用Bean的销毁方法当Bean为多例时,Bean不归Spring容器管理,Spring容器关闭,不会调用Bean的销毁方法二、通过@Bean的参数(initMethod,destroyMethod)指定Bean的初始化和销毁方法1、项目结构2、PersonpublicclassPerson{publicP

spring设计模式有:1、依赖注入和控制反转;2、工厂模式;3、模板模式;4、观察者模式;5、装饰者模式;6、单例模式;7、策略模式和适配器模式等。详细介绍:1、依赖注入和控制反转: 这两个设计模式是Spring框架的核心。通过依赖注入,Spring负责管理和注入组件之间的依赖关系,降低了组件之间的耦合度。控制反转则是指将对象的创建和依赖关系的管理交给Spring容器等等。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版