package com.chinacreator.video.util;

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

@Component

public class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    @Override

    public void setApplicationContext(ApplicationContext applicationContext) throws

            BeansException {

// TODO Auto-generated method stub

        SpringBeanUtil.applicationContext = applicationContext;

    }


    /**

     * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.

     */

    @SuppressWarnings("unchecked")

    public static <T> T getBean(String name) {

        if(name == null || applicationContext == null)

            return null;

        return (T) applicationContext.getBean(name);

    }


    /**

     * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.

     */

    public static <T> T getBean(Class<T> clazz) {

        return applicationContext.getBean(clazz);

    }


}