Spring容器加載完以後執行特定任務(方法)

有兩個服務器類,須要SpringContext在InitAfterSpringContextService以前初始化:java

一、SpringContextspring

spring容器的上線文環境服務器

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);
    }
}

二、InitAfterSpringContextServiceapp

在Spring初始化完成以後執行一些初始化任務(須要在SpringContext執行完成以後執行)ide

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容器是否初始化完成。ui

相關文章
相關標籤/搜索