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?

- 2,937
- 4
- 37
- 61

- 1,107
- 3
- 24
- 38
5 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. :-)

- 3,222
- 3
- 29
- 42
-
5Note that you will get 2 refreshed events since you are using Spring MVC. See this post for more info: http://stackoverflow.com/questions/6164573/why-is-my-spring-contextrefreshed-event-called-twice/6165557#6165557 – sourcedelica Jun 01 '11 at 19:04
-
1You can find the event that refreshes the root context by testing whether `event.getApplicationContext().getRoot() == null` – Philipp Jardas Dec 07 '13 at 12:54
-
1@Philipp - Did you mean to say `event.getApplicationContext().getParent() == null` ? – htompkins Jun 22 '16 at 18:43
-
1Absolutely, @htompkins, thanks. Two-and-a-half years later. :-) Can't seem to be able to edit my comment though... – Philipp Jardas Jun 22 '16 at 20:15
If you want run a job after Spring's context start, then you can use the ApplicationListener
and the event ContextRefreshedEvent
.
public class YourJobClass implements ApplicationListener<ContextRefreshedEvent>{
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent ) {
// do what you want - you can use all spring beans (this is the difference between init-method and @PostConstructor where you can't)
// this class can be annotated as spring service, and you can use @Autowired in it
}
}

- 1
- 1

- 509
- 7
- 16
-
1Not sure what you mean. You can just implement the interface multiple times with the different event classes you want, or just implement `ApplicationListener` in which case you will get all the events. – Tom Jenkinson Jan 31 '15 at 14:41
You can write a bean class that implements the org.springframework.context.Lifecycle
interface. Add this bean to your context, and the start()
method will be invoked by the container once that context has finished starting up.

- 398,947
- 96
- 818
- 769
-
1thank you for the quick reply I tried that and I'll tell you if it works – user405458 Oct 22 '10 at 08:04
-
7This is not true. `Lifecycle` is meant for explicit invoking. Only `SmartLifecycle` will be invoked by the spring framework automatically. – mrembisz Sep 06 '12 at 08:28
Use the @PostConstruct
annotation. Than you can combine any job properties and guarantee to run your method on the load context.

- 59,111
- 13
- 86
- 103

- 47
- 1
- 1
-
3
-
15`@PostConstruct` only guarantees that the autowiring is finished in this bean. Do not use `@PostConstruct` if you want to call methods of autowired dependencies, since there is no guarantee that autowiring is finished in these dependencies. – Mr.Radar Oct 22 '15 at 19:07
-
@Mr.Radar - I think you're partially wrong. Dependencies are fully wired when injected, Spring orders wiring in a way that starts from those not having any dependencies. But entire context was not yet fully initialized therefore system might behave unpredictably when any level of indirection exists (like using events or manual searching for beans in injected application context) – wciesiel May 13 '16 at 20:37
-
@wciesiel I am triggering some actions now in PostConstruct, and the reason I am googling this up is that some other beans (not the one which contains PostConstruct), are not fully prepared yet. So, Mr.Radar is right at least in that PostConstruct is not a good place for this. – Kirill G. Aug 15 '19 at 12:31
thank you all for your reply. In fact I missed a little detail in my question, I wanted to run Quartz Job just after loading the application context.. I tried the solution stakfeman, but I had some problems running the Quartz Jobs. Finally I found that solution: Use Quartz within Spring ,here is the code:
<!--
===========================Quartz configuration====================
-->
<bean id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="processLauncher" />
<property name="targetMethod" value="execute" />
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- see the example of method invoking job above -->
<property name="jobDetail" ref="jobDetail" />
<!-- 10 seconds -->
<property name="startDelay" value="10000" />
<!-- repeat every 50 seconds -->
<property name="repeatInterval" value="50000" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
thank you again for the help and I apologize if the question was not very clear ':(

- 1,107
- 3
- 24
- 38