Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute jobs just after spring loads application context?

Tags:

java

spring

I want to run some jobs just after loading the Spring context but I do not know how to do this.
Do you have any idea how to do that?

like image 727
user405458 Avatar asked Oct 22 '10 07:10

user405458


People also ask

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 .

How does Spring load application context?

ApplicationContext is a corner stone of a Spring Boot application. It represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.

Can we have multiple application context in Spring?

You can have two contexts within an application. If you have two contexts each will have its own singleton.


1 Answers

Another possibility would be to register a listener to application context events (). Basically it's the same as skaffman's solution, just implement:

org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent> 

instead of Lifecycle. It has only one method instead of three. :-)

like image 155
Philipp Jardas Avatar answered Sep 18 '22 09:09

Philipp Jardas