say it up front
Why do you want to study the spring source code? I think the most NX of java is that I write the interface and you implement it, like oracle, or you want a framework like spring, and I can integrate you with any framework, so you don’t want to study the source code of the NX framework. Impulsive? O(∩_∩)O haha~.
text
event listener
Today, I will introduce the event model in the spring architecture system. Event-oriented programming can make your application more scalable, more beautiful in design, and more design sense. It is also the most common way of decoupling. First, look at the class diagram.
ApplicationListener event listener interface, implemented based on the observer pattern.
GenericApplicationListener processing is based on a common event listener interface, providing a monitoring based on event types, as shown in the following figure
boolean supportsEventType(ResolvableType eventType);
Is an improved version of SmartApplicationListener.
SmartApplicationListener event-based listener interface, as shown below
boolean supportsEventType(Class<? extends ApplicationEvent> eventType);
ApplicationListenerMethodAdapter GenericApplicationListener adapter implementation, as shown below
public class ApplicationListenerMethodAdapter implements GenericApplicationListener {
It can be seen that the adapter mode is implemented by implementing the interface. It was introduced in the previous article. Why is the implementation of the interface more scalable than the implementation of the inherited class? Java is single inheritance, and the implementation of the interface is used in this way. Multiple inheritance can be implemented indirectly, and the scalability is better.
SourceFilteringListener is implemented based on the decorator pattern of GenericApplicationListener, SmartApplicationListener, filters events from the specified event source, and calls its delegate listener to match the application event object.
GenericApplicationListenerAdapter GenericApplicationListener adapter pattern implementation.
event
ApplicationEvent event abstract class
ApplicationContextEvent Base class for application context events
ContextStoppedEvent Event raised when the application context is stopped
ContextRefreshedEvent Event raised when the ApplicationContext is initialized or refreshed
ContextStartedEvent Event raised when the application context starts
ContextClosedEvent Event raised when the application context is closed
publish and subscribe
The ApplicationEventMulticaster interface is implemented by objects that can manage multiple ApplicationListener objects and publish events to them
AbstractApplicationEventMulticaster provides basic listener registration functionality
SimpleApplicationEventMulticaster provides basic listener registration functionality, ignoring events it is not interested in
org.springframework.context.ApplicationEventPublisherAware Interface implemented by any object that any object wishes to be notified of by the ApplicationEventPublisher (usually the ApplicationContext) it is running on
EventPublicationInterceptor event publication interceptor
ApplicationEventPublisher An interface that encapsulates the event publishing function
to the end
This time, the general framework is introduced first, and the next article introduces the specific implementation source code analysis.
Long press the QR code to follow