单一类型依赖查找-BeanFactory
- 根据Bean名称查找
- getBean(String)
- Spring 2.5覆盖默认参数:getBean(String,Object...)
- 根据Bean类型查找
- Bean实时查找
- Spring3.0 getBean(Class)
- Spring 4.1覆盖默认参数:getBean(Class,Object)
- Spring 5.1 Bean延迟查找
- getBeanProvider(Class)
- Bean实时查找
public class ObjectProviderDemo {
public static void main(String[] args) {
//创建BeanFactory容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//ObjectProviderDemo Class(配置类)
applicationContext.register(ObjectProviderDemo.class);
//启动spring应用上下文
applicationContext.refresh();
//依赖查找集合
lookupByObjectProvider(applicationContext);
//显示的关闭spring应用上下文
applicationContext.close();
}
@Bean
public String helloWorld(){ //方法名就是Bean名称 = "helloWorld"
return "hello world";
}
private static void lookupByObjectProvider(AnnotationConfigApplicationContext applicationContext) {
ObjectProvider<String> beanProvider = applicationContext.getBeanProvider(String.class);
System.out.println(beanProvider.getObject());
}
}
* getBeanProvider(ResolvableType)
- 根据Bean名称+类型查找:getBean(String,Class)
集合类型依赖查找
集合类型查找接口-ListableBeanFactory
- 根据Bean类型查找
- 获取同类型Bean名称列表
- getBeanNamesForType(Class)
- Spring 4.2 getBeanNamesForType(ResovleType)
- 获取同类型Bean实例列表
- getBeansOfType(Class)以及重载方法
- 获取同类型Bean名称列表
- 通过注解类型查找
- Spring 3.0 获取标注类型Bean名称列表
- getBeanNamesForAnnotation(Class<? extends Annotation>)
- Spring 3.0 获取标注类型Bean实例列表
- getBeansWithAnnotation(Class<? Extends Annotation>)
- Spring 3.0 获取指定名称 + 标注类型Bean实例
- findAnnotationOnBean(String,Class<? extends Annotation>)
根据之前(见Spring Bean深入剖析)对于ListableBeanFactory的介绍,我们可以得出结论:它是针对某一个类型去查找一个集合列表,集合列表可能有两种情况,一种是查询这个Bean的名称,一种情况是查询Bean的实例,推荐使用Bean的名称去判断这个Bean是否存在,当然,重要的方式是判断BeanDefinition是否存在,这种方式会避免Bean的提早的初始化,产生一些不确定的因素。
层次性依赖查找
层次性依赖查找接口-HierachicalBeanFactory
- findAnnotationOnBean(String,Class<? extends Annotation>)
- Spring 3.0 获取标注类型Bean名称列表
public interface HierarchicalBeanFactory extends BeanFactory {
/**
* Return the parent bean factory, or {@code null} if there is none.
*/
@Nullable
BeanFactory getParentBeanFactory();
/**
* Return whether the local bean factory contains a bean of the given name,
* ignoring beans defined in ancestor contexts.
* <p>This is an alternative to {@code containsBean}, ignoring a bean
* of the given name from an ancestor bean factory.
* @param name the name of the bean to query
* @return whether a bean with the given name is defined in the local factory
* @see BeanFactory#containsBean
*/
boolean containsLocalBean(String name);
}
- 双亲BeanFactory:getParenetBeanFactroy()
public static void main(String[] args) {
//创建BeanFactory容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//ObjectProviderDemo Class(配置类)
applicationContext.register(ObjectProviderDemo.class);
//获取 HierarchicalBeanFactory <- ConfigurableBeanFactory <-ConfigurableListableBeanFactory
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
System.out.println("当前 BeanFactory 的 Parent BeanFactory :" + beanFactory.getParentBeanFactory());
//设置 Parent BeanFactory
HierarchicalBeanFactory parentBeanFactory = createParentBeanFactory();
beanFactory.setParentBeanFactory(parentBeanFactory);
System.out.println("当前 BeanFactory 的 Parent BeanFactory :" + beanFactory.getParentBeanFactory());
//启动spring应用上下文
applicationContext.refresh();
//显示的关闭spring应用上下文
applicationContext.close();
}
private static ConfigurableListableBeanFactory createParentBeanFactory(){
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
String location = "classpath:/META-INF/dependecy-lookup-context.xml";
reader.loadBeanDefinitions(location);
return beanFactory;
}
- 层次性查找
- 根据Bean的名称查找
- 基于containsLocalBean方法实现
- 根据Bean的名称查找
public class HierarchicalDependencyLookupDemo {
public static void main(String[] args) {
//创建BeanFactory容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//ObjectProviderDemo Class(配置类)
applicationContext.register(ObjectProviderDemo.class);
//获取 HierarchicalBeanFactory <- ConfigurableBeanFactory <-ConfigurableListableBeanFactory
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
System.out.println("当前 BeanFactory 的 Parent BeanFactory :" + beanFactory.getParentBeanFactory());
//设置 Parent BeanFactory
HierarchicalBeanFactory parentBeanFactory = createParentBeanFactory();
beanFactory.setParentBeanFactory(parentBeanFactory);
System.out.println("当前 BeanFactory 的 Parent BeanFactory :" + beanFactory.getParentBeanFactory());
displayLocalBean(beanFactory,"user");
displayLocalBean(parentBeanFactory,"user");
displayContainsBean(beanFactory,"user");
displayContainsBean(parentBeanFactory,"user");
//启动spring应用上下文
applicationContext.refresh();
//显示的关闭spring应用上下文
applicationContext.close();
}
private static void displayContainsBean(HierarchicalBeanFactory beanFactory,String beanName){
System.out.printf("当前 BeanFactory [s%] 是否包含 Local bean[ name : s%]\n",beanFactory,beanName);
containsBean(beanFactory,beanName);
}
private static boolean containsBean(HierarchicalBeanFactory beanFactory,String beanName){
BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory();
if(parentBeanFactory instanceof HierarchicalBeanFactory){
HierarchicalBeanFactory parentHierarchicalBeanFactory = HierarchicalBeanFactory.class.cast(parentBeanFactory);
if(containsBean(parentHierarchicalBeanFactory,beanName)){
return true;
}
}
return beanFactory.containsLocalBean(beanName);
}
private static void displayLocalBean(HierarchicalBeanFactory beanFactory,String beanName){
System.out.printf("当前 BeanFactory [s%] 是否包含 Local bean[ name : s%]\n",beanFactory,beanName);
}
private static ConfigurableListableBeanFactory createParentBeanFactory(){
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
String location = "classpath:/META-INF/dependecy-lookup-context.xml";
reader.loadBeanDefinitions(location);
return beanFactory;
}
}
- 根据Bean类型查找实例列表
- 单一类型:BeanFactoryUtils#beanOfType
- 集合类型:BeanFactoryUtils#beansOfTypeIncludingAncestors
- 根据Java注解查找名称列表
- BeansFactoryUtils#beanNamesForTypeIncluingAncestors