zoukankan      html  css  js  c++  java
  • Spring的事件处理

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/event-handling-in-spring.html

    Spring的核心是ApplicationContext,它负责管理beans的完整生命周期。当加载beans时,ApplicationContext发布某些类型的事件。例如,当上下文启动时,ContextStartedEvent事件触发,当上下文停止时,ContextStoppedEvent事件触发。

    通过ApplicationEvent类和ApplicationListener接口来提供在ApplicationContext中处理事件。如果一个bean实现ApplicationListener,那么每次ApplicationEvent被发布到ApplicationContext上时,那个bean会被通知。

    Spring提供了以下的标准事件:

    Spring 内置事件描述
    ContextRefreshedEvent

    ApplicationContext被初始化或刷新时,该事件被发布。这也可以在ConfigurableApplicationContext接口中使用refresh()方法来发生。

    ContextStartedEvent

    当使用ConfigurableApplicationContext接口中的start()方法启动ApplicationContext时,该事件被触发。你可以查询你的数据库,或者你可以在接受到这个事件后重启任何停止的应用程序。

    ContextStoppedEvent

    当使用ConfigurableApplicationContext接口中的stop()方法停止ApplicationContext时,该事件被触发。你可以在接受到这个事件后做必要的清理的工作。

    ContextClosedEvent

    当使用ConfigurableApplicationContext接口中的close()方法关闭ApplicationContext时,该事件被触发。一个已关闭的上下文到达生命周期末端;它不能被刷新或重启。

    RequestHandledEvent

    这是一个web-specific事件,告诉所有bean HTTP请求已经被服务。

    Spring的事件处理是单线程的,所以如果一个事件被触发,除非所有的接收者得到消息,否则这些进程被阻止,流程将不会继续。因此,如果要使用事件处理,在设计应用程序时应小心。

    监听上下文事件

    为了监听上下文事件,一个bean应该实现只有一个方法onApplicationEvent()的ApplicationListener接口。

    例子:

    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>testeventhandling</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <name>testeventhandling</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>

    HelloWorld.java:

    package com.jsoft.testspring.testeventhandling;
    
    public class HelloWorld {
        private String message;
        
        public void setMessage(String message){
            this.message = message;
        }
        
        public void getMessage(){
            System.out.println(this.message);
        }
    }

    StartedEvent.java:

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

    StoppedEvent.java:

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

    beas.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="helloWorld" class="com.jsoft.testspring.testeventhandling.HelloWorld">
            <property name="message" value="Hello World!"></property>
        </bean>
        
        <bean id="startedEvent" class="com.jsoft.testspring.testeventhandling.StartedEvent"></bean>
        <bean id="stoppedEvent" class="com.jsoft.testspring.testeventhandling.StoppedEvent"></bean>
    </beans>

    App.java:

    package com.jsoft.testspring.testeventhandling;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
            ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            
            applicationContext.start();
            
            HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
            helloWorld.setMessage("Hello World!");
            helloWorld.getMessage();
            
            applicationContext.stop();
        }
    }

    测试结果:

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

  • 相关阅读:
    Truck History(poj 1789)
    Highways poj 2485
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    115. Distinct Subsequences
    114. Flatten Binary Tree to Linked List
    113. Path Sum II
    109. Convert Sorted List to Binary Search Tree
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
  • 原文地址:https://www.cnblogs.com/EasonJim/p/6901575.html
Copyright © 2011-2022 走看看