Spring容器加载完之后执行特定任务
http://my.oschina.net/simpleton/blog/692799
有两个服务器类,需要SpringContext在InitAfterSpringContextService之前初始化:1、SpringContext
有两个服务器类,需要SpringContext在InitAfterSpringContextService之前初始化:1、SpringContext
spring容器的上线文环境
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
Java代码 package com.cdelabcare.pubservice; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Service; /** * Spring上下文环境 */ @Service public class SpringContext implements ApplicationContextAware { /** Spring应用上下文环境 */ private static ApplicationContext applicationContext; /** * 实现ApplicationContextAware接口的回调方法,设置上下文环境 * <pre> * 当spring容器加载完后会触发该方法 * </pre> * @param applicationContext {@link ApplicationContext} * @throws BeansException */ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContext.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取对象 * * @param name * @return Object 一个以所给名字注册的bean的实例 * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } /** * 获取类型为requiredType的对象 * 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException) * * @param name * bean注册名 * @param requiredType * 返回对象类型 * @return Object 返回requiredType类型对象 * @throws BeansException */ public static <T> T getBean(String name, Class<T> requiredType) throws BeansException { return applicationContext.getBean(name, requiredType); } } |
2、InitAfterSpringContextService
在Spring初始化完成之后执行一些初始化任务(需要在SpringContext执行完成之后执行)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
Java代码 package com.cdelabcare.service.serviceutil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Service; import com.cdelabcare.pubservice.SpringContext; /** * 初始化Spring完成之后的操作 * @create pengjunhao * @createDate 2016年6月16日 上午10:06:11 * @update * @updateDate */ @Service public class InitAfterSpringContextService implements ApplicationListener<ContextRefreshedEvent> { /** 日志 */ private static final Logger LOGGER = LoggerFactory .getLogger(InitAfterSpringContextService.class); /** 引入SpringContext的依赖,主要为了让SpringContext优先加载 */ @Autowired private SpringContext springContext; /* * 监听事件 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent) */ @Override public void onApplicationEvent(ContextRefreshedEvent event) { //root application context 没有parent if (event.getApplicationContext().getParent() == null) { LOGGER.info("【初始化事务....】"); // TODO something } } } |
通过使用implements ApplicationListener<ContextRefreshedEvent>可以监听spring容器是否初始化完成。