package com.hhwy;

import org.springframework.context.ApplicationEvent;

public class DemoEvent extends ApplicationEvent{

    private String msg;

    private String email;

    public DemoEvent(Object source,String msg,String email) {
        super(source);
        this.msg=msg;
        this.email=email;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}
package com.hhwy.fm.projectmonthlyreport;

import com.hhwy.DemoEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * <br>描 述:
 * <br>创 建 人:jzq
 * <br>创建时间:2019/6/3 19:48
 * <br>修改备注:无
 * <br>版本:1.0.0
 */
@Component
public class DemoEventListener implements ApplicationListener<DemoEvent> {

    //使用注解@Async支持 这样不仅可以支持通过调用,也支持异步调用,非常的灵活,
    @Async
    @Override
    public void onApplicationEvent(DemoEvent event) {
        System.out.println("注册成功,发送确认邮件为:" + event.getEmail()+",消息摘要为:"+event.getMsg());
    }

}
package com.hhwy.fm.projectmonthlyreport;

import com.hhwy.DemoEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * <br>描 述:
 * <br>创 建 人:jzq
 * <br>创建时间:2019/6/3 19:52
 * <br>修改备注:无
 * <br>版本:1.0.0
 */
@Component
public class DemoEventPublisher {

    @Autowired
    private ApplicationContext applicationContext;

    public void pushlish(String msg,String mail){
        applicationContext.publishEvent(new DemoEvent(this, msg,mail));
    }

}
package com.hhwy.fm.projectmonthlyreport.controller;

import com.hhwy.fm.projectmonthlyreport.DemoEventPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <br>描 述:检测-保函管理
 * <br>创 建 人:Jinzhaoqiang
 * <br>创建时间:19.5.17
 * <br>修改备注:无
 * <br>版本:1.0.0
 */
@RestController
@RequestMapping("/test")
public class Test {

   @Autowired
   private DemoEventPublisher demoEventPublisher;

   @RequestMapping(value = "/test", method = RequestMethod.GET)
   public void test() {
      demoEventPublisher.pushlish("张三1","565792147@qq.com");
      demoEventPublisher.pushlish("张三2","565792147@qq.com");
   }
}
06-03 23:25
查看更多