Event-driven in spring

1. The most popular understanding:

The spring ioc container applicationContext has a component called ApplicationEventMulticaster. The multicaster is a container of a bunch of listeners (applicationListener). The applicationContext executes the publishEvent() method to publish an event. It will actively for loop to traverse the multicaster. The listener in to execute the callback method of the listener.


2. What events will appear in spring:

1) When the spring ioc container is refreshed, the ContextRefreshedEvent event will be released.
2) During the process, the event will be released through the applicationContext in the programmer’s custom method.
3) ContextClosedEvent will be released when the spring ioc container is closed.


3. Steps to use spring monitoring mechanism:

1) Write listener

Method 1:
Write a listener (ApplicationListener implementation class) to listen to certain events (ApplicationEvent and its subclasses)
. Send 2:
@EventListener;
Principle: Use EventListenerMethodProcessor processor to resolve @EventListener on the method, if a component contains one @EventListener method, this component is the listener.

2) Add the listener to the container;

3) As long as there are related events released in the ioc container, we can listen to this event

For example, some system events of the ioc container:
ContextRefreshedEvent event: this event will be published when the container is refreshed (all beans are completely created)
ContextClosedEvent event: this event will be published when the container is closed
Then, the callback method of the corresponding listener will be executed.

4) How does the spring ioc container publish an event:

applicationContext.publishEvent(事件)


4. After the event is published (that is, the publishEvent() method is executed), what will happen:

3) publishEvent(new ContextRefreshedEvent(this));
a) Get the event's multicaster (distributor): getApplicationEventMulticaster()
b) The multicaster calls multicastEvent() to dispatch the event:
c) Pseudo-code logic of multicastEvent( ):

          //获取到所有订阅此事件的ApplicationListener;
		  for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
    
    
			  /*
			  a)如果有Executor,可以支持使用Executor进行异步派发;
				   Executor executor = getTaskExecutor();
			  b)否则,同步的方式直接执行listener方法:
			   invokeListener(listener, event),拿到listener回调onApplicationEvent(  )方法;
			  */

Related source code:
Insert picture description here


5. When will the event multicaster (dispatcher) be born?

1) ioc container to create an object: into the refresh () method
initApplicationEventMulticaster () in 2) refresh (), i.e., where the initialization ApplicationEventMulticaster component
  a) go to find there the container id = [applicationEventMulticaster] components, nothing exists Don't do it.
  b) If not, execute this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory), and add the event multicaster to the ioc container through the beanFactory.registerSingleton(String beanName, Object singletonObject) method.


6. When is the listener saved to the event multicaster

1) Container creation object: enter refresh();
2) registerListeners() in refresh();
simple code:

 	 //从容器中拿到所有的监听器,把他们注册到[ applicationEventMulticaster ]中;
	 String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
	 
	 //将listener注册到ApplicationEventMulticaster中
	 getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);

Guess you like

Origin blog.csdn.net/nangonghen/article/details/102059447