springEvent
public class OrderCreateEvent extends ApplicationEvent {
private JSONObject jsonObject;
public OrderCreateEvent(Object source, JSONObject jsonObject) {
super(source);
this.jsonObject = jsonObject;
}
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
}
package com.event; import org.springframework.context.ApplicationListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @Component public class MyListener implements ApplicationListener<CaseEvent> { @Override @Async public void onApplicationEvent(CaseEvent event) { System.out.println(Thread.currentThread().getName()+"发送邮件内容:" + event.getJsonObject().toJSONString()); } }
package com.service; import com.alibaba.fastjson.JSONObject; import com.event.CaseEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author yuhuiqing * @title: CaseExecuteService * @description: * @date 2020/4/11 14:56 */ @RestController public class CaseExecuteService { @Autowired private ApplicationEventPublisher applicationEventPublisher; @RequestMapping("/addOrder") public String addOrder() { System.out.println("创建订单..."); // 3.组装消息内容 JSONObject jsonObject = new JSONObject(); jsonObject.put("email", "644064779@qq.com"); jsonObject.put("phone", "15123456789"); jsonObject.put("text", "恭喜您以1399.00元购买蚂蚁课堂永久会员."); CaseEvent event = new CaseEvent(this, jsonObject); applicationEventPublisher.publishEvent(event); return "success"; } }
以上内容原创来自每特学院,每特学员学习并整理!
给学院打波广告:
http://www.mayikt.com/
浙公网安备 33010602011771号