关闭

1. Spring 4.2.3前瞻-更简单的Application Event

标签: 框架spring mvcmavenspring
159人阅读 评论(0) 收藏 举报
分类:

1.1 Application Event

1.2 示例

  • 包依赖

<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.wisely</groupId>
  <artifactId>spring4-2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring4-2-3</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

 <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.3.RC1</version>
    </dependency>
</dependencies><repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
</project>

  • 编写自定义的Application Event
  • package com.wisely.spring4_2.3.event;
    
    import org.springframework.context.ApplicationEvent;
    
    public class DemoEvent extends ApplicationEvent{
        private static final long serialVersionUID = 1L;
        private String msg;
    
        public DemoEvent(Object source,String msg) {
            super(source);
            this.msg = msg;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
    }
  • 编写监听类
  • package com.wisely.spring4_2.3.event;
    
    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Component;
    
    @Component
    public class DemoListener {
        @EventListener //注意此处
        public void handleDemoEvent(DemoEvent event){
            System.out.println("我监听到了pulisher发布的message为:"+event.getMsg());
    
        }
    
    }
    
  • 测试
  • package com.wisely.spring4_2.3.event;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.stereotype.Component;
    @Component
    public class Main {
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context = 
                      new AnnotationConfigApplicationContext("com.wisely.spring4_2.event");
            Main main =context.getBean(Main.class);
            main.pulish(context);
            context.close();
        }
     public void pulish(AnnotationConfigApplicationContext context){
            context.publishEvent(new DemoEvent(this, "22"));
        }
    }
  • 输出结果

    我监听到了pulisher发布的message为:22
  • 0
    0

    查看评论
    * 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
      个人资料
      • 访问:31831次
      • 积分:831
      • 等级:
      • 排名:千里之外
      • 原创:9篇
      • 转载:8篇
      • 译文:44篇
      • 评论:2条
      最新评论