自定义Spring event

通过Spring自定义event

首先我们定义我们的event类

package com.hyenas.spring.custom.event;



import org.springframework.context.ApplicationEvent;



public class CustomEvent extends ApplicationEvent{

    

    private static final long serialVersionUID = -82737763905791865L;



    public CustomEvent(Object source) {

        super(source);

    }



    @Override

    public String toString() {

        return "my custom event";

    }





}

然后我们实现一个ApplicationListener ,这个ApplicationListener是带泛型的,带入我们自定义的event类

package com.hyenas.spring.custom.event;



import org.springframework.context.ApplicationListener;



public class CustomEventHandler implements ApplicationListener<CustomEvent>{



    @Override

    public void onApplicationEvent(CustomEvent event) {

        System.out.println(event.toString());

    }



}

最后我们实现一个Publisher。

package com.hyenas.spring.custom.event;



import org.springframework.context.ApplicationEventPublisher;

import org.springframework.context.ApplicationEventPublisherAware;



public class CustomEventPublisher implements  ApplicationEventPublisherAware {

    

    private ApplicationEventPublisher publisher;

    

    @Override

    public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {

        this.publisher = publisher;

    }

    

    

    public void publish() {

        CustomEvent event = new CustomEvent(this);

        publisher.publishEvent(event);

    }



}

在这里我们也可以实现一个ApplicationContextAware,因为ApplicationContext接口也是继承了我们的ApplicationEventPublisher接口的。Spring源码如下

public interface ApplicationContext extends ListableBeanFactory, HierarchicalBeanFactory,

        MessageSource, ApplicationEventPublisher, ResourcePatternResolver

我们实现ApplicationContextAware的话,会往我们的程序中setApplicationContext,ApplicationContext同样可以publishEvent

 

然后,我们定义我们的spring xml文件custom-event.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"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">



   <bean id="customEventHandler" 

      class="com.hyenas.spring.custom.event.CustomEventHandler"/>



   <bean id="customEventPublisher" 

      class="com.hyenas.spring.custom.event.CustomEventPublisher"/>



</beans>

好,最后我们写一个MainApp测试一下我们的程序吧。

package com.hyenas.spring.custom.event;



import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public class MainApp {

     private static ConfigurableApplicationContext context;



    public static void main(String[] args) {

          context = new ClassPathXmlApplicationContext("custom-event.xml");

          

          CustomEventPublisher cvp = 

          (CustomEventPublisher) context.getBean("customEventPublisher");

          cvp.publish();  

          cvp.publish();

       }

}

运行结果

my custom event
my custom event

 

 

这样我们就完成了整个event从建立,到触发的过程。

 

 

你可能感兴趣的:(spring)