2010-10-22 38 views

回答

2

谢谢大家的回复。 事实上,我错过了我的问题中的一个小细节,我想在加载应用程序上下文后运行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> 

再次感谢你的帮助,我很抱歉,如果问题不是很清楚“:(

5

您可以编写一个实现org.springframework.context.Lifecycle接口的bean类。将此Bean添加到您的上下文中,一旦该上下文启动完成,将由容器调用方法start()

+1

感谢您的快速回复 我试过了,我会告诉你它是否有效 – user405458 2010-10-22 08:04:39

+7

这是不正确的。 '生命周期'是为了显式调用。只有'SmartLifecycle'会被Spring框架自动调用。 – mrembisz 2012-09-06 08:28:32

22

另一种可能性是将监听器注册到应用程序上下文事件()。基本上它与skaffman的解决方案一样,只是执行:

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

而不是生命周期。它只有一种方法而不是三种。 :-)

+5

请注意,由于您使用的是Spring MVC,因此您将得到2个刷新事件。看到这篇文章的更多信息:http://stackoverflow.com/questions/6164573/why-is-my-spring-contextrefreshed-event-called-twice/6165557#6165557 – sourcedelica 2011-06-01 19:04:26

+1

你可以找到刷新根上下文的事件测试是否event.getApplicationContext()。getRoot()== null' – 2013-12-07 12:54:54

+1

@Philipp - 你的意思是说'event.getApplicationContext()。getParent()== null'? – htompkins 2016-06-22 18:43:56

15

如果你想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 

    } 
} 
+0

关于它的蹩脚的事情是,你不能听多个事件... – kboom 2014-08-16 13:18:35

+1

不知道你的意思。你可以用你想要的不同事件类多次实现接口,或者只是实现'ApplicationListener',在这种情况下你将得到所有的事件。 – 2015-01-31 14:41:32

5

使用@PostConstruct注解。比你可以混合使用任何的工作特性,并保证运行甲基od在负载上下文中。

+3

值得一提的是它是一个javax注释,而不是Spring特有的注释。 – kboom 2014-02-27 19:28:20

+10

'@ PostConstruct'只保证自动装配在这个bean中完成。如果你想调用自动装配依赖关系的方法,不要使用'@ PostConstruct',因为不能保证自动装配在这些依赖关系中完成。 – 2015-10-22 19:07:17

+0

@ Mr.Radar - 我认为你是部分错误。注入时依赖关系是完全连接的,Spring命令以一种从没有任何依赖关系的方式开始接线。但是整个上下文还没有完全初始化,因此当存在任何间接级别时系统可能会表现出不可预测的行为(如在注入的应用程序上下文中使用事件或手动搜索bean) – wciesiel 2016-05-13 20:37:30

相关问题