Spring-层次性依赖查找Bean

42次阅读

共计 3127 个字符,预计需要花费 8 分钟才能阅读完成。

层次性依赖查找接口 – HierarchicalBeanFactory

  • 双亲 BeanFactory:getParentBeanFactory()

层次性查找

  • 依据 Bean 名称查找

    • 基于 containsLocalBean 办法实现
  • 依据 Bean 类型查找实例列表

    • 繁多类型:BeanFactoryUtils#beanOfType
    • 汇合类型:BeanFactoryUtils#beansOfTypeIncludingAncestors
  • 依据 Java 注解查找名称列表

    • BeanFactoryUtils#beanNamesForTypeIncludingAncestors

实体类 Rumenz.java / SuperRumenz.java

package com.rumenz;
public class Rumenz extends SuperRumenz{

    private Integer id;
    private String name;

    @Override
    public String toString() {
        return "Rumenz{" +
                "id=" + id +
                ", name='" + name + '\'' +
                "}" + super.toString();}

    public Integer getId() {return id;}

    public void setId(Integer id) {this.id = id;}

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}
}

//SuperRumenz.java

package com.rumenz;

public class SuperRumenz {

    private String key;

    public String getKey() {return key;}

    public void setKey(String key) {this.key = key;}

    @Override
    public String toString() {
        return "SuperRumenz{" +
                "key='" + key + '\'' +
                '}';
    }
}

配置类 Config.java / Config1.java

package com.rumenz;
import org.springframework.context.annotation.Bean;

public class Config {

    @Bean
    public SuperRumenz superRumenz(){SuperRumenz s=new SuperRumenz();
        s.setKey("超级管理员");
        return s;
    }
}

// Config1.java

package com.rumenz;
import org.springframework.context.annotation.Bean;

public class Config1 {

    @Bean
    public static Rumenz rumenz(){Rumenz r = new Rumenz();
        r.setId(123);
        r.setName("入门小站");
        return r;
    }
}

案例 DemoApplication.java

package com.rumenz;

public class DemoApplication {public static void main(String[] args) {AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
         ac.register(Config1.class);
         ac.refresh();
         ConfigurableListableBeanFactory beanFactory = ac.getBeanFactory();
         System.out.println("父容器"+beanFactory.getParentBeanFactory()); //null

         // 层级的 BeanFactory, 创立一个父容器
         HierarchicalBeanFactory parentBeanFactory = createBeanFactory();

         displayBean(beanFactory,"rumenz");
         displayBean(parentBeanFactory,"superRumenz");

         // 设置父容器
         beanFactory.setParentBeanFactory(parentBeanFactory);

        displayBean(beanFactory,"superRumenz");
        displayBean(parentBeanFactory,"rumenz");

        // 递归查找 Bean, 双亲委派,先去父容器查找(parentBeanFactory),找不到最初去本地容器(beanFactory)查找
        displayContainsBean(beanFactory,"superRumenz");
        displayContainsBean(parentBeanFactory,"superRumenz");

         ac.close();}
    private static void displayBean(HierarchicalBeanFactory beanFactory,String beanName){System.out.println(beanFactory.containsLocalBean(beanName));

    }

    private static void displayContainsBean(HierarchicalBeanFactory beanFactory,String beanName){System.out.println(containsBean(beanFactory,beanName));

    }

    private static boolean containsBean(HierarchicalBeanFactory beanFactory,String beanName){BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
        if(parentBeanFactory instanceof HierarchicalBeanFactory){HierarchicalBeanFactory hierarchicalBeanFactory = HierarchicalBeanFactory.class.cast(parentBeanFactory);
            if(containsBean(hierarchicalBeanFactory,beanName)){return true;}
        }
        return beanFactory.containsLocalBean(beanName);

    }

    private static HierarchicalBeanFactory createBeanFactory(){AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
          ac.register(Config.class);
          ac.refresh();
          return ac.getBeanFactory();}
}

输入

 父容器 null
true
true
false
false
true
true

源码:https://github.com/mifunc/Spr…

原文: https://rumenz.com/rumenbiji/Spring-HierarchicalBean.html

正文完
 0