当spring 容器初始化完成后执行某个方法

[java]  view plain  copy


  1. package com.yk.test.executor.processor;  
  2.   
  3. import org.springframework.context.ApplicationListener;  
  4. import org.springframework.context.event.ContextRefreshedEvent;  
  5.   
  6. public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {  
  7.   
  8.     public void onApplicationEvent(ContextRefreshedEvent event) {  
  9.         if (event.getApplicationContext().getParent() == null) {// root  
  10.             System.out.println("=====================================");  
  11.             System.out.println("============spring 初始化完成===========");  
  12.             System.out.println("============spring 初始化完成===========");  
  13.             System.out.println("=====================================");  
  14.         }  
  15.     }  
  16.       
  17. }  

application.xml配置(spring3.1)

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  8.   
  9.     <!-- 自动扫描的包名 -->  
  10.     <context:component-scan base-package="com.techbirds.controller"></context:component-scan>  
  11.     <!-- spirng 初始化完成执行-->  
  12.     <bean class="com.yk.test.executor.processor.InstantiationTracingBeanPostProcessor"/>    
  13.     <!-- 默认的注解映射的支持 -->  
  14.     <mvc:annotation-driven />  
  15.     <!-- 视图解释类 -->  
  16.     <bean  
  17.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  18.         <property name="prefix" value="/WEB-INF/jsp/" />  
  19.         <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->  
  20.         <property name="suffix" value=".jsp" />  
  21.     </bean>  
  22.   
  23.   
  24. </beans>  

注解方式

[java]  view plain  copy
  1. import org.apache.log4j.Logger;  
  2. import org.quartz.Scheduler;  
  3. import org.quartz.SchedulerException;  
  4. import org.quartz.impl.StdSchedulerFactory;  
  5. import org.springframework.context.ApplicationListener;  
  6. import org.springframework.context.event.ContextRefreshedEvent;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Component  
  10. public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {  
  11.   
  12.     private static Logger logger =Logger.getLogger(InstantiationTracingBeanPostProcessor.class);  
  13.     @Override  
  14.     public void onApplicationEvent(ContextRefreshedEvent event) {  
  15.         //if (event.getApplicationContext().getParent() == null) {// root  
  16.             try {  
  17.                 logger.info("★★★★★★★★★★★★★★★★★★★★★★★★★★★");  
  18.                 logger.info("============quartz 初始化完成===========");  
  19.                 logger.info("★★★★★★★★★★★★★★★★★★★★★★★★★★★");  
  20.                 Scheduler scheduler = new StdSchedulerFactory().getScheduler();  
  21.                 scheduler.start();  
  22.             } catch (SchedulerException e) {  
  23.                 // TODO Auto-generated catch block  
  24.                 e.printStackTrace();  
  25.             }             
  26.         //}  
  27.           
  28.     }  
  29.       
  30. }  


单元测试时,初始化spring容器,阻塞进程结束,测试定时任务

[java]  view plain  copy
  1. @Test  
  2. public void initSpirng(){  
  3.     int num = -1;  
  4.     System.out.print("Please input a number:");  
  5.     Scanner sc = new Scanner((System.in));  //键盘输入  
  6.     num = sc.nextInt();  
  7.     System.err.println(num);  
  8. }  
定时任务

[java]  view plain  copy
  1. @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次  
  2. public void myTest(){  
  3.     System.out.println("...spring容器不关闭,这样子就可以看定时任务效果...");  
  4. }  





http://blog.csdn.net/u010081710/article/details/50015171

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值