spring event (@EventListener)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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;

/**
 * 
描 述: *
创 建 人:jzq *
创建时间:2019/6/3 19:48 *
修改备注:无 *
版本:1.0.0 */ @Component public class DemoEventListener implements ApplicationListener { //使用注解@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;

/**
 * 
描 述: *
创 建 人:jzq *
创建时间:2019/6/3 19:52 *
修改备注:无 *
版本: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.*;

/**
 * 
描 述:检测-保函管理 *
创 建 人:Jinzhaoqiang *
创建时间:19.5.17 *
修改备注:无 *
版本: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","[email protected]"); demoEventPublisher.pushlish("张三2","[email protected]"); } }

转载于:https://my.oschina.net/u/2351011/blog/3057903

你可能感兴趣的:(spring event (@EventListener))