Home > AI > Backend > SpringBoot >

ApplicationListener

Using ApplicationListener to listen multiple events.

Example code:

@SpringBootApplication
public class DemoApplication {


	public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
                DemoApplication.class);
        context.stop();
        context.start();
        context.close();

	    SpringApplication.run(DemoApplication.class, args);
	}


    @Bean
    AListenerBean listenerBean() {
        return new AListenerBean();
    }
    private static class AListenerBean implements ApplicationListener<ApplicationEvent> {

        @Override
        public void onApplicationEvent (ApplicationEvent event) {
            System.out.println("[Shark] event received: "+event);
        }
    }
}

Leave a Reply