通过类名获取spring里的Bean

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

public class GetBeanFactory implements BeanFactoryAware  {
    private static BeanFactory beanFactory;  
    
    public static Object getSpringEntry(String beanId){
          return beanFactory.getBean(beanId);  
    }
    public static <T>T getSpringEntry(String beanId,Class<T>clazz){
        return clazz.cast(getSpringEntry(beanId));  
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        GetBeanFactory.beanFactory = beanFactory;  
        
    } 

}

 

posted @ 2016-05-29 15:32  吐槽村  阅读(2367)  评论(0编辑  收藏  举报