Spring(九)之事件处理
Spring的核心是ApplicationContext,它管理bean的完整生命周期。ApplicationContext在加载bean时发布某些类型的事件。例如,ContextStartedEvent当上下文启动,并公布ContextStoppedEvent当上下文停止出版。
ApplicationContext中的事件处理是通过ApplicationEvent类和ApplicationListener接口提供的。因此,如果bean实现了ApplicationListener,那么每次将ApplicationEvent发布到ApplicationContext时,都会通知该bean。
Spring的事件处理是单线程的,因此如果事件被发布,除非所有接收者都收到消息,否则流程将被阻止,流程将不会继续。因此,如果要使用事件处理,在设计应用程序时应该小心。
监听上下文事件
要监听上下文事件,bean应该实现ApplicationListener接口,该接口只有一个onApplicationEvent()方法。因此,让我们编写一个示例来查看事件如何传播以及如何使代码根据特定事件执行所需任务。
示例:
(1)编写HelloWorld.java
package com.tutorialspoint; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } }
(2)编写CStartEventHandler.java
package com.tutorialspoint; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStartedEvent; public class CStartEventHandler implements ApplicationListener<ContextStartedEvent>{ public void onApplicationEvent(ContextStartedEvent event) { System.out.println("ContextStartedEvent Received"); } }
(3)编写CStopEventHandler.java
package com.tutorialspoint; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStoppedEvent; public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{ public void onApplicationEvent(ContextStoppedEvent event) { System.out.println("ContextStoppedEvent Received"); } }
(4)编写MainApp.java
package com.tutorialspoint; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); // Let us raise a start event. context.start(); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); // Let us raise a stop event. context.stop(); } }
(5)编写Beans.xml
<?xml version = "1.0" encoding = "UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld"> <property name = "message" value = "Hello World!"/> </bean> <bean id = "cStartEventHandler" class = "com.tutorialspoint.CStartEventHandler"/> <bean id = "cStopEventHandler" class = "com.tutorialspoint.CStopEventHandler"/> </beans>
(6)运行MainApp.java中的main方法,结果如下:
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Spring(八)之基于Java配置
基于 Java 的配置 到目前为止,你已经看到如何使用 XML 配置文件来配置 Spring bean。如果你熟悉使用 XML 配置,那么我会说,不需要再学习如何进行基于 Java 的配置是,因为你要达到相同的结果,可以使用其他可用的配置。 基于 Java 的配置选项,可以使你在不用配置 XML 的情况下编写大多数的 Spring,但是一些有帮助的基于 Java 的注解,解释如下: @Configuration 和 @Bean 注解 带有@Configuration的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。@Bean注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。 演示示例: (1).编写HelloWorldConfig.java package com.tutorialspoint; import org.springframework.context.annotation.*; @Configuration public class HelloWorld...
- 下一篇
Spring(七)之基于注解配置
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入。而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身。 在 XML 注入之前进行注解注入,因此后者的配置将通过两种方式的属性连线被前者重写。 一、@Required注解 @Required注解应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。下面显示的是一个使用 @Required 注解的示例。 (1)编写Student.java package com.tutorialspoint; import org.springframework.beans.factory.annotation.Required; public class Student { private Integer age; private String name; @Required public void setAge(I...
相关文章
文章评论
共有0条评论来说两句吧...