1. 程式人生 > 實用技巧 >BeanFactory後置處理器 - ConfigurationClassPostProcessor#postProcessBeanFactory

BeanFactory後置處理器 - ConfigurationClassPostProcessor#postProcessBeanFactory

demo和springIOC - ConfigurationClassPostProcessor - full / lite裡面是同一個.

暫且先不管 full , lite 有什麼作用, 也不管spring是如何做到的, 如果是自己做, 怎麼可以實現那種效果.

demo:

public class Parent {

    public IndexDao1 indexDao1() {
        System.out.println("parent indexDao1");
        return new IndexDao1();
    }


    public IndexDao2 indexDao2() {
        System.out.println(
"parent indexDao2"); indexDao1(); return new IndexDao2(); } }

這裡, 我有兩個方法 indexDao1 和 indexDao2, 方法indexDao2中呼叫了 indexDao1方法. 現在這種情況, 會導致 IndexDao1 被建立兩次.

那麼通過什麼辦法, 可以讓 IndexDao1 只建立一次呢?

方法一:

再寫一個類Son, 來繼承 Parent 類, 然後通過 重寫他的 indexDao1 和 indexDao2 方法來改變他們的行為.

同時, 我還需要引入一個map, 來儲存建立的物件. 在執行方法之前, 先從map中獲取

-> 獲取到了, 則直接返回這個物件

-> 沒獲取到, 則執行父類中的方法, 來獲取物件, 存入 map 中.

public class Son extends Parent {
     Map<String , Object> map = new HashMap<>();

    @Override
    public  IndexDao1 indexDao1() {
        System.out.println("son indexDao1");
        if(map.get("indexDao1") != null){
            
return (IndexDao1) map.get("indexDao1"); } IndexDao1 indexDao1 = super.indexDao1(); map.put("indexDao1", indexDao1); return indexDao1; } @Override public IndexDao2 indexDao2() { System.out.println("son indexDao2"); //Parent中, 執行的 indexDao1() 已經被 Son 重寫, 所以會執行 Son 中的 indexDao1() if(map.get("indexDao2") != null){ return (IndexDao2) map.get("indexDao2"); } IndexDao2 indexDao2 = super.indexDao2(); map.put("indexDao2", indexDao2); return indexDao2; } }

測試程式碼:

public static void main(String[] args) {
    printf();

    Son son = new Son();
    son.indexDao1();
    printf();
    son.indexDao2();
}

private static void printf(){
    System.out.println("================");
}

結果:

方法二:

通過cglib代理的方式, 來建立一個繼承Parent的類也是可以達到效果的.

public class ParentMethodInceptor implements MethodInterceptor {
    Map<String, Object> map = new HashMap<>();

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("proxy --- " + method.getName());
        if (map.get(method.getName()) != null) {
            return map.get(method.getName());
        }

        Object res = methodProxy.invokeSuper(obj, args);

        map.put(method.getName(), res);
        return res;
    }
}

測試程式碼:

public static void main(String[] args) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(Parent.class);
    enhancer.setCallback(new ParentMethodInceptor());
    Parent proxy = (Parent) enhancer.create();

    printf();
    proxy.indexDao1();
    printf();
    proxy.indexDao2();
}

private static void printf(){
    System.out.println("================");
}

結果:

如果 Parent 類中的 indexDao1 變成一個靜態方法: public static IndexDao1 indexDao1{}

那麼不管是方法一,還是方法二, 都是不能實現只建立一次的效果.

方法一 中, 不能對靜態方法進行重寫覆蓋

方法二 中, 不能對靜態方法進行代理

不過, 我測試過 spring , 他也是辦不到的, 也會建立兩次.

原始碼:

ConfigurationClassPostProcessor 的postProcessBeanDefinitionRegistry 呼叫結束之後,

