background
- ApplicationContext event mechanism is the observer design pattern realized by ApplicationEvent ApplicationListener classes and interfaces can be implemented ApplicationContext event processing;
- If ApplicationListener presence of Bean container when calling publishEvent method ApplicationContext, corresponding Bean will be triggered.
spring built-in event
Built-in event | description |
ContextRefreshedEvent | ApplicationContext is initialized or refreshed when the event is triggered. This can also occur with refresh () method in ConfigurableApplicationContext interface. Initialization herein means: Bean all been successfully loaded, and is detected after treatment Bean activated, all the Singleton Bean are pre-instantiated, the container is ready for use the ApplicationContext |
ContextStartedEvent | When ConfigurableApplicationContext (ApplicationContext sub-interface) interface start () method to start ApplicationContext, the event is published. You can survey your database, you can restart or stop any applications received after this incident. |
ContextStoppedEvent | When using the stop (ConfigurableApplicationContext interface) stop ApplicationContext, published this event. You can do the necessary clean-up work after receiving this event. |
ContextClosedEvent | When ConfigurableApplicationContext interface close () method to close ApplicationContext, the event is published. Context reaches a closed end of the life cycle; it can not be restarted or refreshed. |
RequestHandledEvent | This is a web-specific event telling all bean HTTP request has been serviced. DispatcherServlet can only be applied using a Web application. When used as a front end of the MVC controller Spring, Spring process ends when the user requests, the system will automatically trigger the event. |
The same event can customize, monitor can also be customized , fully processed according to their own business logic.
ApplicationListener source
@FunctionalInterface public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { /** * Handle an application event. * @param event the event to respond to */ void onApplicationEvent(E event); }
ContextRefreshedEvent monitor events
以Spring的内置事件ContextRefreshedEvent为例,当ApplicationContext被初始化或刷新时,会触发ContextRefreshedEvent事件,下面我们就实现一个ApplicationListener来监听此事件的发生。
@Component public class MyListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println("容器中初始化Bean数量:" + event.getApplicationContext().getBeanDefinitionCount()); } }
启动服务,可以看到
至此,便完成了一个事件及监听类的实现和实例化。
自定义事件及监听,以发送邮件为例
- 自定义邮件通知事件类:EmailEvent
package com.lw.coodytest.event; import org.springframework.context.ApplicationEvent; /** * @Classname EmailEvent * @Description 邮件通知事件 * @Author lw * @Date 2019-12-20 11:05 */ public class EmailEvent extends ApplicationEvent { private String email; private String content; public EmailEvent(Object source){ super(source); } public EmailEvent(Object source, String email, String content){ super(source); this.email = email; this.content = content; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
- 自定义邮件通知监听类:EmailListener
package com.lw.coodytest.listener; import com.lw.coodytest.event.EmailEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; /** * @Classname EmailListener * @Description 邮件通知监听 * @Author lw * @Date 2019-12-20 11:10 */ @Component public class EmailListener implements ApplicationListener<EmailEvent> { @Override public void onApplicationEvent(EmailEvent emailEvent) { System.out.println("邮件地址:" + emailEvent.getEmail()); System.out.println("邮件内容:" + emailEvent.getContent()); } }
- 单元测试类
package com.lw.coodytest.junit; import com.lw.coodytest.event.EmailEvent; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.context.WebApplicationContext; /** * @Classname ListenerTest * @Description 监听测试类 * @Author lw * @Date 2019-12-20 11:12 */ @RunWith(SpringRunner.class) @SpringBootTest public class ListenerTest { @Autowired private WebApplicationContext webapplicationcontext; @Test public void testListener(){ EmailEvent emailEvent = new EmailEvent("object", "[email protected]", "###listener"); webapplicationcontext.publishEvent(emailEvent); } }
监听器通过@Component注解进行实例化,并在onApplicationEvent中打印相关信息
- 执行测试类,可以看到
至此,便完成了一个自定义事件及监听类的实现和实例化。
特别注意:
不管是内置监听还是外部自定义监听一定要把实现ApplicationListener的类定义成一个bean才行,可以通过注解@Component或者在bean.xml中定义来实现。