package com.itheima.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/*****
 * @Author: http://www.itheima.com
 * @Description: com.itheima.util.Beans
 ****/
@Component
public class Beans implements ApplicationContextAware {

    @Autowired
    private Environment env;

    //容器对象
    private static ApplicationContext act;

    //服务唯一标识符
    public static String unique;

    /***
     * 根据ID获取Bean
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name){
        return (T) act.getBean(name);
    }


    /***
     * 根据ID获取Bean
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<?> clazz){
        return (T) act.getBean(clazz);
    }

    /***
     * 获取容器
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        act = applicationContext;
        //获取唯一标识符
        unique = env.getProperty("unique");
        System.out.println(unique);
    }
}
