preface

  • From today on, let’s learn a design pattern every day. How will I implement this design pattern from principle to code implementation to Spring

define

  • There are one-to-many or one-to-one dependencies between objects, and when an object changes state, the dependent object is notified and updated automatically. MQ is essentially an observer model where publishers publish information, subscribe to receive information, subscribe to receive information, and don’t subscribe to receive information.

advantages

  • 1. Observer and observed are abstractly coupled.
  • 2. Create a trigger mechanism.

disadvantages

  • 1. If an observed object has many direct and indirect observers, it can be time-consuming to notify all observers.
  • 2. If there is a circular dependency between the observation and the observation target, the observation target will trigger a circular call between them, which may cause the system to crash.

Code implementation

  • AbstrackInfo.java
Public abstract class AbstrackInfo {private Clock Clock; abstract void message(); }Copy the code
  • EatInfo.java
Public class abstrinfo extends AbstrackInfo {@override public void message(){system.out.println (" we eat! "); ); }}Copy the code
  • SleepInfo.java
Public class SleepInfo extends AbstrackInfo{@override public void message(){system.out.println (" SleepInfo "){@override public void message(){system.out.println (" SleepInfo "); ); }}Copy the code
  • Clock.java
Public class Clock {private List<AbstrackInfo> infos = new ArrayList<AbstrackInfo>(); Public void say(){system.out.println (" work! ); / / notice the update (); } // Add an object to be notified public void addInfo(AbstrackInfo info){infos.add(info); } public void update(){for (AbstrackInfo info: infos) {info.message();} public void update(){AbstrackInfo info: infos; } } public static void main(String[] args) { Clock clock = new Clock(); AbstrackInfo eat = new EatInfo(); AbstrackInfo sp = new SleepInfo(); clock.addInfo(eat); clock.addInfo(sp); clock.say(); }}Copy the code

Spring Observer pattern

  • The ApplicationContext event mechanism is implemented by the observer design pattern. The ApplicationEvent class and the ApplicationListener interface enable the ApplicationContext event processing.
  • If there is an AppliListener Bean in the container, the ApplicationListener Bean will automatically be fired whenever ApplicationContext publishes an ApplicationEvent. This event mechanism must be triggered by the program display.
  • Spring has some built-in times that emit event actions when an operation is complete, such as listening for contextRefreshedEvent events, which are triggered when all beans are initialized and loaded successfully. Implementing the ApplicationListener interface can receive listening actions and then write its own logic.
  • The same time can be customized, listening can also be customized, completely according to their own business logic to deal with.
  • Object description
    • 1.ApplicationContext container object
    • 2.ApplicationEvent (ContextRefreshedEvent container refresh event)
    • 3.ApplicationListener Event listener object

ApplicationContext event listener

  • When the Bean object in the ApplicationContext is initialized, you can be notified by listening on ContextRefreshedEvent.
  • 1. Create ApplicationContextListener monitoring object
    • ApplicationEventListener.java
Public Class ApplicationEventListener implements ApplicationListener<ContextRefreshedEvent> {// The method is triggered when a specified event occurs @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { ApplicationContext act = contextRefreshedEvent.getApplicationContext(); System.out.println(" Listen until container initialization is complete!" ); // Start -> load the corresponding data from the database -> fill Redis}}Copy the code
  • 2. Add the object to the container
<bean id="applicationEventListener" class="com.itheima.spring.ApplicationEventListener"/>
Copy the code
  • 3. The test
    • SpringContextTest.java
public class SpringContextTest{ public static void main(String[] args) { ApplicationContext act = new ClassPathXmlApplicationContext("spring-event.xml"); }}Copy the code

Custom listening events

  • Custom listening events can listen for container changes and also locate the specified event object precisely
  • Define the event listener object MessageEventListener
Public class MessageEventListener implements ApplicationListener {// The listener is triggered by @override public void OnApplicationEvent (ApplicationEvent ApplicationEvent) {system.out.println (" I heard the event! ); }}Copy the code
  • Define the event object: MessageEvent
Public class MessageEvent extends ApplicationEvent {/*** * can be used to pass data */ public MessageEvent(Object source) { super(source); }}Copy the code
  • Adds objects to the container
<bean id="messageEventListener" class="com.itheima.spring.MessageEventListener"/>
Copy the code
  • Test SpringContextTest. Java
public class SpringContextTest { public static void main(String[] args) { ApplicationContext act = new ClassPathXmlApplicationContext("spring-event.xml"); // Add a custom event act.publishevent (new MessageEvent(" Hello!") )); }}Copy the code