就會呼叫他的postProcessBeanFactory 方法.

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    int factoryId = System.identityHashCode(beanFactory);
    if (this.factoriesPostProcessed.contains(factoryId)) {
        throw new IllegalStateException(
                "postProcessBeanFactory already called on this post-processor against " + beanFactory);
    }
    this.factoriesPostProcessed.add(factoryId);
    if (!this.registriesPostProcessed.contains(factoryId)) {
        // BeanDefinitionRegistryPostProcessor hook apparently not supported...
        // Simply call processConfigurationClasses lazily at this point then.
        processConfigBeanDefinitions((BeanDefinitionRegistry) beanFactory);
    }
    //給配置類產生cglib代理
    //為什麼需要產生cglib代理?
    enhanceConfigurationClasses(beanFactory);
    beanFactory.addBeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory));
}

除了產生了一次 cglib 代理外, 還向容器中註冊了一個後置處理器:ImportAwareBeanPostProcessor

這裡主要還是看這個 cglib 代理, 它是根據是否有 @Configuration 註解來判斷是否要進行的.

此例中, 主要是對 配置類 StartConfig 產生cglib代理的.

那麼為什麼要對 StartConfig 產生 cglib 代理呢?

是不是想要達到 上面 demo 中的效果呢? 讓 IndexDao1 只建立一遍?

public void enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFactory) {
    Map<String, AbstractBeanDefinition> configBeanDefs = new LinkedHashMap<>();
   //解析拿到容器中的配置類(加了@Configuration註解的), 放在 configBeanDefs 中
for (String beanName : beanFactory.getBeanDefinitionNames()) { BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName); //判斷是否是一個全註解類 //掃描是全註解類?full和lite的關係 if (ConfigurationClassUtils.isFullConfigurationClass(beanDef)) { if (!(beanDef instanceof AbstractBeanDefinition)) { throw new BeanDefinitionStoreException("Cannot enhance @Configuration bean definition '" + beanName + "' since it is not stored in an AbstractBeanDefinition subclass"); } else if (logger.isInfoEnabled() && beanFactory.containsSingleton(beanName)) { logger.info("Cannot enhance @Configuration bean definition '" + beanName + "' since its singleton instance has been created too early. The typical cause " + "is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor " + "return type: Consider declaring such methods as 'static'."); } configBeanDefs.put(beanName, (AbstractBeanDefinition) beanDef); } } if (configBeanDefs.isEmpty()) { // nothing to enhance -> return immediately return; } ConfigurationClassEnhancer enhancer = new ConfigurationClassEnhancer(); for (Map.Entry<String, AbstractBeanDefinition> entry : configBeanDefs.entrySet()) { AbstractBeanDefinition beanDef = entry.getValue(); // If a @Configuration class gets proxied, always proxy the target class beanDef.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE); try { // Set enhanced subclass of the user-specified bean class Class<?> configClass = beanDef.resolveBeanClass(this.beanClassLoader); if (configClass != null) { //完成對全註解類的cglib代理 Class<?> enhancedClass = enhancer.enhance(configClass, this.beanClassLoader); if (configClass != enhancedClass) { if (logger.isTraceEnabled()) { logger.trace(String.format("Replacing bean definition '%s' existing class '%s' with " + "enhanced class '%s'", entry.getKey(), configClass.getName(), enhancedClass.getName())); } beanDef.setBeanClass(enhancedClass); } } } catch (Throwable ex) { throw new IllegalStateException("Cannot load configuration class: " + beanDef.getBeanClassName(), ex); } } }

從上面這段程式碼來看, 只有載入了 @Configuration 的配置類, 才會進行cglib代理.

那前面的 full / lite 屬性設定的意義, 在這裡就體現出來了.

1. full 模式下, 會進行配置類的 cglib 代理.

2. lite模式下, 此處就直接返回了.

enhance

org.springframework.context.annotation.ConfigurationClassEnhancer#enhance

public Class<?> enhance(Class<?> configClass, @Nullable ClassLoader classLoader) {
    //判斷是否被代理過
    if (EnhancedConfiguration.class.isAssignableFrom(configClass)) {
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Ignoring request to enhance %s as it has " +
                    "already been enhanced. This usually indicates that more than one " +
                    "ConfigurationClassPostProcessor has been registered (e.g. via " +
                    "<context:annotation-config>). This is harmless, but you may " +
                    "want check your configuration and remove one CCPP if possible",
                    configClass.getName()));
        }
        return configClass;
    }
    //沒有被代理cglib代理
    Class<?> enhancedClass = createClass(newEnhancer(configClass, classLoader));
    if (logger.isTraceEnabled()) {
        logger.trace(String.format("Successfully enhanced %s; enhanced class name is: %s",
                configClass.getName(), enhancedClass.getName()));
    }
    return enhancedClass;
}

