• 10. Spring高级IOC的深入剖析


    1、Spring中的BeanFactory

    1.1、BeanFactory类视图

    1.2、工厂详解

    1.2.1、BeanFactory
    	BeanFactory 中定义的各种方法如上面方法注释,整个设计还是比较简洁、直观的,其中将近一半是获取 bean 对象的各种方法,另外就是对 bean 属性的获取和判定,该接口仅仅是定义了 IoC 容器的最基本基本形式,具体实现都交由子类来实现。
    
    1.2.2、HierarchicalBeanFactory
    	HierarchicalBeanFactory 译为中文是“分层的”,它相对于 BeanFactory 增加了对父 BeanFactory 的获取,子容器可以通过接口方法访问父容器,让容器的设计具备了层次性。这种层次性增强了容器的扩展性和灵活性,我们可以通过编程的方式为一个已有的容器添加一个或多个子容器,从而实现一些特殊功能。层次容器有一个特点就是子容器对于父容器来说是透明的,而子容器则能感知到父容器的存在。典型的应用场景就是 Spring MVC,控制层的 bean 位于子容器中,并将业务层和持久层的 bean 所在的容器设置为父容器,这样的设计可以让控制层的 bean 访问业务层和持久层的 bean,反之则不行,从而在容器层面对三层软件结构设计提供支持。
    
    1.2.3、ListableBeanFactory
    ListableBeanFactory 引入了获取容器中 bean 的配置信息的若干方法,比如获取容器中 bean 的个数,获取容器中所有 bean 的名称列表,按照目标类型获取 bean 名称,以及检查容器中是否包含指定名称的 bean 等等。Listable 中文译为“可列举的”,对于容器而言,bean 的定义和属性是可以列举的对象。
    
    1.2.4、AutowireCapableBeanFactory
    	AutowireCapableBeanFactory 提供了创建 bean、自动注入,初始化以及应用 bean 的后置处理器等功能。自动注入让配置变得更加简单,也让注解配置成为可能,Spring 提供了四种自动注入类型:
    		byName:
    			根据名称自动装配。假设 bean A 有一个名为 b 的属性,如果容器中刚好存在一个 bean 的名称为 b,则将该 bean 装配给 bean A 的 b 属性。
    		byType:
    			根据类型自动匹配。假设 bean A 有一个类型为 B 的属性,如果容器中刚好有一个 B 类型的 bean,则使用该 bean 装配 A 的对应属性。
    		constructor:
    			仅针对构造方法注入而言,类似于 byType。如果 bean A 有一个构造方法,构造方法包含一个 B 类型的入参,如果容器中有一个 B 类型的 bean,则使用该 bean 作为入参,如果找不到,则抛出异常。
    		autodetect:
    			根据 bean 的自省机制决定采用 byType 还是 constructor 进行自动装配。如果 bean 提供了默认的构造函数,则采用 byType,否则采用 constructor。
    	总结:
    		<beans/> 元素标签中的 default-autowire 属性可以配置全局自动匹配,default-autowire 默认值为 no,表示不启用自动装配。在实际开发中,XML 配置方式很少启用自动装配功能,而基于注解的配置方式默认采用 byType 自动装配策略。
    
    1.2.5、ConfigurableBeanFactory
    ConfigurableBeanFactory 提供配置 Factory 的各种方法,增强了容器的可定制性,定义了设置类装载器、属性编辑器、容器初始化后置处理器等方法。
    
    1.2.6、DefaultListableBeanFactory
    DefaultListableBeanFactory 是一个非常重要的类,它包含了 IoC 容器所应该具备的重要功能,是容器完整功能的一个基本实现,XmlBeanFactory 是一个典型的由该类派生出来的 Factory,并且只是增加了加载 XML 配置资源的逻辑,而容器相关的特性则全部由 DefaultListableBeanFactory 来实现。
    
    1.2.7、ApplicationContext
    ApplicationContext 是 Spring 为开发者提供的高级容器形式,也是我们初始化 Spring 容器的常用方式,除了简单容器所具备的功能外,ApplicationContext 还提供了许多额外功能来降低开发人员的开发量,提升框架的使用效率。这些额外的功能主要包括:
    	国际化支持:ApplicationContext 实现了 org.springframework.context.MessageSource 接口,该接口为容器提供国际化消息访问功能,支持具备多语言版本需求的应用开发,并提供了多种实现来简化国际化资源文件的装载和获取。
    	发布应用上下文事件:ApplicationContext 实现了 org.springframework.context.ApplicationEventPublisher 接口,该接口让容器拥有发布应用上下文事件的功能,包括容器启动、关闭事件等,如果一个 bean 需要接收容器事件,则只需要实现 ApplicationListener 接口即可,Spring 会自动扫描对应的监听器配置,并注册成为主题的观察者。
    	丰富的资源获取的方式:ApplicationContext 实现了 org.springframework.core.io.support.ResourcePatternResolver 接口,ResourcePatternResolver 的实现类 PathMatchingResourcePatternResolver 让我们可以采用 Ant 风格的资源路径去加载配置文件。
    
    1.2.8、ConfigurableApplicationContext
    ConfigurableApplicationContext 中主要增加了 refresh 和 close 两个方法,从而为应用上下文提供了启动、刷新和关闭的能力。其中 refresh 方法是高级容器的核心方法,方法中概括了高级容器初始化的主要流程(包含简单的容器的全部功能,以及高级容器特有的扩展功能)
    
    1.2.9、WebApplicationContext
    WebApplicationContext 是为 WEB 应用定制的上下文,可以基于 WEB 容器来实现配置文件的加载,以及初始化工作。对于非 WEB 应用而言,bean 只有 singleton 和 prototype 两种作用域,而在 WebApplicationContext 中则新增了 request、session、globalSession,以及 application 四种作用域。
    WebApplicationContext 将整个应用上下文对象以属性的形式放置到 ServletContext 中,所以在 WEB 应用中,我们可以通过 WebApplicationContextUtils 的 getWebApplicationContext(ServletContext sc) 方法,从 ServletContext 中获取到 ApplicationContext 实例。为了支持这一特性,WebApplicationContext 定义了一个常量:
    
    ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT"
    
    并在初始化应用上下文时以该常量为 key,将 WebApplicationContext 实例存放到 ServletContext 的属性列表中,当我们在调用 WebApplicationContextUtils 的 getWebApplicationContext(ServletContext sc) 方法时,本质上是在调用 ServletContext 的 getAttribute(String name) 方法,只不过 Spring 会对获取的结果做一些校验。
    
    1.2.10、高级容器的一些具体实现类型
    AnnotationConfigApplicationContext:
    	是基于注解驱动开发的高级容器类,该类中提供了AnnotatedBeanDefinitionReader和ClassPathBeanDefinitionScanner两个成员,AnnotatedBeanDefinitionReader用于读取注解创建Bean的定义信息,ClassPathBeanDefinitionScanner负责扫描指定包获取Bean的定义信息。
    	
    ClasspathXmlApplicationContext:
    	是基于xml配置的高级容器类,它用于加载类路径下配置文件。
    	
    FileSystemXmlApplicationContext:
    	是基于xml配置的高级容器类,它用于加载文件系统中的配置文件。
    	
    AnnotationConfigWebApplicationContext:
    	是注解驱动开发web应用的高级容器类。
    

    2、Spring中的BeanDefinition

    2.1、BeanDefinition类视图

    2.2.1、类视图
    2.2.2、说明
    现实中的容器都是用来装物品的,Spring 的容器也不例外,这里的物品就是 bean。我通常对于 bean 的印象是一个个躺在配置文件中的 <bean/> 标签,或者是被注解的类,但是这些都是 bean 的静态表示,是还没有放入容器的物料,最终(加载完配置,且在 getBean 之前)加载到容器中的是一个个 BeanDefinition 实例。BeanDefinition 的继承关系如下图,RootBeanDefinition、ChildBeanDefinition,以及 GenericBeanDefinition 是三个主要的实现。有时候我们需要在配置时,通过 parent 属性指定 bean 的父子关系,这个时候父 bean 则用 RootBeanDefinition 表示,而子 bean 则用 ChildBeanDefinition 表示。GenericBeanDefinition 自 2.5 版本引入,是对于一般的 bean 定义的一站式服务中心。
    

    2.2、Bean的定义信息详解

    2.2.1、源码分析
    /**
     * 在上一小节我们介绍了RootBeanDefinition,ChildBeanDefinition,GenericBeanDefinition三个类
     * 他们都是由AbstractBeanDefinition派生而来,该抽象类中包含了bean的所有配置项和一些支持程序运
     * 行的属性。以下是类中属性的说明。
     */
    public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor implements BeanDefinition, Cloneable {
        // 常量定义略
    
        /** bean 对应的类实例 */
        private volatile Object beanClass;
        /** bean的作用域,对应scope属性 */
        private String scope = SCOPE_DEFAULT;
        /** 是否是抽象类,对应abstract属性 */
        private boolean abstractFlag = false;
        /** 是否延迟加载,对应lazy-init属性 */
        private boolean lazyInit = false;
        /** 自动装配模式,对应autowire属性 */
        private int autowireMode = AUTOWIRE_NO;
        /** 依赖检查,对应dependency-check属性 */
        private int dependencyCheck = DEPENDENCY_CHECK_NONE;
        /** 对应depends-on,表示一个bean实例化前置依赖另一个bean */
        private String[] dependsOn;
        /** 对应autowire-candidate属性,设置为false时表示取消当前bean作为自动装配候选者的资格 */
        private boolean autowireCandidate = true;
        /** 对应primary属性,当自动装配存在多个候选者时,将其作为首选 */
        private boolean primary = false;
        /** 对应qualifier属性 */
        private final Map<String, AutowireCandidateQualifier> qualifiers = new LinkedHashMap<String, AutowireCandidateQualifier>(0);
        /** 非配置项:表示允许访问非公开的构造器和方法,由程序设置 */
        private boolean nonPublicAccessAllowed = true;
        /**
         * 非配置项:表示是否允许以宽松的模式解析构造函数,由程序设置
         *
         * 例如:如果设置为true,则在下列情况时不会抛出异常(示例来源于《Spring源码深度解析》)
         * interface ITest{}
         * class ITestImpl implements ITest {}
         * class Main {
         * Main(ITest i){}
         * Main(ITestImpl i){}
         * }
         */
        private boolean lenientConstructorResolution = true;
        /** 对应factory-bean属性 */
        private String factoryBeanName;
        /** 对应factory-method属性 */
        private String factoryMethodName;
        /** 记录构造函数注入属性,对应<construct-arg/>标签 */
        private ConstructorArgumentValues constructorArgumentValues;
        /** 记录<property/>属性集合 */
        private MutablePropertyValues propertyValues;
        /** 记录<lookup-method/>和<replaced-method/>标签配置 */
        private MethodOverrides methodOverrides = new MethodOverrides();
        /** 对应init-method属性 */
        private String initMethodName;
        /** 对应destroy-method属性 */
        private String destroyMethodName;
        /** 非配置项:是否执行init-method,由程序设置 */
        private boolean enforceInitMethod = true;
        /** 非配置项:是否执行destroy-method,由程序设置 */
        private boolean enforceDestroyMethod = true;
        /** 非配置项:表示是否是用户定义,而不是程序定义的,创建AOP时为true,由程序设置 */
        private boolean synthetic = false;
        /**
         * 非配置项:定义bean的应用场景,由程序设置,角色如下:
         * ROLE_APPLICATION:用户
         * ROLE_INFRASTRUCTURE:完全内部使用
         * ROLE_SUPPORT:某些复杂配置的一部分
         */
        private int role = BeanDefinition.ROLE_APPLICATION;
        /** bean的描述信息,对应description标签 */
        private String description;
        /** bean定义的资源 */
        private Resource resource;
        
        // 方法定义略
    }
    
    2.2.2、总结
    BeanDefinition 是容器对于bean配置的内部表示,Spring 将各个 bean 的 BeanDefinition 实例注册记录在 BeanDefinitionRegistry 中,该接口定义了对 BeanDefinition 的各种增删查操作,类似于内存数据库,其实现类 SimpleBeanDefinitionRegistry 主要以 Map 作为存储标的。
    

    3、注解驱动执行过程分析

    3.1、使用配置类字节码的构造函数

    3.1.1、构造函数源码
    /**
     * Create a new AnnotationConfigApplicationContext that needs to be populated
     * through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
     */
    public AnnotationConfigApplicationContext() {
    	this.reader = new AnnotatedBeanDefinitionReader(this);
    	this.scanner = new ClassPathBeanDefinitionScanner(this);
    }
    /**
     * Create a new AnnotationConfigApplicationContext, deriving bean definitions
     * from the given annotated classes and automatically refreshing the context.
     * @param annotatedClasses one or more annotated classes,
     * e.g. {@link Configuration @Configuration} classes
     */
    public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
    	this();
    	register(annotatedClasses);
    	refresh();
    }
    
    3.1.2、register方法说明
    	它是根据传入的配置类字节码解析Bean对象中注解的(包括类上的和类中方法和字段上的注解。如果类没有被注解,那么类中方法和字段上的注解不会被扫描)。使用的是AnnotatedGenericBeanDefinition,里面包含了BeanDefinition和Scope两部分信息,其中BeanDefinition是传入注解类的信息,即SpringConfiguration;scope是指定bean的作用范围,默认情况下为单例。
    	同时,借助AnnotationConfigUtils类中processCommonDefinitionAnnotations方法判断是否使用了Primary,Lazy,DependsOn等注解来决定Bean的加载时机。
    	在ConfigurationClassBeanDefinitionReader类中的registerBeanDefinitionForImportedConfigurationClass方法会把导入的JdbcConfig类注册到容器中。而loadBeanDefinitionsForBeanMethod方法会解析Bean注解,把被Bean注解修饰的方法返回值存入容器。
    
    3.1.3、执行过程分析图

    3.2、使用包扫描的构造函数

    3.2.1、构造函数源码
    /**
     * Create a new AnnotationConfigApplicationContext that needs to be populated
     * through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
     */
    public AnnotationConfigApplicationContext() {
    	this.reader = new AnnotatedBeanDefinitionReader(this);
    	this.scanner = new ClassPathBeanDefinitionScanner(this);
    }
    /**
     * Create a new AnnotationConfigApplicationContext, scanning for bean definitions
     * in the given packages and automatically refreshing the context.
     * @param basePackages the packages to check for annotated classes
     */
    public AnnotationConfigApplicationContext(String... basePackages) {
        this();
        scan(basePackages);
        refresh();
    }
    
    3.2.2、scan方法说明
    	它是根据传入的类路径下(classpath*)的包解析Bean对象中注解的(包括类上以及类成员的),使用的是ClassPathBeanDefinitionScanner类中的doScan方法,该方法最终将得到的BeanDefinitionHolder信息存储到LinkedHashSet中,为后面初始化容器做准备。
    	doScan中的findCandidateComponents方法调用ClassPathScanningCandidateComponentProvider类中的scanCandidateComponents方法,而此方法又去执行了PathMatchingResourcePatternResolver类中的doFindAllClassPathResources方法,找到指定扫描包的URL(是URL,不是路径。因为是带有file协议的),然后根据磁盘路径读取当前目录及其子目录下的所有类。接下来执行AnnotationConfigUtils类中的processCommonDefinitionAnnotations方法,剩余就和本章节第一小节后面的过程一样了。
    
    3.2.3、执行过程分析图

    3.3、注册注解类型过滤器

    3.3.1、ClassPathScanningCandidateComponentProvider的registerDefaultFilters方法说明
    /**
    	 * Register the default filter for {@link Component @Component}.
    	 * <p>This will implicitly register all annotations that have the
    	 * {@link Component @Component} meta-annotation including the
    	 * {@link Repository @Repository}, {@link Service @Service}, and
    	 * {@link Controller @Controller} stereotype annotations.
    	 * <p>Also supports Java EE 6's {@link javax.annotation.ManagedBean} and
    	 * JSR-330's {@link javax.inject.Named} annotations, if available.
    	 *
    	 */
    	@SuppressWarnings("unchecked")
    	protected void registerDefaultFilters() {
    		this.includeFilters.add(new AnnotationTypeFilter(Component.class));
    		ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
    		try {
    			this.includeFilters.add(new AnnotationTypeFilter(
    					((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
    			logger.trace("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
    		}
    		catch (ClassNotFoundException ex) {
    			// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
    		}
    		try {
    			this.includeFilters.add(new AnnotationTypeFilter(
    					((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
    			logger.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
    		}
    		catch (ClassNotFoundException ex) {
    			// JSR-330 API not available - simply skip.
    		}
    	}
    

    3.4、准备和初始化容器

    3.4.1、AbstractApplicationContext的refresh方法说明
    @Override
    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            //Prepare this context for refreshing.
          	//1.准备容器,设置一些初始化信息,例如启动时间。验证必须要的属性等等。
            prepareRefresh();
    
            // Tell the subclass to refresh the internal bean factory.
            //2.告诉子类刷新内部bean工厂。实际就是重新创建一个Bean工厂
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    
            // Prepare the bean factory for use in this context.
            //3.准备使用创建的这个BeanFactory,添加或者注册到当前Bean工厂一些必要对象。
            prepareBeanFactory(beanFactory);
    
            try {
                // Allows post-processing of the bean factory in context subclasses.
              	//4.允许子容器对BeanFactory进行后处理。例如,在web环境中bean的作用范围等等。
                postProcessBeanFactory(beanFactory);
    
                // Invoke factory processors registered as beans in the context.
              	//5.在Singleton的Bean对象初始化前,对Bean工厂进行一些处理
                invokeBeanFactoryPostProcessors(beanFactory);
    
                // Register bean processors that intercept bean creation.
              	//6.注册拦截bean创建的处理器
                registerBeanPostProcessors(beanFactory);
    
                // Initialize message source for this context.
              	//7.初始化消息资源接口的实现类。主要用于处理国际化(i18n)
                initMessageSource();
    
                // Initialize event multicaster for this context.
              	//8、为容器注册一个事件组播器
                initApplicationEventMulticaster();
    
                // Initialize other special beans in specific context subclasses.
              	//9.在AbstractApplicationContext的子类中初始化其他特殊的bean
                onRefresh();
    
                // Check for listener beans and register them.
              	//10.注册应用的监听器。就是注册实现了ApplicationListener接口的监听器bean
                registerListeners();
    
                // Instantiate all remaining (non-lazy-init) singletons.
              	//11.实例化所有剩余的(非lazy init)单例。(就是没有被@Lazy修饰的单例Bean)
                finishBeanFactoryInitialization(beanFactory);//十一、
    
                // Last step: publish corresponding event.
              	//12.完成context的刷新。主要是调用LifecycleProcessor的onRefresh()方法,并且发布事件(ContextRefreshedEvent)。
                finishRefresh();
            }
    
            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }
    
                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();//如果刷新失败那么就会将已经创建好的单例Bean销毁掉
    
                // Reset 'active' flag.
                cancelRefresh(ex);//重置context的活动状态
    
                // Propagate exception to caller.
                throw ex;//抛出异常
            }
    
            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                resetCommonCaches();//重置的Spring内核的缓存。因为可能不再需要metadata给单例Bean了。
            }
        }
    }
    

    3.5、实例化和获取Bean对象

    3.5.1、AbstractBeanFactory的doGetBean方法说明
    protected <T> T doGetBean(
                final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException {
        /*
         * 获取name对应的真正beanName
         *
         * 因为传入的参数可以是alias,也可能是FactoryBean的name,所以需要进行解析,包含以下内容:
         * 1. 如果是FactoryBean,则去掉修饰符“&”
         * 2. 沿着引用链获取alias对应的最终name
         */
        final String beanName = this.transformedBeanName(name);
     
        Object bean;
     
        /*
         * 检查缓存或者实例工厂中是否有对应的单例
         *
         * 在创建单例bean的时候会存在依赖注入的情况,而在创建依赖的时候为了避免循环依赖
         * Spring创建bean的原则是不等bean创建完成就会将创建bean的ObjectFactory提前曝光(将对应的ObjectFactory加入到缓存)
         * 一旦下一个bean创建需要依赖上一个bean,则直接使用ObjectFactory对象
         */
        Object sharedInstance = this.getSingleton(beanName); // 获取单例
        if (sharedInstance != null && args == null) {
            // 实例已经存在
            if (logger.isDebugEnabled()) {
                if (this.isSingletonCurrentlyInCreation(beanName)) {
                    logger.debug("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference");
                } else {
                    logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
                }
            }
            // 返回对应的实例
            bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, null);
        } else {
            // 单例实例不存在
            if (this.isPrototypeCurrentlyInCreation(beanName)) {
                /*
                 * 只有在单例模式下才会尝试解决循环依赖问题
                 * 对于原型模式,如果存在循环依赖,也就是满足this.isPrototypeCurrentlyInCreation(beanName),抛出异常
                 */
                throw new BeanCurrentlyInCreationException(beanName);
            }
     
            // 获取parentBeanFactory实例
            BeanFactory parentBeanFactory = this.getParentBeanFactory();
            // 如果在beanDefinitionMap中(即所有已经加载的类中)不包含目标bean,则尝试从parentBeanFactory中获取
            if (parentBeanFactory != null && !this.containsBeanDefinition(beanName)) {
                String nameToLookup = this.originalBeanName(name);  // 获取name对应的真正beanName,如果是factoryBean,则加上“&”前缀
                if (args != null) {
                    // 递归到BeanFactory中寻找
                    return (T) parentBeanFactory.getBean(nameToLookup, args);
                } else {
                    return parentBeanFactory.getBean(nameToLookup, requiredType);
                }
            }
     
            // 如果不仅仅是做类型检查,标记bean的状态已经创建,即将beanName加入alreadyCreated集合中
            if (!typeCheckOnly) {
                this.markBeanAsCreated(beanName);
            }
     
            try {
                /*
                 * 将存储XML配置的GenericBeanDefinition实例转换成RootBeanDefinition实例,方便后续处理
                 * 如果存在父bean,则同时合并父bean的相关属性
                 */
                final RootBeanDefinition mbd = this.getMergedLocalBeanDefinition(beanName);
                // 检查bean是否是抽象的,如果是则抛出异常
                this.checkMergedBeanDefinition(mbd, beanName, args);
     
                // 加载当前bean依赖的bean
                String[] dependsOn = mbd.getDependsOn();
                if (dependsOn != null) {
                    // 存在依赖,递归实例化依赖的bean
                    for (String dep : dependsOn) {
                        if (this.isDependent(beanName, dep)) {
                            // 检查dep是否依赖beanName,从而导致循环依赖
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
                        }
                        // 缓存依赖调用
                        this.registerDependentBean(dep, beanName);
                        this.getBean(dep);
                    }
                }
     
                // 完成加载依赖的bean后,实例化mbd自身
                if (mbd.isSingleton()) {
                    // scope == singleton
                    sharedInstance = this.getSingleton(beanName, new ObjectFactory<Object>() {
                        @Override
                        public Object getObject() throws BeansException {
                            try {
                                return createBean(beanName, mbd, args);
                            } catch (BeansException ex) {
                                // 清理工作,从单例缓存中移除
                                destroySingleton(beanName);
                                throw ex;
                            }
                        }
                    });
                    bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                } else if (mbd.isPrototype()) {
                    // scope == prototype
                    Object prototypeInstance;
                    try {
                        // 设置正在创建的状态
                        this.beforePrototypeCreation(beanName);
                        // 创建bean
                        prototypeInstance = this.createBean(beanName, mbd, args);
                    } finally {
                        this.afterPrototypeCreation(beanName);
                    }
                    // 返回对应的实例
                    bean = this.getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
                } else {
                    // 其它scope
                    String scopeName = mbd.getScope();
                    final Scope scope = this.scopes.get(scopeName);
                    if (scope == null) {
                        throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
                    }
                    try {
                        Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
                            @Override
                            public Object getObject() throws BeansException {
                                beforePrototypeCreation(beanName);
                                try {
                                    return createBean(beanName, mbd, args);
                                } finally {
                                    afterPrototypeCreation(beanName);
                                }
                            }
                        });
                        // 返回对应的实例
                        bean = this.getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                    } catch (IllegalStateException ex) {
                        throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex);
                    }
                }
            } catch (BeansException ex) {
                cleanupAfterBeanCreationFailure(beanName);
                throw ex;
            }
        }
     
        // 检查需要的类型是否符合bean的实际类型,对应getBean时指定的requireType
        if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) {
            try {
                // 执行类型转换,转换成期望的类型
                return this.getTypeConverter().convertIfNecessary(bean, requiredType);
            } catch (TypeMismatchException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to convert bean '" + name + "' to required type '" + ClassUtils.getQualifiedName(requiredType) + "'", ex);
                }
                throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
            }
        }
        return (T) bean;
    }
    

    4、BeanNameGenerator及其实现类

    4.1、BeanNameGenerator

    BeanNameGenerator接口位于 org.springframework.beans.factory.support 包下面:

    /**
     * Strategy interface for generating bean names for bean definitions.
     *
     * @author Juergen Hoeller
     * @since 2.0.3
     */
    public interface BeanNameGenerator {
    
    	/**
    	 * Generate a bean name for the given bean definition.
    	 * @param definition the bean definition to generate a name for
    	 * @param registry the bean definition registry that the given definition
    	 * is supposed to be registered with
    	 * @return the generated bean name
    	 */
    	String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry);
    
    }
    

    它有两个实现类:分别是:

    其中DefaultBeanNameGenerator是给资源文件加载bean时使用(BeanDefinitionReader中使用);AnnotationBeanNameGenerator是为了处理注解生成bean name的情况。

    4.2、AnnotationBeanNameGenerator

    /**
     * 此方法是接口中抽象方法的实现。
     * 该方法分为两个部分:
     *	  第一个部分:当指定了bean的名称,则直接使用指定的名称。
     *	  第二个部分:当没有指定bean的名称时,则使用当前类的短类名作为bean的唯一标识。
     */
    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
      	//判断bean的定义信息是否为基于注解的
    	if (definition instanceof AnnotatedBeanDefinition) {
          	//解析注解中的属性,看看有没有指定的bean的唯一标识
    		String beanName = determineBeanNameFromAnnotation((AnnotatedBeanDefinition) definition);
    		if (StringUtils.hasText(beanName)) {
    			//返回注解的属性指定的bean的唯一标识
    			return beanName;
    		}
    	}
    	// 调用方法,使用注解bean名称的命名规则,生成bean的唯一标识
    	return buildDefaultBeanName(definition, registry);
    }
    
    /**
     * Derive a default bean name from the given bean definition.
     * <p>The default implementation delegates to {@link #buildDefaultBeanName(BeanDefinition)}.
     * @param definition the bean definition to build a bean name for
     * @param registry the registry that the given bean definition is being registered with
     * @return the default bean name (never {@code null})
     */
    protected String buildDefaultBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
      return buildDefaultBeanName(definition);
    }
    
    /**
     * Derive a default bean name from the given bean definition.
     * <p>The default implementation simply builds a decapitalized version
     * of the short class name: e.g. "mypackage.MyJdbcDao" -> "myJdbcDao".
     * <p>Note that inner classes will thus have names of the form
     * "outerClassName.InnerClassName", which because of the period in the
     * name may be an issue if you are autowiring by name.
     * @param definition the bean definition to build a bean name for
     * @return the default bean name (never {@code null})
     */
    protected String buildDefaultBeanName(BeanDefinition definition) {
      String beanClassName = definition.getBeanClassName();
      Assert.state(beanClassName != null, "No bean class name set");
      String shortClassName = ClassUtils.getShortName(beanClassName);
      return Introspector.decapitalize(shortClassName);
    }
    
    ClassUtils的代码节选:
    /**
     * Miscellaneous class utility methods.
     * Mainly for internal use within the framework.
     *
     * @author Juergen Hoeller
     * @author Keith Donald
     * @author Rob Harrop
     * @author Sam Brannen
     * @since 1.1
     * @see TypeUtils
     * @see ReflectionUtils
     */
    public abstract class ClassUtils {
    
    	/** Suffix for array class names: {@code "[]"}. */
    	public static final String ARRAY_SUFFIX = "[]";
    
    	/** Prefix for internal array class names: {@code "["}. */
    	private static final String INTERNAL_ARRAY_PREFIX = "[";
    
    	/** Prefix for internal non-primitive array class names: {@code "[L"}. */
    	private static final String NON_PRIMITIVE_ARRAY_PREFIX = "[L";
    
    	/** The package separator character: {@code '.'}. */
    	private static final char PACKAGE_SEPARATOR = '.';
    
    	/** The path separator character: {@code '/'}. */
    	private static final char PATH_SEPARATOR = '/';
    
    	/** The inner class separator character: {@code '$'}. */
    	private static final char INNER_CLASS_SEPARATOR = '$';
    
    	/** The CGLIB class separator: {@code "$$"}. */
    	public static final String CGLIB_CLASS_SEPARATOR = "$$";
    
    	/** The ".class" file suffix. */
    	public static final String CLASS_FILE_SUFFIX = ".class";
    	
      	/**
    	 * Get the class name without the qualified package name.
    	 * @param className the className to get the short name for
    	 * @return the class name of the class without the package name
    	 * @throws IllegalArgumentException if the className is empty
    	 */
    	public static String getShortName(String className) {
    		Assert.hasLength(className, "Class name must not be empty");
    		int lastDotIndex = className.lastIndexOf(PACKAGE_SEPARATOR);
    		int nameEndIndex = className.indexOf(CGLIB_CLASS_SEPARATOR);
    		if (nameEndIndex == -1) {
    			nameEndIndex = className.length();
    		}
    		String shortName = className.substring(lastDotIndex + 1, nameEndIndex);
    		shortName = shortName.replace(INNER_CLASS_SEPARATOR, PACKAGE_SEPARATOR);
    		return shortName;
    	}
      
      	//其余代码略
    }
    

    4.3、DefaultBeanNameGenerator

    /**
     * Default implementation of the {@link BeanNameGenerator} interface, delegating to
     * {@link BeanDefinitionReaderUtils#generateBeanName(BeanDefinition, BeanDefinitionRegistry)}.
     *
     * @author Juergen Hoeller
     * @since 2.0.3
     */
    public class DefaultBeanNameGenerator implements BeanNameGenerator {
    
    	@Override
    	public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
    		return BeanDefinitionReaderUtils.generateBeanName(definition, registry);
    	}
    }
    
    DefaultBeanNameGenerator类将具体的处理方式委托给了,BeanDefinitionReaderUtils#generateBeanName(BeanDefinition, BeanDefinitionRegistry)方法处理。 
    
    以下是代码节选:
    public abstract class BeanDefinitionReaderUtils {
    
    	public static String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry registry)
    			throws BeanDefinitionStoreException {
    		//此方法除了bean的定义信息和定义注册之外,还有一个布尔类型的值,用于确定是内部bean还是顶层bean
    		return generateBeanName(beanDefinition, registry, false);
    	}
    
    	/**
    	 * 生成bean的唯一标识(默认规则)
    	 */
    	public static String generateBeanName(
    			BeanDefinition definition, BeanDefinitionRegistry registry, boolean isInnerBean)
    			throws BeanDefinitionStoreException {
    
    		String generatedBeanName = definition.getBeanClassName();
    		if (generatedBeanName == null) {
    			if (definition.getParentName() != null) {
    				generatedBeanName = definition.getParentName() + "$child";
    			}
    			else if (definition.getFactoryBeanName() != null) {
    				generatedBeanName = definition.getFactoryBeanName() + "$created";
    			}
    		}
    		if (!StringUtils.hasText(generatedBeanName)) {
    			throw new BeanDefinitionStoreException("Unnamed bean definition specifies neither " +
    					"'class' nor 'parent' nor 'factory-bean' - can't generate bean name");
    		}
    
    		String id = generatedBeanName;
    		if (isInnerBean) {
    			// Inner bean: generate identity hashcode suffix.
    			id = generatedBeanName + GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(definition);
    		}
    		else {
    			// Top-level bean: use plain class name with unique suffix if necessary.
    			return uniqueBeanName(generatedBeanName, registry);
    		}
    		return id;
    	}
    
    	//其他代码略
    }
    

    5、ScopedProxyMode枚举

    /**
     * Enumerates the various scoped-proxy options.
     *
     * <p>For a more complete discussion of exactly what a scoped proxy is, see the
     * section of the Spring reference documentation entitled '<em>Scoped beans as
     * dependencies</em>'.
     *
     * @author Mark Fisher
     * @since 2.5
     * @see ScopeMetadata
     */
    public enum ScopedProxyMode {
    
    	/**
    	 * Default typically equals {@link #NO}, unless a different default
    	 * has been configured at the component-scan instruction level.
    	 */
    	DEFAULT,
    
    	/**
    	 * Do not create a scoped proxy.
    	 * <p>This proxy-mode is not typically useful when used with a
    	 * non-singleton scoped instance, which should favor the use of the
    	 * {@link #INTERFACES} or {@link #TARGET_CLASS} proxy-modes instead if it
    	 * is to be used as a dependency.
    	 */
    	NO,
    
    	/**
    	 * Create a JDK dynamic proxy implementing <i>all</i> interfaces exposed by
    	 * the class of the target object.
    	 */
    	INTERFACES,
    
    	/**
    	 * Create a class-based proxy (uses CGLIB).
    	 */
    	TARGET_CLASS;
    
    }
    

    6、自定义组件扫描过滤规则

    6.1、FilterType枚举

    public enum FilterType {
    
    	/**
    	 * Filter candidates marked with a given annotation.
    	 * @see org.springframework.core.type.filter.AnnotationTypeFilter
    	 */
    	ANNOTATION,
    
    	/**
    	 * Filter candidates assignable to a given type.
    	 * @see org.springframework.core.type.filter.AssignableTypeFilter
    	 */
    	ASSIGNABLE_TYPE,
    
    	/**
    	 * Filter candidates matching a given AspectJ type pattern expression.
    	 * @see org.springframework.core.type.filter.AspectJTypeFilter
    	 */
    	ASPECTJ,
    
    	/**
    	 * Filter candidates matching a given regex pattern.
    	 * @see org.springframework.core.type.filter.RegexPatternTypeFilter
    	 */
    	REGEX,
    
    	/** Filter candidates using a given custom
    	 * {@link org.springframework.core.type.filter.TypeFilter} implementation.
    	 */
    	CUSTOM
    
    }
    

    6.2、TypeFilter接口

    @FunctionalInterface
    public interface TypeFilter {
    
    	/**
    	 * 此方法返回一个boolean类型的值。
    	 * 当返回true时,表示加入到spring的容器中。返回false时,不加入容器。
    	 * 参数metadataReader:表示读取到的当前正在扫描的类的信息
    	 * 参数metadataReaderFactory:表示可以获得到其他任何类的信息
    	 */
    	boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
    			throws IOException;
    
    }
    

    6.3、使用Spring提供的过滤规则-AnnotationTypeFilter

    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     * 当我们使用注解驱动开发JavaEE项目时,spring提供的容器分为RootApplicationContext和 
     * ServletApplicationContext。此时我们不希望Root容器创建时把Controller加入到容器中,
     * 就可以使用过滤规则排除@Controller注解配置的Bean对象。
     */
    @Configuration
    @ComponentScan(value = "com.itheima",excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class))
    public class SpringConfiguration {
    }
    

    6.4、自定义过滤规则

    6.4.1、场景分析
    	在实际开发中,有很多下面这种业务场景:一个业务需求根据环境的不同可能会有很多种实现。针对不同的环境,要加载不同的实现。我们看下面这个案例:
    	我们现在是一个汽车销售集团,在成立之初,只是在北京销售汽车,我们的项目研发完成后只在北京部署上线。但随着公司的业务发展,现在全国各地均有销售大区,总部设在北京。各大区有独立的项目部署,但是每个大区的业绩计算和绩效提成的计算方式并不相同。
    	例如:
    		在华北区销售一台豪华级轿车绩效算5,提成销售额1%,销售豪华级SUV绩效算3,提成是0.5%。
    		在西南区销售一台豪华级轿车绩效算3,提成销售额0.5%,销售豪华级SUV绩效算5,提成是1.5%。
    	这时,我们如果针对不同大区对项目源码进行删减替换,会带来很多不必要的麻烦。而如果加入一些if/else的判断,显然过于简单粗暴。此时应该考虑采用桥接设计模式,把将涉及到区域性差异的模块功能单独抽取到代表区域功能的接口中。针对不同区域进行实现。并且在扫描组件注册到容器中时,采用哪个区域的具体实现,应该采用配置文件配置起来。而自定义TypeFilter就可以实现注册指定区域的组件到容器中。
    
    6.4.2、代码实现
    /**
     * 区域的注解
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface District {
    
        /**
         * 指定区域的名称
         * @return
         */
        String value()  default "";
    }
    
    /**
     * 销售分成的桥接接口
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public interface DistrictPercentage {
    
        /**
         * 不同车型提成
         * @param carType
         */
        void salePercentage(String carType);
    }
    
    /**
     * 绩效计算桥接接口
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public interface DistrictPerformance {
    
        /**
         * 计算绩效
         * @param carType
         */
        void calcPerformance(String carType);
    }
    
    /**
     * 华北区销售分成具体实现
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @Component("districtPercentage")
    @District("north")
    public class NorthDistrictPercentage implements DistrictPercentage {
    
    
        @Override
        public void salePercentage(String carType) {
            if("SUV".equalsIgnoreCase(carType)) {
                System.out.println("华北区"+carType+"提成1%");
            }else if("car".equalsIgnoreCase(carType)){
                System.out.println("华北区"+carType+"提成0.5%");
            }
        }
    }
    
    /**
     * 华北区销售绩效具体实现
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @Component("districtPerformance")
    @District("north")
    public class NorthDistrictPerformance implements DistrictPerformance {
    
        @Override
        public void calcPerformance(String carType) {
            if("SUV".equalsIgnoreCase(carType)) {
                System.out.println("华北区"+carType+"绩效3");
            }else if("car".equalsIgnoreCase(carType)){
                System.out.println("华北区"+carType+"绩效5");
            }
        }
    }
    
    /**
     * 西南区销售分成具体实现
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @Component("districtPercentage")
    @District("southwest")
    public class SouthwestDistrictPercentage implements DistrictPercentage {
    
        @Override
        public void salePercentage(String carType) {
            if("SUV".equalsIgnoreCase(carType)) {
                System.out.println("西南区"+carType+"提成1.5%");
            }else if("car".equalsIgnoreCase(carType)){
                System.out.println("西南区"+carType+"提成0.5%");
            }
        }
    }
    
    
    /**
     * 西南区绩效计算具体实现
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @Component("districtPerformance")
    @District("southwest")
    public class SouthwestDistrictPerformance implements DistrictPerformance {
    
        @Override
        public void calcPerformance(String carType) {
            if("SUV".equalsIgnoreCase(carType)) {
                System.out.println("西南区"+carType+"绩效5");
            }else if("car".equalsIgnoreCase(carType)){
                System.out.println("西南区"+carType+"绩效3");
            }
        }
    }
    
    /**
     * spring的配置类
     * 用于替代xml配置
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @Configuration
    @PropertySource(value = "classpath:district.properties")
    @ComponentScan(value = "com.itheima",
                excludeFilters = @ComponentScan.Filter(type=FilterType.CUSTOM,classes = DistrictTypeFilter.class))
    public class SpringConfiguration {
    }
    
    /**
     * spring的自定义扫描规则
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public class DistrictTypeFilter extends AbstractTypeHierarchyTraversingFilter {
    
        //定义路径校验类对象
        private PathMatcher pathMatcher;
    
        //注意:使用@Value注解的方式是获取不到配置值的。
        //因为Spring的生命周期里,负责填充属性值的InstantiationAwareBeanPostProcessor 与TypeFilter的实例化过程压根搭不上边。
    //    @Value("${common.district.name}")
        private String districtName;
    
        /**
         * 默认构造函数
         */
        public DistrictTypeFilter() {
            //1.第一个参数:不考虑基类。2.第二个参数:不考虑接口上的信息
            super(false, false);
    
    
            //借助Spring默认的Resource通配符路径方式
            pathMatcher = new AntPathMatcher();
    
            //硬编码读取配置信息
            try {
                Properties loadAllProperties = PropertiesLoaderUtils.loadAllProperties("district.properties");
                districtName = loadAllProperties.getProperty("common.district.name");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        //注意本类将注册为Exclude, 返回true代表拒绝
        @Override
        protected boolean matchClassName(String className) {
            try{
                if (!isPotentialPackageClass(className)) {
                    return false;
                }
    
                // 判断当前区域是否和所配置的区域一致, 不一致则阻止载入Spring容器
                Class<?> clazz = ClassUtils.forName(className, DistrictTypeFilter.class.getClassLoader());
                District districtAnnotation = clazz.getAnnotation(District.class);
                if(null == districtAnnotation){
                    return false;
                }
                final String districtValue = districtAnnotation.value();
                return (!districtName.equalsIgnoreCase(districtValue));
            }catch (Exception e){
                throw new RuntimeException(e);
            }
        }
    
        // 潜在的满足条件的类的类名, 指定package下
        private static final String PATTERN_STANDARD = ClassUtils
                .convertClassNameToResourcePath("com.itheima.service.impl.*");
    
        // 本类逻辑中可以处理的类 -- 指定package下的才会进行逻辑判断,
        private boolean isPotentialPackageClass(String className) {
            // 将类名转换为资源路径, 以进行匹配测试
            final String path = ClassUtils.convertClassNameToResourcePath(className);
            return pathMatcher.match(PATTERN_STANDARD, path);
        }
    
    }
    
    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public class SpringAnnotationTypeFilterTest {
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext("config");
            DistrictPerformance districtPerformance = ac.getBean("districtPerformance", DistrictPerformance.class);
            districtPerformance.calcPerformance("SUV");
    
            DistrictPercentage districtPercentage = ac.getBean("districtPercentage",DistrictPercentage.class);
            districtPercentage.salePercentage("car");
    
        }
    }
    

    7、@Import注解的高级分析

    7.1、ImportSelector和ImportBeanDefinitionRegistrar介绍

    特别说明:
    	我们在注入bean对象时,可选的方式有很多种。
    	例如:
    		我们自己写的类,可以使用@Component,@Service,@Repository,@Controller等等。
    		我们导入的第三方库中的类,可以使用@Bean(当需要做一些初始化操作时,比如DataSource),也可以使用@Import注解,直接指定要引入的类的字节码。
    		但是当我们的类很多时,在每个类上加注解会很繁琐,同时使用@Bean或者@Import写起来也很麻烦。此时我们就可以采用自定义ImportSelector或者ImportBeanDefinitionRegistrar来实现。顺便说一句,在SpringBoot中,@EnableXXX这样的注解,绝大多数都借助了ImportSelector或者ImportBeanDefinitionRegistrar。在我们的spring中,@EnableTransactionManagement就是借助了ImportSelector,而@EnableAspectJAutoporxy就是借助了ImportBeanDefinitionRegistrar。
    		
    共同点:
    	他们都是用于动态注册bean对象到容器中的。并且支持大批量的bean导入。
    区别:
    	ImportSelector是一个接口,我们在使用时需要自己提供实现类。实现类中返回要注册的bean的全限定类名数组,然后执行ConfigurationClassParser类中中的processImports方法注册bean对象的。
    	ImportBeanDefinitionRegistrar也是一个接口,需要我们自己编写实现类,在实现类中手动注册bean到容器中。
    
    注意事项:
    	实现了ImportSelector接口或者ImportBeanDefinitionRegistrar接口的类不会被解析成一个Bean注册到容器中。
    	同时,在注册到容器中时bean的唯一标识是全限定类名,而非短类名。
    

    7.2、自定义ImportSelector

    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public interface UserService {
        void saveUser();
    }
    
    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public class UserServiceImpl implements UserService {
    
        @Override
        public void saveUser() {
            System.out.println("保存用户");
        }
    }
    
    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @Configuration
    @ComponentScan("com.itheima")
    @Import(CustomeImportSelector.class)
    public class SpringConfiguration {
    }
    
    /**
     * customeimport.properties配置文件中的内容:
     * 		custome.importselector.expression= com.itheima.service.impl.*
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public class CustomeImportSelector implements ImportSelector {
    
        private String expression;
    
        public CustomeImportSelector(){
            try {
                Properties loadAllProperties = PropertiesLoaderUtils.loadAllProperties("customeimport.properties");
                expression = loadAllProperties.getProperty("custome.importselector.expression");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        /**
         * 生成要导入的bean全限定类名数组
         * @param importingClassMetadata
         * @return
         */
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            //1.定义扫描包的名称
            String[] basePackages = null;
            //2.判断有@Import注解的类上是否有@ComponentScan注解
            if (importingClassMetadata.hasAnnotation(ComponentScan.class.getName())) {
                //3.取出@ComponentScan注解的属性
                Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName());
                //4.取出属性名称为basePackages属性的值
                basePackages = (String[]) annotationAttributes.get("basePackages");
            }
            //5.判断是否有此属性(如果没有ComponentScan注解则属性值为null,如果有ComponentScan注解,则basePackages默认为空数组)
            if (basePackages == null || basePackages.length == 0) {
                String basePackage = null;
                try {
                    //6.取出包含@Import注解类的包名
                    basePackage = Class.forName(importingClassMetadata.getClassName()).getPackage().getName();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                //7.存入数组中
                basePackages = new String[] {basePackage};
            }
            //8.创建类路径扫描器
            ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
            //9.创建类型过滤器(此处使用切入点表达式类型过滤器)
            TypeFilter typeFilter = new AspectJTypeFilter(expression,this.getClass().getClassLoader());
            //10.给扫描器加入类型过滤器
            scanner.addIncludeFilter(typeFilter);
            //11.创建存放全限定类名的集合
            Set<String> classes = new HashSet<>();
            //12.填充集合数据
            for (String basePackage : basePackages) {
                scanner.findCandidateComponents(basePackage).forEach(beanDefinition -> classes.add(beanDefinition.getBeanClassName()));
            }
            //13.按照规则返回
            return classes.toArray(new String[classes.size()]);
        }
    }
    
    /**
     * 测试类
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public class SpringCustomeImportSelectorTest {
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext("config");
            String[] names = ac.getBeanDefinitionNames();
            for(String beanName : names){
                Object obj = ac.getBean(beanName);
                System.out.println(beanName+"============"+obj);
            }
        }
    }
    

    7.3、自定义ImportBeanDefinitionRegistrar

    借助7.2小节的案例代码,只需要把配置改一下:
    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @Configuration
    @ComponentScan("com.itheima")
    @Import(CustomeImportDefinitionRegistrar.class)
    public class SpringConfiguration {
    }
    
    /**
     * 自定义bean导入注册器
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public class CustomeImportDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    
        private String expression;
    
        public CustomeImportDefinitionRegistrar(){
            try {
                Properties loadAllProperties = PropertiesLoaderUtils.loadAllProperties("customeimport.properties");
                expression = loadAllProperties.getProperty("custome.importselector.expression");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    
        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
            //1.定义扫描包的名称
            String[] basePackages = null;
            //2.判断有@Import注解的类上是否有@ComponentScan注解
            if (importingClassMetadata.hasAnnotation(ComponentScan.class.getName())) {
                //3.取出@ComponentScan注解的属性
                Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName());
                //4.取出属性名称为basePackages属性的值
                basePackages = (String[]) annotationAttributes.get("basePackages");
            }
            //5.判断是否有此属性(如果没有ComponentScan注解则属性值为null,如果有ComponentScan注解,则basePackages默认为空数组)
            if (basePackages == null || basePackages.length == 0) {
                String basePackage = null;
                try {
                    //6.取出包含@Import注解类的包名
                    basePackage = Class.forName(importingClassMetadata.getClassName()).getPackage().getName();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                //7.存入数组中
                basePackages = new String[] {basePackage};
            }
            //8.创建类路径扫描器
            ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false);
            //9.创建类型过滤器(此处使用切入点表达式类型过滤器)
            TypeFilter typeFilter = new AspectJTypeFilter(expression,this.getClass().getClassLoader());
            //10.给扫描器加入类型过滤器
            scanner.addIncludeFilter(typeFilter);
            //11.扫描指定包
            scanner.scan(basePackages);
        }
    }
    

    7.4、原理分析

    我们写的自定义导入器的解析写在了ConfigurationClassParser类中的processImports方法,以下是源码节选:
    private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass,
                Collection<SourceClass> importCandidates, boolean checkForCircularImports) {
    
            if (importCandidates.isEmpty()) {
                return;
            }
    
            if (checkForCircularImports && isChainedImportOnStack(configClass)) {
                this.problemReporter.error(new CircularImportProblem(configClass, this.importStack));
            }
            else {
                this.importStack.push(configClass);
                try {
                    for (SourceClass candidate : importCandidates) {
                //对ImportSelector的处理
                        if (candidate.isAssignable(ImportSelector.class)) {
                            // Candidate class is an ImportSelector -> delegate to it to determine imports
                            Class<?> candidateClass = candidate.loadClass();
                            ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class);
                            ParserStrategyUtils.invokeAwareMethods(
                                    selector, this.environment, this.resourceLoader, this.registry);
                            if (this.deferredImportSelectors != null && selector instanceof DeferredImportSelector) {
                    //如果为延迟导入处理则加入集合当中
                                this.deferredImportSelectors.add(
                                        new DeferredImportSelectorHolder(configClass, (DeferredImportSelector) selector));
                            }
                            else {
                    //根据ImportSelector方法的返回值来进行递归操作
                                String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());
                                Collection<SourceClass> importSourceClasses = asSourceClasses(importClassNames);
                                processImports(configClass, currentSourceClass, importSourceClasses, false);
                            }
                        }
                        else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
                            // Candidate class is an ImportBeanDefinitionRegistrar ->
                            // delegate to it to register additional bean definitions
                            Class<?> candidateClass = candidate.loadClass();
                            ImportBeanDefinitionRegistrar registrar =
                                    BeanUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class);
                            ParserStrategyUtils.invokeAwareMethods(
                                    registrar, this.environment, this.resourceLoader, this.registry);
                            configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata());
                        }
                        else {
                  // 如果当前的类既不是ImportSelector也不是ImportBeanDefinitionRegistar就进行@Configuration的解析处理
                            // Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar ->
                            // process it as an @Configuration class
                            this.importStack.registerImport(
                                    currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());
                            processConfigurationClass(candidate.asConfigClass(configClass));
                        }
                    }
                }
                catch (BeanDefinitionStoreException ex) {
                    throw ex;
                }
                catch (Throwable ex) {
                    throw new BeanDefinitionStoreException(
                            "Failed to process import candidates for configuration class [" +
                            configClass.getMetadata().getClassName() + "]", ex);
                }
                finally {
                    this.importStack.pop();
                }
            }
        }
    

    8、自定义PropertySourceFactory实现YAML文件解析

    8.1、PropertySourceFactory及DefaultPropertySourceFactory

    /**
     * Strategy interface for creating resource-based {@link PropertySource} wrappers.
     *
     * @author Juergen Hoeller
     * @since 4.3
     * @see DefaultPropertySourceFactory
     */
    public interface PropertySourceFactory {
    
    	/**
    	 * Create a {@link PropertySource} that wraps the given resource.
    	 * @param name the name of the property source
    	 * @param resource the resource (potentially encoded) to wrap
    	 * @return the new {@link PropertySource} (never {@code null})
    	 * @throws IOException if resource resolution failed
    	 */
    	PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException;
    
    }
    
    /**
     * The default implementation for {@link PropertySourceFactory},
     * wrapping every resource in a {@link ResourcePropertySource}.
     *
     * @author Juergen Hoeller
     * @since 4.3
     * @see PropertySourceFactory
     * @see ResourcePropertySource
     */
    public class DefaultPropertySourceFactory implements PropertySourceFactory {
    
    	@Override
    	public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
    		return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));
    	}
    
    }
    

    8.2、执行过程分析

    8.2.1、ResourcePropertySource
    /**
     *  DefaultPropertySourceFactory在创建PropertySource对象时使用的是此类的构造函数
     *  在构造时,调用的
     */
    public class ResourcePropertySource extends PropertiesPropertySource {
    
    	/**
    	 * 当我们没有指定名称时,执行的是此构造函数,此构造函数调用的是父类的构造函数
    	 * 通过读取父类的构造函数,得知第二个参数是一个properties文件
    	 * spring使用了一个工具类获取properties文件
    	 */
    	public ResourcePropertySource(EncodedResource resource) throws IOException {
    		super(getNameForResource(resource.getResource()), PropertiesLoaderUtils.loadProperties(resource));
    		this.resourceName = null;
    	}
    	
        //其他方法略
    }
    
    8.2.2、PropertiesPropertySource
    
    /**
     * 此类是ResourcePropertySource的父类
     */
    public class PropertiesPropertySource extends MapPropertySource {
    	
      	/**
      	 * 此构造函数中包含两个参数:第一个是名称。第二个是解析好的properties文件
      	 */
    	@SuppressWarnings({"unchecked", "rawtypes"})
    	public PropertiesPropertySource(String name, Properties source) {
    		super(name, (Map) source);
    	}
    
    	protected PropertiesPropertySource(String name, Map<String, Object> source) {
    		super(name, source);
    	}
    
    }
    
    8.2.3、PropertiesLoaderUtils
    /**
     * 获取properties的工具类
     */
    public abstract class PropertiesLoaderUtils {
    
    	private static final String XML_FILE_EXTENSION = ".xml";
    
    
    	/**
    	 * ResourcePropertySource类中的构造函数就是执行了此方法
    	 */
    	public static Properties loadProperties(EncodedResource resource) throws IOException {
    		Properties props = new Properties();
    		fillProperties(props, resource);
    		return props;
    	}
    
    	/**
    	 * 
    	 */
    	public static void fillProperties(Properties props, EncodedResource resource)
    			throws IOException {
    
    		fillProperties(props, resource, new DefaultPropertiesPersister());
    	}
    
    	/**
    	 * 通过此方法源码,可以看出spring在使用@PropertySource注解时,支持xml和properties文件
    	 */
    	static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)
    			throws IOException {
    
    		InputStream stream = null;
    		Reader reader = null;
    		try {
    			String filename = resource.getResource().getFilename();
    			if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
    				stream = resource.getInputStream();
                    //读取xml文件
    				persister.loadFromXml(props, stream);
    			}
    			else if (resource.requiresReader()) {
    				reader = resource.getReader();
                    //读取properties文件
    				persister.load(props, reader);
    			}
    			else {
    				stream = resource.getInputStream();
    				persister.load(props, stream);
    			}
    		}
    		finally {
    			if (stream != null) {
    				stream.close();
    			}
    			if (reader != null) {
    				reader.close();
    			}
    		}
    	}
    	//其他代码略
    }
    
    8.2.4、PropertiesPersister
    
    /**
     * spring中声明的解析xml和properties文件的接口
     */
    public interface PropertiesPersister {
    
    	
    	/**
    	 * Load properties from the given Reader into the given
    	 * Properties object.
    	 * @param props the Properties object to load into
    	 * @param reader the Reader to load from
    	 * @throws IOException in case of I/O errors
    	 */
    	void load(Properties props, Reader reader) throws IOException;
    
    	/**
    	 * Load properties from the given XML InputStream into the
    	 * given Properties object.
    	 * @param props the Properties object to load into
    	 * @param is the InputStream to load from
    	 * @throws IOException in case of I/O errors
    	 * @see java.util.Properties#loadFromXML(java.io.InputStream)
    	 */
    	void loadFromXml(Properties props, InputStream is) throws IOException;
      
      	//其他代码略
    }
    
    /** 
     *  PropertiesPersister接口的实现类
     */
    public class DefaultPropertiesPersister implements PropertiesPersister {
    
    
    	@Override
    	public void load(Properties props, Reader reader) throws IOException {
    		props.load(reader);
    
    	@Override
    	public void loadFromXml(Properties props, InputStream is) throws IOException {
    		props.loadFromXML(is);
    	}
    
    	//其他代码略
    }
    

    8.3、自定义PropertySourceFactory

    我们通过分析@PropertySource源码,得知默认情况下此注解只能解析properties文件和xml文件,而遇到yaml(yml)文件,解析就会报错。此时就需要我们自己编写一个PropertySourceFactory的实现类,借助yaml解析器,实现yml文件的解析。

    8.3.1、编写yml配置文件
    jdbc:
     driver: com.mysql.jdbc.Driver
     url: jdbc:mysql://localhost:3306/spring_day01
     username: root
     password: 1234
    
    8.3.2、导入yaml解析器的坐标
    <!-- yaml解析器 https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
    <dependency>
      <groupId>org.yaml</groupId>
      <artifactId>snakeyaml</artifactId>
      <version>1.23</version>
    </dependency>
    
    8.3.3、编写自定义PropertySourceFactory
    /**
     * 自定义资源文件解析器,用于解析yaml yml文件
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public class CustomerPropertySourceFactory implements PropertySourceFactory {
    
        /**
         * 重写接口中的方法
         * @param name
         * @param resource
         * @return
         * @throws IOException
         */
        @Override
        public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
            //1.创建yaml文件解析工厂
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            //2.设置资源内容
            yaml.setResources(resource.getResource());
            //3.解析成properties文件
            Properties properties = yaml.getObject();
            //4.返回符合spring的PropertySource对象
            return name != null ? new PropertiesPropertySource(name,properties) : new PropertiesPropertySource(resource.getResource().getFilename(), properties);
    
        }
    }
    
    8.3.4、使用@PropertyeSource的factory属性配置自定义工厂
    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @Configuration
    @Import(JdbcConfig.class)
    @PropertySource(value = "classpath:jdbc.yml",factory = CustomerPropertySourceFactory.class)
    @ComponentScan("com.itheima")
    public class SpringConfiguration {
    }
    
    /**
     * 连接数据库的配置
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public class JdbcConfig {
    
        @Value("${jdbc.driver}")
        private String driver;
        @Value("${jdbc.url}")
        private String url;
        @Value("${jdbc.username}")
        private String username;
        @Value("${jdbc.password}")
        private String password;
    
    
        @Bean("jdbcTemplate")
        public JdbcTemplate createJdbcTemplate(DataSource dataSource){
            return new JdbcTemplate(dataSource);
        }
    
    
        @Bean("dataSource")
        public DataSource createDataSource(){
            System.out.println(driver);
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            return dataSource;
        }
    }
    
    8.3.5、测试运行结果
    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public class SpringAnnotationDrivenTest {
    
        /**
         * 测试
         * @param args
         */
        public static void main(String[] args) {
            ApplicationContext ac = new AnnotationConfigApplicationContext("config");
        }
    }
    

    9、@Profile注解的使用

    9.1、使用场景分析

    	@Profile注解是spring提供的一个用来标明当前运行环境的注解。我们正常开发的过程中经常遇到的问题是,开发环境是一套环境,测试是一套环境,线上部署又是一套环境。这样从开发到测试再到部署,会对程序中的配置修改多次,尤其是从测试到上线这个环节,让测试的也不敢保证改了哪个配置之后能不能在线上运行。为了解决上面的问题,我们一般会使用一种方法,就是针对不同的环境进行不同的配置,从而在不同的场景中跑我们的程序。
      而spring中的@Profile注解的作用就体现在这里。在spring使用DI来注入的时候,能够根据当前制定的运行环境来注入相应的bean。最常见的就是使用不同的DataSource了。
    

    9.2、代码实现

    9.2.1、自定义不同环境的数据源
    package config;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Profile;
    
    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    public class JdbcConfig {
    
        @Value("${jdbc.driver}")
        private String driver;
        @Value("${jdbc.url}")
        private String url;
        @Value("${jdbc.username}")
        private String username;
        @Value("${jdbc.password}")
        private String password;
    
    
    
    
        /**
         * @return
         */
        @Bean("dataSource")
        @Profile("dev")
        public DruidDataSource createDevDataSource(){
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setMaxActive(10);
            return dataSource;
        }
    
        /**
         * @return
         */
        @Bean("dataSource")
        @Profile("test")
        public DruidDataSource createTestDataSource(){
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setMaxActive(50);
            return dataSource;
        }
    
        /**
         * @return
         */
        @Bean("dataSource")
        @Profile("produce")
        public DruidDataSource createProduceDataSource(){
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setMaxActive(100);
            return dataSource;
        }
    }
    
    9.2.2、编写配置类
    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @Configuration
    @Import(JdbcConfig.class)
    public class SpringConfiguration {
    }
    
    9.2.3、编写测试类
    /**
     * @author 黑马程序员
     * @Company http://www.itheima.com
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfiguration.class)
    @ActiveProfiles("test")
    public class SpringProfileTest {
    
        @Autowired
        private DruidDataSource druidDataSource;
    
        @Test
        public void testProfile(){
            System.out.println(druidDataSource.getMaxActive());
        }
    }
    
    9.2.4、测试结果

  • 相关阅读:
    USACO 4.1 Fence Rails
    POJ 1742
    LA 2031
    uva 10564
    poj 3686
    LA 3350
    asp.net MVC 3多语言方案--再次写, 配源码
    使用Log4net记录日志
    在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
    为什么要使用反射机制
  • 原文地址:https://www.cnblogs.com/weijiqian/p/16394283.html
Copyright © 2020-2023  润新知