In the actual development, often encounter this scenario:
When something is completed, we need to inform other modules perform corresponding processing. We can send a request to a notification, but a better approach is accomplished by an event listener. Event Listener is based on the design pattern of releases - subscription, observer mode to achieve.
Next we springboot to tell the story based on a custom event listeners process.
- Configure custom event: MyEvent extends ApplicationEvent
- Configure custom listeners: MyApplicationListener implements ApplicationListener
- Event publishing: Event publishing using the spring container: context.publishEvent
2 which configure a custom listeners in four ways:
2.1: Inheritance ApplicationEvent rewrite onApplicationEvent, the current must be injected into the spring bean container.
public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } } @Component public class MyApplicationListener implements ApplicationListener{ public void onApplicationEvent(ApplicationEvent event) { System.out.println(event.getSource()); System.out.println(event.getClass().getName()); } }
2.2: Inheritance ApplicationEvent rewrite onApplicationEvent, the use of container reprint listener context.addApplicationListener (new MyApplicationListener ());
public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } } public class MyApplicationListener implements ApplicationListener{ public void onApplicationEvent(ApplicationEvent event) { System.out.println(event.getSource()); System.out.println(event.getClass().getName()); } } @SpringBootApplication(scanBasePackages = "com.example.eventListener") public class SpringbootApplication_listener { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(SpringbootApplication_listener.class); ConfigurableApplicationContext context = springApplication.run(args); context.addApplicationListener(new MyApplicationListener()); context.publishEvent(new MyEvent("测试事件监听")); // context.close(); } }
2.3: Inheritance ApplicationEvent rewrite onApplicationEvent, the configuration file, add a listener:
public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } } public class MyApplicationListener implements ApplicationListener{ public void onApplicationEvent(ApplicationEvent event) { System.out.println(event.getSource()); System.out.println(event.getClass().getName()); } }
application.yml
context:
listener:
classes: com.example.eventListener.MyApplicationListener
2.4: utilizing annotation @EventListener, can customize multiple methods, does not require class inherits the ApplicationEvent, current must be injected into the spring type container.
public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } } @Component public class MyApplicationListener { @EventListener public void onApplicationEvent11(ApplicationEvent event) { System.out.println(event.getSource()); System.out.println(event.getClass().getName()); } }