In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the methods and steps of realizing the Observer Mode based on Spring". In the daily operation, I believe that many people have doubts about the methods and steps of realizing the Observer Mode based on Spring. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "the methods and steps of realizing the Observer Mode based on Spring". Next, please follow the editor to study!
The observer pattern defines an one-to-many dependency between objects. when the state of an object changes, all objects that depend on it are notified and updated automatically. it mainly solves the problem of notifying other associated objects of a change in the state of an object, ensuring ease of use and low coupling. A typical application scenario is that when a user registers, he or she needs to send email and coupons to the user, as shown in the following figure.
After UserService completes its user registration logic, it only needs to issue a UserRegisterEvent event without paying attention to other extension logic. Other Service can subscribe to UserRegisterEvent events and implement custom extension logic. The event mechanism of Spring is mainly composed of three parts.
ApplicationEvent: implement custom events by inheriting it. In addition, the event source can be obtained through its source property, and the occurrence time can be obtained by the timestamp property.
ApplicationEventPublisher: publish change events by implementing it.
ApplicationEventListener: by implementing it, listen for events of a specified type and respond to actions. Let's take the above user registration as an example to take a look at the code example. First define the user registration event UserRegisterEvent.
Publicclass UserRegisterEvent extends ApplicationEvent {/ * * user name * / private String username; public UserRegisterEvent (Object source) {super (source);} public UserRegisterEvent (Object source, String username) {super (source); this.username = username;} public String getUsername () {return username;}}
Then define the user registration service class and implement the ApplicationEventPublisherAware interface to inject ApplicationEventPublisher. As you can see from the following code, after the registration logic is executed, the publishEvent (ApplicationEvent event) method of ApplicationEventPublisher is called and the UserRegisterEvent event is issued.
Servicepublicclass UserService implements ApplicationEventPublisherAware {/ / private Logger logger = LoggerFactory.getLogger (getClass ()); private ApplicationEventPublisher applicationEventPublisher; @ Override public void setApplicationEventPublisher (ApplicationEventPublisher applicationEventPublisher) {this.applicationEventPublisher = applicationEventPublisher;} public void register (String username) {/ /. Execute the registration logic logger.info ("[register] [execute the registration logic of the user ({})], username); / /. Publish applicationEventPublisher.publishEvent (new UserRegisterEvent (this, username));}}
Create the mailbox Service, implement the ApplicationListener interface, set the events of interest through E generics, realize the onApplicationEvent (E event) method, and customize the monitoring UserRegisterEvent events.
@ Servicepublicclass EmailService implements ApplicationListener {/ / private Logger logger = LoggerFactory.getLogger (getClass ()); @ Override @ Async// public void onApplicationEvent (UserRegisterEvent event) {/ / logger.info ("[onApplicationEvent] [send an email to the user ({})]", event.getUsername ();}}
Create a coupon Service, which is different from the way you implement the ApplicationListener interface above. In the method, add the @ EventListener annotation and set the listening event to UserRegisterEvent. This is another way to use it.
@ Servicepublicclass CouponService {private Logger logger = LoggerFactory.getLogger (getClass ()); @ EventListener// public void addCoupon (UserRegisterEvent event) {logger.info ("[addCoupon] [issuing coupons to users ({})]", event.getUsername ();}}
Seeing here, careful students may have thought of the publish and subscribe model. In fact, the observer model is different from the publish and subscribe model. To put it simply, the publish subscription model belongs to the observer model in a broad sense. On the basis of Subject and Observer of the observer pattern, Event Channel is introduced as an intermediary to further decouple. As shown in the figure, you can see that the observer mode is lighter and is usually used on a stand-alone machine, while the publish-subscribe model is relatively heavier and is usually used in message notification scenarios in a distributed environment.
At this point, the study of "the method and steps of implementing the Observer pattern based on Spring" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.