zoukankan      html  css  js  c++  java
  • Spring中实现自定义事件

    原理:

    1. 通过扩展ApplicationEvent,创建一个事件类CustomEvent。这个类必须定义一个默认的构造函数,它应该从ApplicationEvent类中继承的构造函数。
    2. 一旦定义事件类,你可以从任何类中发布它,假定EventClassPublisher实现了ApplicationEventPublisherAware。你还需要在XML配置文件中声明这个类作为一个bean,之所以容器可以识别bean作为事件发布者,是因为它实现了ApplicationEventPublisherAware接口。
    3. 发布的事件可以在一个类中被处理,假定EventClassHandler实现了ApplicationListener接口,而且实现了自定义事件的onApplicationEvent方法。

    例子:

    pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.jsoft.testspring</groupId>
      <artifactId>testcustomevent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>testcustomevent</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
         
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        
      </dependencies>
    </project>

    CustomEvent.java:

    package com.jsoft.testspring.testcustomevent;
    
    import org.springframework.context.ApplicationEvent;
    
    public class CustomEvent extends ApplicationEvent {
    
        public CustomEvent(Object source) {
            super(source);
            // TODO Auto-generated constructor stub
        }
        
        public String toString(){
            return "My Custom Event";
        }
    
    }

    CustomEventPublisher.java:

    package com.jsoft.testspring.testcustomevent;
    
    import org.springframework.context.ApplicationEventPublisher;
    import org.springframework.context.ApplicationEventPublisherAware;
    
    public class CustomEventPublisher implements ApplicationEventPublisherAware {
    
        private ApplicationEventPublisher publisher;
        
        @Override
        public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
            // TODO Auto-generated method stub
            this.publisher = applicationEventPublisher;
        }
        
        public void publish(){
            CustomEvent ce = new CustomEvent(this);
            publisher.publishEvent(ce);
        }
    
    }

    CustomEventHandler.java:

    package com.jsoft.testspring.testcustomevent;
    
    import org.springframework.context.ApplicationListener;
    
    public class CustomEventHandler implements ApplicationListener<CustomEvent> {
    
        @Override
        public void onApplicationEvent(CustomEvent event) {
            // TODO Auto-generated method stub
            System.out.println(event.toString());
        }
    
    }

    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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd">
            
        <bean id="customEventHandler" class="com.jsoft.testspring.testcustomevent.CustomEventHandler"></bean>
        
        <bean id="customEventPublisher" class="com.jsoft.testspring.testcustomevent.CustomEventPublisher"></bean>
    </beans>

    App.java:

    package com.jsoft.testspring.testcustomevent;
    
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            CustomEventPublisher publisher = (CustomEventPublisher)context.getBean("customEventPublisher");
            publisher.publish();
            publisher.publish();
        }
    }

    测试结果:

    总结:

    此实例中主要理解为:1、事件发布;2、事件监听;3、实现事件处理。

    beans中主要是事件处理类的初始化和事件发布类的初始化。

    测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test15

  • 相关阅读:
    Java集合详解1:一文读懂ArrayList,Vector与Stack使用方法和实现原理
    初探Java设计模式5:一文了解Spring涉及到的9种设计模式
    初探Java设计模式4:一文带你掌握JDK中的设计模式
    初探Java设计模式3:行为型模式(策略,观察者等)
    初探Java设计模式2:结构型模式(代理模式,适配器模式等)
    初探Java设计模式1:创建型模式(工厂,单例等)
    [转] <context-param>与<init-param>的区别与作用
    [转]servlet配置中init-param
    [转]Spring 中的p标签
    [转]Spring Security学习总结二
  • 原文地址:https://www.cnblogs.com/EasonJim/p/6901706.html
Copyright © 2011-2022 走看看