您的位置:首页 > 编程语言 > Java开发

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

2015-11-24 16:49 639 查看
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();
}
//}

}

}


转载文章 :http://blog.csdn.net/fatherican/article/details/9130165
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: