BeanFactoryAdvisorRetrievalHelper: Advisor were retrieved from the Bean factory

This class is very important, it is really to find out all the container classes Advisor

Spring AOP is a BeanFactoryAdvisorRetrievalHelper internal tools, the tools used, all that is acquired in the Spring BeanFactory bean from the bean container Advisor.

The tool uses an internal caching mechanism, although public search method may be called multiple times, but not every time a real find, but will use the cache.

public  class BeanFactoryAdvisorRetrievalHelper {
     Private  static  Final the Log Logger = LogFactory.getLog (BeanFactoryAdvisorRetrievalHelper. class ); 

    Private  Final ConfigurableListableBeanFactory beanFactory;
     / ** 
    * local field will make a simple buffer 
    * / 
    Private  volatile String [] cachedAdvisorBeanNames; 


    / ** 
     * the Create the new new BeanFactoryAdvisorRetrievalHelper for gIVEN a the beanFactory. 
     * Create a BeanFactoryAdvisorRetrievalHelper for a given instance of the beanFactory 
     * / 
    public BeanFactoryAdvisorRetrievalHelper (ConfigurableListableBeanFactory beanFactory) {
        Assert.notNull (beanFactory, "Not ListableBeanFactory MUST BE null" );
         the this .beanFactory = beanFactory; 
    } 


    / ** 
     * in the Find All eligible Beans The Advisor Current Factory the bean, 
     * core method, all of the acquired Advisors, 
     * / 
    public List < Advisor> findAdvisorBeans () {
         // . List of the Determine Advisor bean names, IF Not already cached 
        String [] = advisorNames the this .cachedAdvisorBeanNames;
         IF (== advisorNames null ) {
             // Get parent container bean container and all Spring Advisor bean name 
            advisorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors (
                     the this. .beanFactory, Advisor class , to true , to false );
             // the acquired Advisor bean name recorded this.cachedAdvisorBeanNames 
            the this .cachedAdvisorBeanNames = advisorNames; 
        } 
        IF (advisorNames.length == 0 ) {
             return  new new the ArrayList <Advisor> (); 
        } 

        List <Advisor> Advisors = new new the ArrayList <Advisor> ();
         for (String name: advisorNames) {
              // for obtaining from the container to the top of all the Advisor bean, check whether they qualify,
             //Check here qualified by the predicate logic isEligibleBean () is completed
             // isEligibleBean () in the default implementation of this class is: always returns to true,
             // actually class can be inherited, and then rewriting isEligibleBean () method implementation own
             // qualify checking logic 
            IF (isEligibleBean (name)) {
                 // ignore the bean is being created in the 
                IF ( the this .beanFactory.isCurrentlyInCreation (name)) {
                     IF (logger.isDebugEnabled ()) { 
                        logger.debug ( " Currently Created Advisor Skipping ' "+ name +"' " ); 
                    } 
                } 
                the else{
                     The try {
                         BeanCurrentlyInCreationException) {// met a truly qualified bean, instantiate it, and then put advisors in
                         // if encountered in the process of creating an exception because it relies bean is being created, then to ignore the bean,
                         // if it is other exceptions thrown, the current interrupt method 
                        advisors.add ( the this .beanFactory.getBean (name, Advisor. class )); 
                    } 
                    the catch (BeanCreationException EX) { 
                        the Throwable rootCause = ex.getMostSpecificCause ();
                         IF (rootCause the instanceof 
                            BeanCreationException BCE = (BeanCreationException) rootCause;
                             IF (this.beanFactory.isCurrentlyInCreation(bce.getBeanName())) {
                                if (logger.isDebugEnabled()) {
                                    logger.debug("Skipping advisor '" + name +
                                            "' with dependency on currently created bean: " + ex.getMessage());
                                }
                                // Ignore: indicates a reference back to the bean we're trying to advise.
                                // We want to find advisors other than the currently created bean itself.
                                continue;
                            }
                        }
                        throw ex;
                    }
                }
            }
        }
        return advisors;
    }

    /**
     * Determine whether the aspect bean with the given name is eligible.
     * <p>The default implementation always returns {@code true}.
     * @param beanName the name of the aspect bean
     * @return whether the bean is eligible
     */
    protected boolean isEligibleBean(String beanName) {
        return true;
    }

}

Guess you like

Origin www.cnblogs.com/mayang2465/p/12141610.html