spring注解源码分析-解析和注入注解配置的资源

类内部的注解,如@Autowire、@Value、@Required、@Resource以及EJB和WebService相关的注解,是容器对Bean实例化和依赖注入时,通过容器中注册的Bean后置处理处理这些注解的。

当使用Spring的注解功能时,

 

<context:annotation-config>  
< context:component-scan >


上面的配置将隐式地向Spring容器注册:CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor以及这4个专门用于处理注解的Bean后置处理器。

 

 

AutowiredAnnotationBeanPostProcessor

是spring容器专门处理配置了自动依赖注入装配相关注解的Bean后置处理器。

(1)构造函数

public AutowiredAnnotationBeanPostProcessor() {  
        //后置处理器将处理@Autowire注解  
        this.autowiredAnnotationTypes.add(Autowired.class);  
        //后置处理器将处理@Value注解  
        this.autowiredAnnotationTypes.add(Value.class);  
        //获取当前类的类加载器  
        ClassLoader cl = AutowiredAnnotationBeanPostProcessor.class.getClassLoader();  
        try {  
            //后置处理器将处理javax.inject.Inject JSR-330注解  
            this.autowiredAnnotationTypes.add((Class<? extends Annotation>) cl.loadClass("javax.inject.Inject"));  
            logger.info("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring");  
        }  
        catch (ClassNotFoundException ex) {  
            // JSR-330 API not available - simply skip.  
        }  
    } 

 

(2)指定类选择合适的构造方法

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

 

容器对指定类进行自动依赖注入装配时,容器需要对Bean调用合适的构造方法创建实例对象,AutowiredAnnotationBeanPostProcessor为指定类选择相应的构造方法。

 

(3)对属性的依赖注入

 

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

(4)对字段进行注入

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

(5)对方法进行注入

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

 

CommonAnnotationBeanPostProcessor

是对JavaEE中常用注解的处理,可以处理@PostConstruct、@PreDestroy等,处理最核心的是@Resource注解。

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

 

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

 

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

 

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

 

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 
//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

RequiredAnnotationBeanPostProcessor

处理@Required注解,@Required注解强制要求Bean属性必须被配置,当Spring容器对Bean属性进行依赖注入时,配置了@Required注解的属性,Spring容器会检查依赖关系是否设置。

 

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

 

 

PersistenceAnnotationBeanPostProcessor

用于处理JPA相关注解的Bean后置处理器,主要解析和处理@PersistenceUnit @PersistenceContext注解,其主要作用是为JPA的实体管理器工厂和实体管理器注入相应的持久化单元或上下文。

 

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 
//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

 

 

 

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 

 

//为自动依赖注入装配Bean选择合适的构造方法  
public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {  
        //首先从容器的缓存中查找是否有指定Bean的构造方法  
        Constructor[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
        //容器缓存中没有给定类的构造方法  
        if (candidateConstructors == null) {  
            //线程同步以确保容器中数据一致性  
            synchronized (this.candidateConstructorsCache) {  
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);  
                if (candidateConstructors == null) {  
                    //通过JDK反射机制,获取指定类的中所有声明的构造方法  
                    Constructor[] rawCandidates = beanClass.getDeclaredConstructors();  
                    //存放候选构造方法的集合  
                    List<Constructor> candidates = new ArrayList<Constructor>(rawCandidates.length);  
                    //autowire注解中required属性指定的构造方法  
                    Constructor requiredConstructor = null;  
                    //默认的构造方法  
                    Constructor defaultConstructor = null;  
                    //遍历所有的构造方法,检查是否添加了autowire注解,以及是否  
//指定了required属性  
                    for (Constructor<?> candidate : rawCandidates) {  
                        //获取指定类中所有关于autowire的注解(Annotation)  
                        Annotation annotation = findAutowiredAnnotation(candidate);  
                        //如果指定类中有关于antowire的注解  
                        if (annotation != null) {  
                            //如果antowire注解中指定了required属性  
                            if (requiredConstructor != null) {  
                                throw new BeanCreationException("Invalid autowire-marked constructor: " + candidate +  
                                        ". Found another constructor with 'required' Autowired annotation: " +  
                                        requiredConstructor);  
                            }  
                            //如果autowire注解的参数列表为空  
                            if (candidate.getParameterTypes().length == 0) {  
                                throw new IllegalStateException(  
                                        "Autowired annotation requires at least one argument: " + candidate);  
                            }  
                            //获取autowire注解中required属性值  
                            boolean required = determineRequiredStatus(annotation);  
                            //如果获取到autowire注解中required的属性值  
                            if (required) {  
                                //如果候选构造方法集合不为空  
                                if (!candidates.isEmpty()) {  
                                    throw new BeanCreationException(  
                                            "Invalid autowire-marked constructors: " + candidates +  
                                            ". Found another constructor with 'required' Autowired annotation: " +  
                                            requiredConstructor);  
                                }  
                            //当前的构造方法就是required属性所配置的构造方法  
                                requiredConstructor = candidate;  
                            }  
                            //将当前的构造方法添加到哦啊候选构造方法集合中  
                            candidates.add(candidate);  
                        }  
                       //如果类中没有autowire的相关注解,并且构造方法参数列表为空  
                        else if (candidate.getParameterTypes().length == 0) {  
                            //当前的构造方法是默认构造方法  
                            defaultConstructor = candidate;  
                        }  
                    }  
                    //如果候选构造方法集合不为空  
                    if (!candidates.isEmpty()) {  
                    //如果所有的构造方法都没有配置required属性,且有默认构造方法  
                        if (requiredConstructor == null && defaultConstructor != null) {  
                            //将默认构造方法添加到候选构造方法列表  
                            candidates.add(defaultConstructor);  
                        }  
                        //将候选构造方法集合转换为数组  
                        candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);  
                    }  
                    //如果候选构造方法集合为空,则创建一个空的数组  
                    else {  
                        candidateConstructors = new Constructor[0];  
                    }  
                    //将类的候选构造方法集合存放到容器的缓存中  
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);  
                }  
            }  
        }  
        //返回指定类的候选构造方法数组,如果没有返回null  
        return (candidateConstructors.length > 0 ? candidateConstructors : null);  
    } 


注解只不过是利用JDK的反射机制,在编译时或者运行时动态获取所配置的信息而已,注解的真正意义在于通过注解标识获取注解所在对象的信息以及注解中配置的信息。

 

最后欢迎大家访问我的个人网站:1024s

​​​​​​​

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值