package cn.hnthyy.thmz.Utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;


@Service
public class SpringBeanUtils implements ApplicationContextAware {
    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context=applicationContext;
    }
    public static <T> T getBean(Class<T> requiredType){

        if(context==null){
            throw new IllegalStateException("spring 环境没有启动！");
        }
       return context.getBean(requiredType);
    }
    public static <T> T getBean(String beanName,Class<T> requiredType){

        if(context==null){
            throw new IllegalStateException("spring 环境没有启动！");
        }
        return context.getBean(beanName,requiredType);

    }

    public static ApplicationContext getContext(){


        if(context==null){
            throw new IllegalStateException("spring 环境没有启动！");
        }
        return context;
    }
}