package com.jwipc.log.logic.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 持有spring上下文的工具类，一个系统只能有一个SpringContextHolder。
 *  * <p>该工具类主要用于: 通过spring上下文获取bean</p>
 */
@Component
public class SpringContextHolderHandle implements ApplicationContextAware, DisposableBean {

    private static ApplicationContext applicationContext;

    /**
     * 将spring容器上下文：applicationContext注入
     */
    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        if(applicationContext != null)  throw new IllegalStateException("ApplicationContextHolder already holded 'applicationContext'.");
        applicationContext = context;
    }

    private static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    /**
     * 本类SpringContextHolder被销毁时，将spring上下文置空
     */
    @Override
    public void destroy() throws Exception {
        applicationContext = null;
    }

    /**
     * 根据class获取spring容器中的bean
     */
    public static <T> T getBean(Class<T> c){
        return applicationContext.getBean(c);
    }

    /**
     * 根据class名称获取spring中的bean
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName){
        return (T)getApplicationContext().getBean(beanName);
    }

}