2.二级接口ListableBeanFactory

这个随笔主要讲的是ListableBeanFactory

package org.springframework.beans.factory;

import java.lang.annotation.Annotation;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;

//可将Bean逐一列出的工厂
public interface ListableBeanFactory extends BeanFactory {

     //是否存在给定的bean名称的信息
    boolean containsBeanDefinition(String beanName);

     //BeanDefinition的总数
    int getBeanDefinitionCount();

    
     //返回工程中所有bean的名字
    String[] getBeanDefinitionNames();

    
     //返回对于指定类型的beanName
    String[] getBeanNamesForType(ResolvableType type);

     //返回指定类型bean的名字
    String[] getBeanNamesForType(Class<?> type);

    
         /*
     * 返回指定类型的名字
     * includeNonSingletons为false表示只取单例Bean,true则不是
     * allowEagerInit为true表示立刻加载,false表示延迟加载。
     * 注意:FactoryBeans都是立刻加载的。
     */
    String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);

    
     //根据bean的类型返回bean和bean的Map集合
    <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException;


    <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
            throws BeansException;

    
     // 根据注解类型,查找所有有这个注解的Bean名和Bean的Map
    String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);

    
     // 根据注解类型,查找所有有这个注解的Bean名和Bean的Map
    Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;


     //根据指定Bean名和注解类型查找指定的Bean
    <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
            throws NoSuchBeanDefinitionException;

}

这个工厂中扩展了

3个 BeanDefinition有关的接口,

3个根据指定类型返回beanName的数组

2个根据指定类型返回bean和存储Bean的Map集合

3个和注解有关的返回bean和beanMap的接口方法

对于BeanDefinition中包含了一个类在Spring工厂中所有属性。

这个工厂如同名字一样,可以返回所有bean的实例,但是这个工厂并没有直接返回实例的方法,只有通过条件返回指定bean的Name数组,Map等,

其实不需要直接返回实例的方法,因为我们拿到了bean的所有名字,我们就可以使用getbean,父接口中得方法,也就是BeanFactory进行获取Bean的实例!

原文地址:https://www.cnblogs.com/java-synchronized/p/6777556.html