EnhancedConfiguration

這裡通過 EnhancedConfiguration 來判斷, 配置類是否被代理過.

為什麼能這麼判斷呢?

org.springframework.context.annotation.ConfigurationClassEnhancer.EnhancedConfiguration

public interface EnhancedConfiguration extends BeanFactoryAware {}

看原始碼, 知道他是一個空介面, 啥也不幹.

如果在進行 cglib 代理的時候, 讓生成的動態代理類實現這個介面, 那麼就可以通過判斷配置類是否實現這個介面, 來確定是否被代理了.

newEnhancer

這裡做了建立代理類前的屬性配置

org.springframework.context.annotation.ConfigurationClassEnhancer#newEnhancer

private Enhancer newEnhancer(Class<?> configSuperClass, @Nullable ClassLoader classLoader) {
    Enhancer enhancer = new Enhancer();
    //增強父類,cglib是基於繼承來的
    enhancer.setSuperclass(configSuperClass);
    //增強介面,為什麼要增強介面?
    //便於判斷,表示一個類以及被增強了
    enhancer.setInterfaces(new Class<?>[] {EnhancedConfiguration.class});
    //不繼承Factory介面
    enhancer.setUseFactory(false);
    enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
    // BeanFactoryAwareGeneratorStrategy是一個生成策略
    // 主要為生成的CGLIB類中新增成員變數$$beanFactory
    // 同時基於介面EnhancedConfiguration的父介面BeanFactoryAware中的setBeanFactory方法,
    // 設定此變數的值為當前Context中的beanFactory,這樣一來我們這個cglib代理的物件就有了beanFactory
    //有了factory就能獲得物件,而不用去通過方法獲得物件了,因為通過方法獲得物件不能控制器過程
    //該BeanFactory的作用是在this呼叫時攔截該呼叫,並直接在beanFactory中獲得目標bean
    enhancer.setStrategy(new BeanFactoryAwareGeneratorStrategy(classLoader));
    //過濾方法,不能每次都去new
    enhancer.setCallbackFilter(CALLBACK_FILTER);
    enhancer.setCallbackTypes(CALLBACK_FILTER.getCallbackTypes());
    return enhancer;
}

從這段程式碼看, spring確實是, 將 EnhancedConfiguration 放到了動態代理類中. 讓動態代理類實現它.

createClass

正式建立代理類, 設定 MethodInterceptor

org.springframework.context.annotation.ConfigurationClassEnhancer#createClass

private Class<?> createClass(Enhancer enhancer) {
    Class<?> subclass = enhancer.createClass();
    // Registering callbacks statically (as opposed to thread-local)
    // is critical for usage in an OSGi environment (SPR-5932)...
    Enhancer.registerStaticCallbacks(subclass, CALLBACKS);
    return subclass;
}

org.springframework.context.annotation.ConfigurationClassEnhancer#CALLBACKS

private static final Callback[] CALLBACKS = new Callback[] {
    //增強方法,主要控制 bean 的作用域
    //不每一次都去呼叫new
    new BeanMethodInterceptor(),
    //設定一個beanFactory
    new BeanFactoryAwareMethodInterceptor(),
    NoOp.INSTANCE
};

