Java 如何在 spring 加载应用程序上下文后立即执行作业?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3994860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute jobs just after spring loads application context?
提问by user405458
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?
我想在加载 Spring 上下文后立即运行一些作业,但我不知道如何执行此操作。
你知道怎么做吗?
采纳答案by user405458
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 Job.. 我尝试了解决方案 stakfeman,但我在运行 Quartz Jobs 时遇到了一些问题。最后我找到了解决方案:在 Spring 中使用 Quartz,这是代码:
<!--
===========================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 ':(
再次感谢您的帮助,如果问题不是很清楚,我深表歉意':(
回答by skaffman
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.
您可以编写一个实现该org.springframework.context.Lifecycle
接口的 bean 类。将此 bean 添加到您的上下文中,start()
一旦该上下文完成启动,容器将调用该方法。
回答by Philipp Jardas
Another possibility would be to register a listener to application context events (). Basically it's the same as skaffman's solution, just implement:
另一种可能性是向应用程序上下文事件 () 注册一个侦听器。基本上它与skaffman的解决方案相同,只需实现:
org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>
instead of Lifecycle. It has only one method instead of three. :-)
而不是生命周期。它只有一种方法,而不是三种。:-)
回答by Dariusz Skrudlik
If you want run a job after Spring's context start, then you can use the ApplicationListener
and the event ContextRefreshedEvent
.
如果您想在 Spring 的上下文启动后运行作业,则可以使用ApplicationListener
和 事件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
}
}
回答by user2791875
Use the @PostConstruct
annotation. Than you can combine any job properties and guarantee to run your method on the load context.
使用@PostConstruct
注释。您可以组合任何作业属性并保证在加载上下文中运行您的方法。