spring 주해 소스 코드 분석 - 주해 설정 자원 분석 및 주입

74326 단어 스프링
클래스 내부 의 주해, 예 를 들 어 @ Autowire, @ Value, @ Required, @ Resource 및 EJB 와 WebService 와 관련 된 주 해 는 용기 가 Bean 에 대한 실례 화 와 의존 주입 시 용기 에 등 록 된 Bean 을 통 해 이 주 해 를 처리 합 니 다.
Spring 의 주석 기능 을 사용 할 때,
 
  
< context:component-scan >

위의 설정 은 Spring 용기 에 암시 적 으로 등록 합 니 다: CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor 및 이 4 개의 주 해 를 처리 하 는 Bean 백 엔 드 프로세서 입 니 다.
 
 
AutowiredAnnotationBeanPostProcessor
spring 용기 전문 처리 입 니 다.
(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 candidates = new ArrayList(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 의 반사 메커니즘 을 이용 하여 컴 파일 할 때 나 실행 할 때 설 정 된 정 보 를 동적 으로 얻 는 것 에 불과 하 다. 주해 의 진정한 의 미 는 주해 표 지 를 통 해 주해 가 있 는 대상 의 정보 와 주해 에 설 정 된 정 보 를 얻 는 것 이다.
 
마지막 으로 제 개인 홈 페이지 방문 을 환영 합 니 다: 1024 s
​​​​​​​

좋은 웹페이지 즐겨찾기