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

时间:2022-09-13 19:49:13
package com.yk.test.executor.processor;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {

public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getApplicationContext().getParent() == null) {// root
System.out.println("=====================================");
System.out.println("============spring 初始化完成===========");
System.out.println("============spring 初始化完成===========");
System.out.println("=====================================");
}
}

}

application.xml配置(spring3.1)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.techbirds.controller"></context:component-scan>
<!-- spirng 初始化完成执行-->
<bean class="com.yk.test.executor.processor.InstantiationTracingBeanPostProcessor"/>
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!-- 视图解释类 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="suffix" value=".jsp" />
</bean>


</beans>

注解方式

import org.apache.log4j.Logger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {

private static Logger logger =Logger.getLogger(InstantiationTracingBeanPostProcessor.class);
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//if (event.getApplicationContext().getParent() == null) {// root
try {
logger.info("★★★★★★★★★★★★★★★★★★★★★★★★★★★");
logger.info("============quartz 初始化完成===========");
logger.info("★★★★★★★★★★★★★★★★★★★★★★★★★★★");
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//}

}

}


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

@Test
public void initSpirng(){
int num = -1;
System.out.print("Please input a number:");
Scanner sc = new Scanner((System.in)); //键盘输入
num = sc.nextInt();
System.err.println(num);
}
定时任务

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