BeanMethodInterceptor實現了MethodInterceptor

熟悉cglib的應該知道, 他是一個方法攔截器, 會對被代理的目標方法進行攔截包裝.

其效果跟 上面demo中的方法二一樣. 可以改變目標方法的邏輯

BeanMethodInterceptor

public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object[] beanMethodArgs,
            MethodProxy cglibMethodProxy) throws Throwable {

    //enhancedConfigInstance 代理
    // 通過enhancedConfigInstance中cglib生成的成員變數$$beanFactory獲得beanFactory。
    ConfigurableBeanFactory beanFactory = getBeanFactory(enhancedConfigInstance);
    String beanName = BeanAnnotationHelper.determineBeanNameFor(beanMethod);

    // Determine whether this bean is a scoped-proxy
    if (BeanAnnotationHelper.isScopedProxy(beanMethod)) {
        String scopedBeanName = ScopedProxyCreator.getTargetBeanName(beanName);
        if (beanFactory.isCurrentlyInCreation(scopedBeanName)) {
            beanName = scopedBeanName;
        }
    }

    // To handle the case of an inter-bean method reference, we must explicitly check the
    // container for already cached instances.

    // First, check to see if the requested bean is a FactoryBean. If so, create a subclass
    // proxy that intercepts calls to getObject() and returns any cached bean instance.
    // This ensures that the semantics of calling a FactoryBean from within @Bean methods
    // is the same as that of referring to a FactoryBean within XML. See SPR-6602.
    if (factoryContainsBean(beanFactory, BeanFactory.FACTORY_BEAN_PREFIX + beanName) &&
            factoryContainsBean(beanFactory, beanName)) {
        Object factoryBean = beanFactory.getBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName);
        if (factoryBean instanceof ScopedProxyFactoryBean) {
            // Scoped proxy factory beans are a special case and should not be further proxied
        }
        else {
            // It is a candidate FactoryBean - go ahead with enhancement
            return enhanceFactoryBean(factoryBean, beanMethod.getReturnType(), beanFactory, beanName);
        }
    }

    //一個非常牛逼的判斷
    //判斷到底是new 還是get
    //判斷執行的方法和呼叫方法是不是同一個方法
    if (isCurrentlyInvokedFactoryMethod(beanMethod)) {
        // The factory is calling the bean method in order to instantiate and register the bean
        // (i.e. via a getBean() call) -> invoke the super implementation of the method to actually
        // create the bean instance.
        if (logger.isInfoEnabled() &&
                BeanFactoryPostProcessor.class.isAssignableFrom(beanMethod.getReturnType())) {
            logger.info(String.format("@Bean method %s.%s is non-static and returns an object " +
                            "assignable to Spring's BeanFactoryPostProcessor interface. This will " +
                            "result in a failure to process annotations such as @Autowired, " +
                            "@Resource and @PostConstruct within the method's declaring " +
                            "@Configuration class. Add the 'static' modifier to this method to avoid " +
                            "these container lifecycle issues; see @Bean javadoc for complete details.",
                    beanMethod.getDeclaringClass().getSimpleName(), beanMethod.getName()));
        }
     //呼叫父類的相應方法
return cglibMethodProxy.invokeSuper(enhancedConfigInstance, beanMethodArgs); } return resolveBeanReference(beanMethod, beanMethodArgs, beanFactory, beanName); }

isCurrentlyInvokedFactoryMethod

//org.springframework.context.annotation.ConfigurationClassEnhancer.BeanMethodInterceptor#isCurrentlyInvokedFactoryMethod
private boolean isCurrentlyInvokedFactoryMethod(Method method) {
    Method currentlyInvoked = SimpleInstantiationStrategy.getCurrentlyInvokedFactoryMethod();
    return (currentlyInvoked != null && method.getName().equals(currentlyInvoked.getName()) &&
            Arrays.equals(method.getParameterTypes(), currentlyInvoked.getParameterTypes()));
}

