Spring自定义事件

 如果仅仅使用Spring的内定事件,那显然是远远不够的,幸好,Spring为我们提供了中自定义发布事件的能力。下面通过例程来展示如何发布并监听自定义的事件。

在工程中,我们定义一个Animal类,为受管Bean,它具有一个Speak方法,我们要做的就是监视该方法,当用户调用该方法时触发AnimalSpeakEvent事件。具体操作如下:

新建名字为IoC_Test3.9的java工程,添加Spring开发能力后,建立ioc.test包。新建一个事件类AnimalSpeakEvent,它继承自ApplicationEvent,重载其默认的构造方法。再添加一个String成员animalName和它的Geter方法。添加一个构造方法,此方法有两个参数,一个为source,一个为animalName。代码如下:

 1 package ioc.test;
 2 
 3 import org.springframework.context.ApplicationEvent;
 4 
 5 public class AnimalSpeakEvent extends ApplicationEvent {
 6 
 7     private static final long serialVersionUID = 1L;
 8 
 9     private String animalName;
10 
11     public AnimalSpeakEvent(Object source) {
12         super(source);
13     }
14 
15     public AnimalSpeakEvent(Object source,String animalName) {
16         super(source);
17         this.animalName = animalName;
18     }
19 
20     public String getAnimalName() {
21         return animalName;
22     }
23 
24 }
25 

创建好该事件类后,就应该把它再合适的时候发布出去。既然它时一个“动物讲话事件”,那么就应该再动物“讲话”的时候发布,如何发布呢?我们知道要发布一个事件,就必须要调用ApplicationContextpublishEvent方法。要在Animal类中获得ApplicationContext的实例,就要实现ApplicationContextAware接口,代码如下:

 

 1 package ioc.test;
 2 
 3 //import省略
 4 
 5 public class Animal implements ApplicationContextAware {
 6 
 7     private ApplicationContext ac;
 8 
 9     private String name;
10 
11     private int age;
12 
13     public String speak(){
14 
15          ac.publishEvent(new AnimalSpeakEvent(this,this.name));
16         return " 我的名字是;"+this.name+",我的年龄是:"+this.age;
17    }
18 
19 
20    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
21     this.ac = arg0;
22    }
23 
24 //Getet和Seter省略
25 
26 }
27 

到目前为之,我们已经在Animal类中把事件发布出去了,现在要监听该事件的发生了,至于监听方法,前面已经演示过,直接上代码:

 

 

 1 ackage ioc.test;
 2 
 3 import org.springframework.context.ApplicationEvent;
 4 import org.springframework.context.ApplicationListener;
 5 
 6 public class AnimalEventListener implements ApplicationListener {
 7 
 8     public void onApplicationEvent(ApplicationEvent event) {
 9         if (event instanceof AnimalSpeakEvent) {
10             AnimalSpeakEvent a = (AnimalSpeakEvent) event;
11                 System.out.println("事件监听器" + this.getClass().getSimpleName()+":有一个动物在讲话!它的名字是:"+ a.getAnimalName());
12         }
13     }
14 }
15 

配置好Bean,为AnimalBean注入值,配置文件如下:

 

 

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans  …………>
 3 
 4     <bean id="Listener" class="ioc.test.AnimalEventListener" />
 5 
 6     <bean id="Animal" class="ioc.test.Animal">
 7       <property name="name" value="老虎" />
 8       <property name="age" value="5" />
 9     </bean>
10 
11 </beans>
12 

现在应该来测试一下了,看看我们的自定义的事件是不是会再动物“讲话”的时候发生,测试类TestMain代码如下: 

 

 

 1 package ioc.test;
 2 
 3 import org.springframework.context.support.AbstractApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class TestMain {
 7 
 8     public static void main(String[] args) {
 9 
10         AbstractApplicationContext ac = new ClassPathXmlApplicationContext(
11                 "applicationContext.xml");
12 
13         //从容器获取动物实例
14         Animal animal = (Animal)ac.getBean("Animal");
15 
16         //让动物讲话
17         System.out.println(animal.speak());                
18     }
19 }
20 

运行该类中的主方法,输出结果如下: 


可以看到,再动物“讲话“之前,我们的事件监听器监听到“动物讲话”,并输出了讲话动物的名字。

Spring Boot中,可以通过自定义事件来实现在特定情况下触发一些自定义的逻辑或功能。下面是一个演示如何调用自定义事件的例子: 首先,我们需要定义一个自定义事件类,例如`CustomEvent`,该类需要继承`ApplicationEvent`: ```java public class CustomEvent extends ApplicationEvent { private String message; public CustomEvent(Object source, String message) { super(source); this.message = message; } public String getMessage() { return message; } } ``` 然后,我们需要定义一个事件发布者类,例如`CustomEventPublisher`,该类负责发布自定义事件: ```java @Component public class CustomEventPublisher { @Autowired private ApplicationEventPublisher applicationEventPublisher; public void publishCustomEvent(String message) { CustomEvent customEvent = new CustomEvent(this, message); applicationEventPublisher.publishEvent(customEvent); } } ``` 接下来,我们可以在需要的地方注入`CustomEventPublisher`,并调用`publishCustomEvent`方法来发布自定义事件: ```java @Service public class MyService { @Autowired private CustomEventPublisher customEventPublisher; public void doSomething() { // 执行一些业务逻辑 // ... // 发布自定义事件 customEventPublisher.publishCustomEvent("Custom event message"); } } ``` 最后,我们需要定义一个事件监听器类,例如`CustomEventListener`,该类负责监听并处理自定义事件: ```java @Component public class CustomEventListener implements ApplicationListener<CustomEvent> { @Override public void onApplicationEvent(CustomEvent customEvent) { // 处理自定义事件 String message = customEvent.getMessage(); System.out.println("Received custom event with message: " + message); } } ``` 通过以上步骤,我们就可以在Spring Boot中调用自定义事件了。当`MyService`中的`doSomething`方法被调用时,会触发自定义事件的发布,然后`CustomEventListener`会监听到该事件并进行处理。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值