package com.cirle.scientific.utils;/*
 *24901
 *2022/10/20
 *18:39
 */

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

/**
 * @author 24901
 */
@Component
public class StringContextUtil implements ApplicationContextAware {

    /**
     * 获取String应用程序上下文对象
     * 基于此对象,可以获取Ioc容器内部对象
     */
    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        StringContextUtil.applicationContext =applicationContext;
    }

    /**
     * 调用applicationContext,获取容器内相同类型的对象
     */
    public static  <T> T getBean(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }
    /**
     * 调用applicationContext,获取容器内部指定名字对象
     */
    public static <T> T getBean(String name , Class<T> clazz){
        return applicationContext.getBean(name,clazz);
    }
}
