springboot13 发布和监听事件
生活随笔
收集整理的這篇文章主要介紹了
springboot13 发布和监听事件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spring中的事件驅動模型Event(也叫發布訂閱模式),是觀察者模式的一個典型的應用
好處:業務解耦,在不影響原來業務邏輯的情況下,加入其它業務
場景:
app上線后已實現用戶注冊功能,現需要在用戶注冊成功后給用戶發送短信提示。
因為怕發送短信的代碼邏輯發送異常會影響以前的用戶注冊功能,所以將發送短信功能設置成監聽事件,在用戶注冊成功后發布一個事件,在事件監聽器中監聽到該事件后處理發送短信邏輯。如果發送短信發生異常,不會影響用戶注冊功能。
代碼實現:
新建maven項目,引入依賴:
<!--父依賴包--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.2.RELEASE</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>項目其他結構:
User
public class User {private int id;private String name;public User() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;} }UserService
import com.mlxs.springboot.event.bean.User; import com.mlxs.springboot.event.event.UserRegisterEvent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service;import java.util.ArrayList; import java.util.List;/*** UserService類描述:** @author yangzhenlong* @since 2018/1/12*/ @Service public class UserService {@Autowiredprivate ApplicationContext applicationContext;private List<User> userList = new ArrayList<>();public User register(User user) {userList.add(user);//發布事件applicationContext.publishEvent(new UserRegisterEvent(this, user));return user;}public List<User> getAll(){return userList;} }UserRest
@RestController public class UserRest {@Autowiredprivate UserService userService;@RequestMapping("/user/all")List<User> getAll(){return userService.getAll();}@RequestMapping("/user/register")User register(@RequestBody User user){return userService.register(user);} }StartApp
@SpringBootApplication public class StartApp {public static void main(String[] args) {SpringApplication.run(StartApp.class, args);} } UserRegisterEvent 用戶注冊事件 public class UserRegisterEvent extends ApplicationEvent {private Object source;private User user;public UserRegisterEvent(Object source, User user) {super(source);this.user = user;}@Overridepublic Object getSource() {return source;}public void setSource(Object source) {this.source = source;}public User getUser() {return user;}public void setUser(User user) {this.user = user;} } UserRegisterEventListener 用戶事件監聽 @Component public class UserRegisterEventListener {@EventListenerpublic void register(UserRegisterEvent event){User user = event.getUser();System.out.println("-----------監聽到用戶注冊事件------,給用戶【" + user.getName() + "】發送短信...");} }?
?
啟動項目,調用 register接口,注冊2個用戶:
{"id":1,"name":"hello" } ------------------------------ {"id":2,"name":"劉德華" }查看控制臺
?
轉載于:https://www.cnblogs.com/yangzhenlong/p/8277022.html
總結
以上是生活随笔為你收集整理的springboot13 发布和监听事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: django项目日志
- 下一篇: Servlet笔记2-文件上传