基础支持
先来看看支持哪些默认事件
程序1(Service)
先看看程序
ApplicationEventPublisher这个是spring的东西,需要注入来进行发送 因为实现了ApplicationEventPublisherAware所以setApplicationEventPublisher这个方法会自动帮我们调用,拿到广播发送者
/** * @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; } }

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runanywhere”哲学。1)itusesbytbytybytecebytecodethatrunsonanyjvm-platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中编写平台特定代码的原因包括访问特定操作系统功能、与特定硬件交互和优化性能。1)使用JNA或JNI访问Windows注册表;2)通过JNI与Linux特定硬件驱动程序交互;3)通过JNI使用Metal优化macOS上的游戏性能。尽管如此,编写平台特定代码会影响代码的可移植性、增加复杂性、可能带来性能开销和安全风险。

Java将通过云原生应用、多平台部署和跨语言互操作进一步提升平台独立性。1)云原生应用将使用GraalVM和Quarkus提升启动速度。2)Java将扩展到嵌入式设备、移动设备和量子计算机。3)通过GraalVM,Java将与Python、JavaScript等语言无缝集成,增强跨语言互操作性。

Java的强类型系统通过类型安全、统一的类型转换和多态性确保了平台独立性。1)类型安全在编译时进行类型检查,避免运行时错误;2)统一的类型转换规则在所有平台上一致;3)多态性和接口机制使代码在不同平台上行为一致。

JNI会破坏Java的平台独立性。1)JNI需要特定平台的本地库,2)本地代码需在目标平台编译和链接,3)不同版本的操作系统或JVM可能需要不同的本地库版本,4)本地代码可能引入安全漏洞或导致程序崩溃。

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版