在上面的demo中, 判斷是否要執行建立過程的邏輯是: 從map中獲取物件, 有則不建立, 沒有則建立.

顯然, spring在這裡, 並不是這麼幹的.

執行 indexDao1() 時, 通過除錯可以知道, method 是indexDao1(), currentlyInvoked 也是 indexDao1().

執行 indexDao2() 時, 會進入此方法兩次, 第一次是對 indexDao2() 本身的攔截, 第二次是對 他呼叫的 indexDao1() 的攔截.

第一次時:method 是indexDao2(), currentlyInvoked 也是 indexDao2().

第二次時:method 是indexDao1(), 但是 currentlyInvoked 卻變成了 indexDao2().

從這裡可以看出, spring 是通過方法名, 來進行是否要走父類方法的判斷的.

resolveBeanReference

這裡是通過spring工廠來獲取物件的. 暫時可以理解成, 上面demo中的map.

private Object resolveBeanReference(Method beanMethod, Object[] beanMethodArgs,
        ConfigurableBeanFactory beanFactory, String beanName) {

    // The user (i.e. not the factory) is requesting this bean through a call to
    // the bean method, direct or indirect. The bean may have already been marked
    // as 'in creation' in certain autowiring scenarios; if so, temporarily set
    // the in-creation status to false in order to avoid an exception.
    //判斷他是否正在建立
    boolean alreadyInCreation = beanFactory.isCurrentlyInCreation(beanName);
    try {
        if (alreadyInCreation) {
            beanFactory.setCurrentlyInCreation(beanName, false);
        }
        boolean useArgs = !ObjectUtils.isEmpty(beanMethodArgs);
        if (useArgs && beanFactory.isSingleton(beanName)) {
            // Stubbed null arguments just for reference purposes,
            // expecting them to be autowired for regular singleton references?
            // A safe assumption since @Bean singleton arguments cannot be optional...
            for (Object arg : beanMethodArgs) {
                if (arg == null) {
                    useArgs = false;
                    break;
                }
            }
        }
        //beanFactory.getBean 通過容器來拿
        Object beanInstance = (useArgs ? beanFactory.getBean(beanName, beanMethodArgs) :
                beanFactory.getBean(beanName));
        if (!ClassUtils.isAssignableValue(beanMethod.getReturnType(), beanInstance)) {
            // Detect package-protected NullBean instance through equals(null) check
            if (beanInstance.equals(null)) {
                if (logger.isDebugEnabled()) {
                    logger.debug(String.format("@Bean method %s.%s called as bean reference " +
                            "for type [%s] returned null bean; resolving to null value.",
                            beanMethod.getDeclaringClass().getSimpleName(), beanMethod.getName(),
                            beanMethod.getReturnType().getName()));
                }
                beanInstance = null;
            }
            else {
                String msg = String.format("@Bean method %s.%s called as bean reference " +
                        "for type [%s] but overridden by non-compatible bean instance of type [%s].",
                        beanMethod.getDeclaringClass().getSimpleName(), beanMethod.getName(),
                        beanMethod.getReturnType().getName(), beanInstance.getClass().getName());
                try {
                    BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName);
                    msg += " Overriding bean of same name declared in: " + beanDefinition.getResourceDescription();
                }
                catch (NoSuchBeanDefinitionException ex) {
                    // Ignore - simply no detailed message then.
                }
                throw new IllegalStateException(msg);
            }
        }
        Method currentlyInvoked = SimpleInstantiationStrategy.getCurrentlyInvokedFactoryMethod();
        if (currentlyInvoked != null) {
            String outerBeanName = BeanAnnotationHelper.determineBeanNameFor(currentlyInvoked);
            beanFactory.registerDependentBean(beanName, outerBeanName);
        }
        return beanInstance;
    }
    finally {
        if (alreadyInCreation) {
            beanFactory.setCurrentlyInCreation(beanName, true);
        }
    }
}