Spring之事件机制详解
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
机制详解
public interface ApplicationEventMulticaster {
 /**
  * Add a listener to be notified of all events.
  * @param listener the listener to add
  */
 void addApplicationListener(ApplicationListener<?> listener);
 /**
  * Add a listener bean to be notified of all events.
  * @param listenerBeanName the name of the listener bean to add
  */
 void addApplicationListenerBean(String listenerBeanName);
 /**
  * Remove a listener from the notification list.
  * @param listener the listener to remove
  */
 void removeApplicationListener(ApplicationListener<?> listener);
 /**
  * Remove a listener bean from the notification list.
  * @param listenerBeanName the name of the listener bean to remove
  */
 void removeApplicationListenerBean(String listenerBeanName);
 /**
  * Remove all listeners registered with this multicaster.
  * <p>After a remove call, the multicaster will perform no action
  * on event notification until new listeners are registered.
  */
 void removeAllListeners();
 /**
  * Multicast the given application event to appropriate listeners.
  * <p>Consider using {@link #multicastEvent(ApplicationEvent, ResolvableType)}
  * if possible as it provides better support for generics-based events.
  * @param event the event to multicast
  */
 void multicastEvent(ApplicationEvent event);
 /**
  * Multicast the given application event to appropriate listeners.
  * <p>If the {@code eventType} is {@code null}, a default type is built
  * based on the {@code event} instance.
  * @param event the event to multicast
  * @param eventType the type of event (can be {@code null})
  * @since 4.2
  */
 void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType);
}
public interface ApplicationEventPublisher {
 /**
  * Notify all <strong>matching</strong> listeners registered with this
  * application of an application event. Events may be framework events
  * (such as ContextRefreshedEvent) or application-specific events.
  * <p>Such an event publication step is effectively a hand-off to the
  * multicaster and does not imply synchronous/asynchronous execution
  * or even immediate execution at all. Event listeners are encouraged
  * to be as efficient as possible, individually using asynchronous
  * execution for longer-running and potentially blocking operations.
  * @param event the event to publish
  * @see #publishEvent(Object)
  * @see org.springframework.context.event.ContextRefreshedEvent
  * @see org.springframework.context.event.ContextClosedEvent
  */
 default void publishEvent(ApplicationEvent event) {
  publishEvent((Object) event);
 }
 /**
  * Notify all <strong>matching</strong> listeners registered with this
  * application of an event.
  * <p>If the specified {@code event} is not an {@link ApplicationEvent},
  * it is wrapped in a {@link PayloadApplicationEvent}.
  * <p>Such an event publication step is effectively a hand-off to the
  * multicaster and does not imply synchronous/asynchronous execution
  * or even immediate execution at all. Event listeners are encouraged
  * to be as efficient as possible, individually using asynchronous
  * execution for longer-running and potentially blocking operations.
  * @param event the event to publish
  * @since 4.2
  * @see #publishEvent(ApplicationEvent)
  * @see PayloadApplicationEvent
  */
 void publishEvent(Object event);
}
领悟
应用
@Component
public class MyEventSource {
public void ccc(){
System.out.println("事件源方法");
}
}
@Component
public class MyEvent extends ApplicationEvent {
    public MyEvent(MyEventSource source) {
        super(source);
    }
    
    public void eventMethod(){
        System.out.println("事件自定义方法");
    }
}
@Component
public class MyListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
if(applicationEvent instanceof MyEvent) {
((MyEvent)applicationEvent).eventMethod();
MyEventSource eventSource = (MyEventSource) applicationEvent.getSource();
eventSource.ccc();
System.out.println("监听者发生一些改变");
}
}
}
public class MyTest {
    @Test
    public void test1() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Object o=applicationContext.getBean("myEvent");
        applicationContext.publishEvent(applicationContext.getBean("myEvent"));
       
    }
}
总结
补充:
@Component
public class MyListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
      while (true){
          System.out.println("执行业务");
      }
    }
}
作者 | 敲代码的小小酥
来源 | csdn.net/qq1309664161/article/details/119025323

评论

