首页 > 编程语言 > 详细

不需要spring管理,自己根据名字取到对应的bean

时间:2019-10-10 15:33:00      阅读:175      评论:0      收藏:0      [点我收藏+]
package com.yiban.abc.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Configuration;

import java.util.Map;


@Configuration
public class SpringBeanUtil implements BeanFactoryAware {
    private static BeanFactory beanFactory;
    private static DefaultListableBeanFactory listtableBeanFactory;

    public SpringBeanUtil() {
    }

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        SpringBeanUtil.beanFactory = beanFactory;
        listtableBeanFactory = (DefaultListableBeanFactory)beanFactory;
    }

    public static Object getBean(String name) throws BeansException {
        return beanFactory.getBean(name);
    }

    public static <T> T getBean(Class<T> requiredType) throws BeansException {
        return beanFactory.getBean(requiredType);
    }

    public static <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return beanFactory.getBean(name, requiredType);
    }

    public static <T> Map<String, T> getBeansOfType(Class<T> requiredType) throws BeansException {
        return listtableBeanFactory.getBeansOfType(requiredType);
    }
}

 

不需要spring管理,自己根据名字取到对应的bean

原文:https://www.cnblogs.com/xwjBlog/p/11647508.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!