Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a hook to the application context initialization event?

For a regular Servlet, I guess you could declare a context listener, but for Spring MVC would Spring make this any easier?

Furthermore, if I define a context listener and then would need to access the beans defined in my servlet.xml or applicationContext.xml, how would I get access to them?

like image 814
teddy teddy Avatar asked Dec 31 '11 05:12

teddy teddy


People also ask

Which of the following interfaces have to be implemented to get application context event notification in the class?

To listen to a context event, a bean should implement the ApplicationListener interface which has just one method onApplicationEvent().

What is context started event event?

ContextStartedEvent. By calling the start() method on the ConfigurableApplicationContext, we trigger this event and start the ApplicationContext. As a matter of fact, the method is typically used to restart beans after an explicit stop. We can also use the method to deal components with no configuration for autostart.

How do I refresh application context in spring boot?

For a Spring Boot Actuator application, some additional management endpoints are available. You can use: POST to /actuator/env to update the Environment and rebind @ConfigurationProperties and log levels. /actuator/refresh to re-load the boot strap context and refresh the @RefreshScope beans.

How can we invoke an action after loading a spring context?

If you want run a job after Spring's context start, then you can use the ApplicationListener and the event ContextRefreshedEvent .


2 Answers

Spring has some standard events which you can handle.

To do that, you must create and register a bean that implements the ApplicationListener interface, something like this:

package test.pack.age;  import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent;  public class ApplicationListenerBean implements ApplicationListener {      @Override     public void onApplicationEvent(ApplicationEvent event) {         if (event instanceof ContextRefreshedEvent) {             ApplicationContext applicationContext = ((ContextRefreshedEvent) event).getApplicationContext();             // now you can do applicationContext.getBean(...)             // ...         }     } } 

You then register this bean within your servlet.xml or applicationContext.xml file:

<bean id="eventListenerBean" class="test.pack.age.ApplicationListenerBean" /> 

and Spring will notify it when the application context is initialized.

In Spring 3 (if you are using this version), the ApplicationListener class is generic and you can declare the event type that you are interested in, and the event will be filtered accordingly. You can simplify a bit your bean code like this:

public class ApplicationListenerBean implements ApplicationListener<ContextRefreshedEvent> {      @Override     public void onApplicationEvent(ContextRefreshedEvent event) {         ApplicationContext applicationContext = event.getApplicationContext();         // now you can do applicationContext.getBean(...)         // ...     } } 
like image 165
Bogdan Avatar answered Sep 20 '22 21:09

Bogdan


Since Spring 4.2 you can use @EventListener (documentation)

@Component class MyClassWithEventListeners {      @EventListener({ContextRefreshedEvent.class})     void contextRefreshedEvent() {         System.out.println("a context refreshed event happened");     } } 
like image 25
David Groomes Avatar answered Sep 22 '22 21:09

David Groomes