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"); } }