zoukankan      html  css  js  c++  java
  • 不需要spring管理,自己根据名字取到对应的bean

    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);
        }
    }
  • 相关阅读:
    eclipse如何设置多个字符的智能提示
    19.面向对象的三大特征 之封装
    18代码块
    成员变量和局部变量的区别
    类与对象
    Python压缩脚本编辑
    字符串内容
    参考
    序列
    元组
  • 原文地址:https://www.cnblogs.com/xwjBlog/p/11647508.html
Copyright © 2011-2022 走看看