spring4.1.8扩展实战之七:控制bean(BeanPostProcessor接口)

简介: 实战在容器初始化的时候对bean实例做设置

欢迎访问我的GitHub

这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos
  • 本章是《spring4.1.8扩展实战》的第七篇,我们来尝试在容器初始化的时候对bean实例做设置;

控制bean的两种扩展方式

  • 两种方式可以对bean做控制(例如修改某个成员变量)
  1. 改变bean的定义(BeanFactoryPostProcessor接口) ,可以想象成修改了class文件,这样实例化出来的每个对象都变了;
  2. 只改变实例化的对象(BeanPostProcessor接口);

本章概览

  • 全文由以下部分组成:
  1. spring源码分析,了解BeanPostProcessor接口的实现类如何被注册到spring容器;
  2. spring源码分析,了解已经注册到spring环境的BeanPostProcessor实现类如何被使用;
  3. 实战,开发一个BeanPostProcessor实现类,验证是否能用来改变指定的bean;

源码分析:BeanPostProcessor接口的实现类如何被注册到spring容器

  • 从spring容器的初始化代码看起吧,看AbstractApplicationContext类的refresh方法,如下图所示,红框中的方法负责将BeanPostProcessor接口的实现类注册到spring容器:

image.png

  • 展开registerBeanPostProcessors方法,发现是委托PostProcessorRegistrationDelegate类的静态方法registerBeanPostProcessors来完成注册工作的:
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
  • 展开PostProcessorRegistrationDelegate.registerBeanPostProcessors方法,里面的代码逻辑简洁整齐,意图目的一目了然:
public static void registerBeanPostProcessors(
        ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
    //获取所有实现BeanPostProcessor接口的bean的名称
    String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

    //注意,此时尽管注册操作还没有开始,但是之前已经有一些特殊的bean已经注册进来了,
    //详情请看AbstractApplicationContext类的prepareBeanFactory方法,
    //因此getBeanPostProcessorCount()方法返回的数量并不为零,
    //加一是因为方法末尾会注册一个ApplicationListenerDetector接口的实现类
    int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
    //这里的BeanPostProcessorChecker也是个BeanPostProcessor的实现类,用于每个bean的初始化完成后,做一些简单的检查
    beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

    //如果这些bean还实现了PriorityOrdered接口(在意执行顺序),就全部放入集合priorityOrderedPostProcessors 
    List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
    //集合internalPostProcessors,用来存放同时实现了PriorityOrdered和MergedBeanDefinitionPostProcessor接口的bean
    List<BeanPostProcessor> internalPostProcessors = new ArrayList<BeanPostProcessor>();
    //集合orderedPostProcessorNames用来存放实现了Ordered接口的bean的名称(在意执行顺序)
    List<String> orderedPostProcessorNames = new ArrayList<String>();
    //集合nonOrderedPostProcessorNames用来存放即没实现PriorityOrdered接口,也没有实现Ordered接口的bean的名称(不关心执行顺序)
    List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
    for (String ppName : postProcessorNames) {
        if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
            //实现了PriorityOrdered接口的bean,都放入集合priorityOrderedPostProcessors
            priorityOrderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                //实现了MergedBeanDefinitionPostProcessor接口的bean,都放入internalPostProcessors集合
                internalPostProcessors.add(pp);
            }
        }
        else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            //实现了Ordered接口的bean,将其名称都放入orderedPostProcessorNames集合
            orderedPostProcessorNames.add(ppName);
        }
        else {
            //既没实现PriorityOrdered接口,也没有实现Ordered接口的bean,将其名称放入nonOrderedPostProcessorNames集合
            nonOrderedPostProcessorNames.add(ppName);
        }
    }

    //实现了PriorityOrdered接口的bean排序
    OrderComparator.sort(priorityOrderedPostProcessors);
    //注册到容器
    registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

    List<BeanPostProcessor> orderedPostProcessors = new ArrayList<BeanPostProcessor>();
    //处理所有实现了Ordered接口的bean
    for (String ppName : orderedPostProcessorNames) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        orderedPostProcessors.add(pp);
        //前面将所有实现了PriorityOrdered和MergedBeanDefinitionPostProcessor的bean放入internalPostProcessors,
        //此处将所有实现了Ordered和MergedBeanDefinitionPostProcessor的bean放入internalPostProcessors
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }

    //实现了Ordered接口的bean排序
    OrderComparator.sort(orderedPostProcessors);
    //注册到容器
    registerBeanPostProcessors(beanFactory, orderedPostProcessors);

    List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanPostProcessor>();
    for (String ppName : nonOrderedPostProcessorNames) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        nonOrderedPostProcessors.add(pp);
        //此处将其余实现了MergedBeanDefinitionPostProcessor的bean放入internalPostProcessors
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    //注册到容器
    registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

    OrderComparator.sort(internalPostProcessors);
    //将所有实现了MergedBeanDefinitionPostProcessor接口的bean也注册到容器
    registerBeanPostProcessors(beanFactory, internalPostProcessors);
    //创建一个ApplicationListenerDetector对象并且注册到容器,这就是前面计算beanProcessorTargetCount的值时加一的原因
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}
  • 以上代码已加注释,就不多解读了,有一点需要注意:对于实现了MergedBeanDefinitionPostProcessor接口的bean,在前面几次调用registerBeanPostProcessors方法的时候已经注册过了,那么在最后执行的registerBeanPostProcessors(beanFactory, internalPostProcessors),岂不是将一个bean注册了多次?
  • 为了弄清楚这个问题需要看registerBeanPostProcessors方法:
private static void registerBeanPostProcessors(
        ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {

    for (BeanPostProcessor postProcessor : postProcessors) {
        //对每个bean都调用beanFactory.addBeanPostProcessor方法来注册
        beanFactory.addBeanPostProcessor(postProcessor);
    }
}
  • addBeanPostProcessor方法的代码在AbstractApplicationContext类中,如下所示,先删除再添加,这样反复注册也没有问题
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
    Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
    //如果已经注册,就先删除掉
    this.beanPostProcessors.remove(beanPostProcessor);
    //再注册
    this.beanPostProcessors.add(beanPostProcessor);
    if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
        this.hasInstantiationAwareBeanPostProcessors = true;
    }
    if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
        this.hasDestructionAwareBeanPostProcessors = true;
    }
}
  • 至此,我们已经弄清楚了BeanPostProcessor实现类的bean注册到spring容器的逻辑,接下来看看spring容器如何使用这些bean;

源码分析,BeanPostProcessor实现类如何被使用

  • 要弄清楚BeanPostProcessor接口的实现类是在哪里被用到的,还是从负责容器初始化的AbstractApplicationContext类的refresh方法看起,如下图红框中的finishBeanFactoryInitialization方法,就是负责实例化和初始化bean的:

image.png

  • 从finishBeanFactoryInitialization方法到BeanPostProcessor的实现类被使用,中间有多层逻辑和调用,篇幅所限就不逐个展开了,直接列出堆栈信息,您可以根据此信息去查看对应源码:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean()
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean()
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean()
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject()
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton()
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean()
org.springframework.beans.factory.support.AbstractBeanFactory.getBean()
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons()
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization()
org.springframework.context.support.AbstractApplicationContext.refresh()
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh()
org.springframework.boot.SpringApplication.refresh()
org.springframework.boot.SpringApplication.refreshContext()
org.springframework.boot.SpringApplication.run()
org.springframework.boot.SpringApplication.run()
org.springframework.boot.SpringApplication.run()
  • 根据上述堆栈信息,直接查看AbstractAutowireCapableBeanFactory类的initializeBean()方法:
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
    if (System.getSecurityManager() != null) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                invokeAwareMethods(beanName, bean);
                return null;
            }
        }, getAccessControlContext());
    }
    else {
        invokeAwareMethods(beanName, bean);
    }

    Object wrappedBean = bean;
    if (mbd == null || !mbd.isSynthetic()) {
        //对已经实例化的bean,在初始化前用BeanPostProcessor实现类去处理
        wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    }

    try {
        //bean的初始化
        invokeInitMethods(beanName, wrappedBean, mbd);
    }
    catch (Throwable ex) {
        throw new BeanCreationException(
                (mbd != null ? mbd.getResourceDescription() : null),
                beanName, "Invocation of init method failed", ex);
    }

    if (mbd == null || !mbd.isSynthetic()) {
        //对已经实例化的bean,在初始化后用BeanPostProcessor实现类去处理
        wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    }
    return wrappedBean;
}
  • 如上所示,我们最关注的代码是applyBeanPostProcessorsBeforeInitializationapplyBeanPostProcessorsAfterInitialization这两个方法,它们分别在bean的初始化方法invokeInitMethods的前后被执行;
  • 先看看applyBeanPostProcessorsBeforeInitialization方法,逻辑非常简单,就是取出所有已注册的BeanPostProcessor实现类,执行其postProcessBeforeInitialization方法,入参是当前正在做实例化和初始化的bean实例:
@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
        throws BeansException {

    Object result = existingBean;
    //逐一执行每个BeanPostProcessor实现类的postProcessBeforeInitialization方法
    for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
        result = beanProcessor.postProcessBeforeInitialization(result, beanName);
        if (result == null) {
            return result;
        }
    }
    return result;
}
  • 在invokeInitMethods方法执行完毕后会执行applyBeanPostProcessorsAfterInitialization方法,代码如下,与applyBeanPostProcessorsBeforeInitialization如出一辙,仅有的不同是调用的beanProcessor的方法变成了postProcessAfterInitialization:
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
        throws BeansException {

    Object result = existingBean;
    //逐一执行每个BeanPostProcessor实现类的postProcessAfterInitialization方法
    for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
        result = beanProcessor.postProcessAfterInitialization(result, beanName);
        if (result == null) {
            return result;
        }
    }
    return result;
}
  • 以上就是spring容器初始化过程中对BeanPostProcessor实现类的使用场景,此时还剩一点疑问需要澄清:在分析注册过程的时候,曾看到实现MergedBeanDefinitionPostProcessor接口的bean是最后注册的,那么这些MergedBeanDefinitionPostProcessor实现类在spring容器中是何处使用的呢?
  • 为了搞清这个问题,来看看AbstractAutowireCapableBeanFactory类的doCreateBean方法,前面我们分析的initializeBean方法就是在这里面被调用的:

image.png

  • 如上图所示,红框中就是用所有MergedBeanDefinitionPostProcessor实现类去处理当前正在实例化的bean,然后才会执行绿框中的initializeBean方法(里面是我们刚才分析的bean的初始化,BeanPostProcessor实现类被使用的逻辑);
  • 来看看红框中的applyMergedBeanDefinitionPostProcessors方法,主要目的是处理特殊的合成bean的定义类:
protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName)
            throws BeansException {

    try {
        //取出每个注册的BeanPostProcessor实现类
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            //只有实现了MergedBeanDefinitionPostProcessor接口才执行
            if (bp instanceof MergedBeanDefinitionPostProcessor) {
                MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
                //执行postProcessMergedBeanDefinition
                bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
            }
        }
    }
    catch (Exception ex) {
        throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                "Post-processing failed of bean type [" + beanType + "] failed", ex);
    }
}
  • 至此,我们对BeanPostProcessor有关的源码分析就完成了,小结一下:
  1. 初始化时,spring容器有特别处理,会直接调用beanFactory.addBeanPostProcessor进行注册(例如AbstractApplicationContext类的prepareBeanFactory方法中就有);
  2. 找出所有实现了BeanPostProcessor接口的bean,注册到容器,注册顺序如下:

第一:实现了PriorityOrdered接口的,排序后;
第二:实现了Ordered接口的,排序后;
第三:既没实现PriorityOrdered接口,也没有实现Ordered接口的;
第四:实现了MergedBeanDefinitionPostProcessor接口的(这些也按照PriorityOrdered、Ordered等逻辑拍过续);
第五:实例化一个ApplicationListenerDetector对象;

  1. 实例化bean的时候,对于每个bean,先用MergedBeanDefinitionPostProcessor实现类的postProcessMergedBeanDefinition方法处理每个bean的定义类;
  2. 再用BeanPostProcessor的postProcessBeforeInitialization方法处理每个bean实例;
  3. bean实例初始化;
  4. 用BeanPostProcessor的postProcessAfterInitialization方法处理每个bean实例;
  • 源码分析结束,接下来自定义一个BeanPostProcessor实现类,验证我们之前的分析:控制bean实例;

实战自定义BeanPostProcessor实现类

  • 本次实战的内容是创建一个springboot工程,在里面自定义一个BeanPostProcessor接口的实现类,如果您不想敲代码,也可以去github下载源码,地址和链接信息如下表所示:
名称 链接 备注
项目主页 https://github.com/zq2599/blog_demos 该项目在GitHub上的主页
git仓库地址(https) https://github.com/zq2599/blog_demos.git 该项目源码的仓库地址,https协议
git仓库地址(ssh) git@github.com:zq2599/blog_demos.git 该项目源码的仓库地址,ssh协议
  • 这个git项目中有多个文件夹,本章源码在文件夹customizebeanpostprocessor下,如下图红框所示:

image.png

  • 接下来开始实战吧:
  • 基于maven创建一个springboot的web工程,名为customizebeanpostprocessor,pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bolingcavalry</groupId>
    <artifactId>customizebeanpostprocessor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>customizebeanpostprocessor</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.15.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 创建Utils.java,里面有一些静态的工具方法,例如打印当前堆栈:
package com.bolingcavalry.customizebeanpostprocessor.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Utils {

    private static final Logger logger = LoggerFactory.getLogger(Utils.class);

    /**
     * 打印当前线程堆栈信息
     * @param prefix
     */
    public static void printTrack(String prefix){
        StackTraceElement[] st = Thread.currentThread().getStackTrace();

        if(null==st){
            logger.info("invalid stack");
            return;
        }

        StringBuffer sbf =new StringBuffer();

        for(StackTraceElement e:st){
            if(sbf.length()>0){
                sbf.append(" <- ");
                sbf.append(System.getProperty("line.separator"));
            }

            sbf.append(java.text.MessageFormat.format("{0}.{1}() {2}"
                    ,e.getClassName()
                    ,e.getMethodName()
                    ,e.getLineNumber()));
        }

        logger.info(prefix
                + "\n************************************************************\n"
                + sbf.toString()
                + "\n************************************************************");
    }
}
  • 创建接口CalculateService,定义了几个方法:
package com.bolingcavalry.customizebeanpostprocessor.service;

public interface CalculateService {
    /**
     * 整数加法
     * @param a
     * @param b
     * @return
     */
    int add(int a, int b);

    /**
     * 返回当前实现类的描述信息
     * @return
     */
    String getServiceDesc();

    /**
     * 设置当前实现类的描述信息
     * @return
     */
    void setServiceDesc(String serviceDesc);
}
  • 创建CalculateService的实现类CalculateServiceImpl,并且用@Service注解将定义为spring的bean:
package com.bolingcavalry.customizebeanpostprocessor.service.impl;


import com.bolingcavalry.customizebeanpostprocessor.service.CalculateService;
import org.springframework.stereotype.Service;

@Service("calculateService")
public class CalculateServiceImpl implements CalculateService {

    private String serviceDesc = "desc from class";

    @Override
    public int add(int a, int b) {
        return a + b;
    }

    @Override
    public String getServiceDesc() {
        return serviceDesc;
    }

    @Override
    public void setServiceDesc(String serviceDesc) {
        this.serviceDesc = serviceDesc;
    }
}
  • 注意该类的成员变量serviceDesc,值已经固定为"desc from class";
  • 创建Controller类HelloController,在响应web请求的时候,会用到calculateService提供的服务:
package com.bolingcavalry.customizebeanpostprocessor.controller;

import com.bolingcavalry.customizebeanpostprocessor.service.CalculateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired(required = false)
    CalculateService calculateService;


    @GetMapping("/add/{a}/{b}")
    public String add(@PathVariable("a") int a, @PathVariable("b") int b){
        return "add result : " + calculateService.add(a, b) + ", from [" + calculateService.getServiceDesc() + "]";
    }
}
  • 如上所示,正常情况下,web响应为"add result : x, from [desc from class]",这个desc from class,就是CalculateServiceImpl的成员变量serviceDesc的值;
  • 创建BeanPostProcessor的实现类CustomizeBeanPostProcessor,这里面会修改calculateService实例的成员变量serviceDesc的值,记得用注解@Component将其设置为bean:
package com.bolingcavalry.customizebeanpostprocessor.processor;

import com.bolingcavalry.customizebeanpostprocessor.service.CalculateService;
import com.bolingcavalry.customizebeanpostprocessor.util.Utils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class CustomizeBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if("calculateService".equals(beanName)) {
        //打印当前堆栈
            Utils.printTrack("do postProcess before initialization");
            CalculateService calculateService = (CalculateService)bean;
            //修改calculateService实例的成员变量serviceDesc的值
            calculateService.setServiceDesc("desc from " + this.getClass().getSimpleName());
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if("calculateService".equals(beanName)) {
            //打印当前堆栈
            Utils.printTrack("do postProcess after initialization");
        }
        return bean;
    }
}
  • 启动应用,在启动日志中可以看到CustomizeBeanPostProcessor实例的两个方法被调用时打印的堆栈日志,和之前我们分析的源码是一致的:
2018-09-02 18:35:53.618  INFO 22520 --- [           main] c.b.c.util.Utils                         : do postProcess before initialization
************************************************************
java.lang.Thread.getStackTrace() 1,559 <- 
com.bolingcavalry.customizebeanpostprocessor.util.Utils.printTrack() 15 <- 
com.bolingcavalry.customizebeanpostprocessor.processor.CustomizeBeanPostProcessor.postProcessBeforeInitialization() 14 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization() 409 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean() 1,626 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean() 555 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean() 483 <- 
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject() 312 <- 
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton() 230 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean() 308 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean() 202 <- 
org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate() 208 <- 
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency() 1,138 <- 
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency() 1,066 <- 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject() 585 <- 
org.springframework.beans.factory.annotation.InjectionMetadata.inject() 88 <- 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues() 366 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean() 1,272 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean() 553 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean() 483 <- 
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject() 312 <- 
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton() 230 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean() 308 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean() 197 <- 
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons() 761 <- 
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization() 867 <- 
org.springframework.context.support.AbstractApplicationContext.refresh() 543 <- 
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh() 122 <- 
org.springframework.boot.SpringApplication.refresh() 693 <- 
org.springframework.boot.SpringApplication.refreshContext() 360 <- 
org.springframework.boot.SpringApplication.run() 303 <- 
org.springframework.boot.SpringApplication.run() 1,118 <- 
org.springframework.boot.SpringApplication.run() 1,107 <- 
com.bolingcavalry.customizebeanpostprocessor.CustomizebeanpostprocessorApplication.main() 10
************************************************************
2018-09-02 18:35:53.619  INFO 22520 --- [           main] c.b.c.util.Utils                         : do postProcess after initialization
************************************************************
java.lang.Thread.getStackTrace() 1,559 <- 
com.bolingcavalry.customizebeanpostprocessor.util.Utils.printTrack() 15 <- 
com.bolingcavalry.customizebeanpostprocessor.processor.CustomizeBeanPostProcessor.postProcessAfterInitialization() 24 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization() 423 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean() 1,638 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean() 555 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean() 483 <- 
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject() 312 <- 
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton() 230 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean() 308 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean() 202 <- 
org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate() 208 <- 
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency() 1,138 <- 
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency() 1,066 <- 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject() 585 <- 
org.springframework.beans.factory.annotation.InjectionMetadata.inject() 88 <- 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues() 366 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean() 1,272 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean() 553 <- 
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean() 483 <- 
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject() 312 <- 
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton() 230 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean() 308 <- 
org.springframework.beans.factory.support.AbstractBeanFactory.getBean() 197 <- 
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons() 761 <- 
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization() 867 <- 
org.springframework.context.support.AbstractApplicationContext.refresh() 543 <- 
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh() 122 <- 
org.springframework.boot.SpringApplication.refresh() 693 <- 
org.springframework.boot.SpringApplication.refreshContext() 360 <- 
org.springframework.boot.SpringApplication.run() 303 <- 
org.springframework.boot.SpringApplication.run() 1,118 <- 
org.springframework.boot.SpringApplication.run() 1,107 <- 
com.bolingcavalry.customizebeanpostprocessor.CustomizebeanpostprocessorApplication.main() 10
************************************************************
  • 在浏览器访问地址:http://localhost:8080/add/1/2,可以看到响应如下图所示,calculateService的成员变量serviceDesc的值已经被CustomizeBeanPostProcessor改为desc from CustomizeBeanPostProcessor

image.png

  • 至此,本次实战就完成了,从spring源码分析再到动手实战,我们对BeanPostProcessor的扩展有了更深入的认识,也希望这种扩展能帮助您更好的控制bean实例为业务需求服务;

欢迎关注阿里云开发者社区博客:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...
相关文章
|
23天前
|
缓存 安全 Java
Spring框架中Bean是如何加载的?从底层源码入手,详细解读Bean的创建流程
从底层源码入手,通过代码示例,追踪AnnotationConfigApplicationContext加载配置类、启动Spring容器的整个流程,并对IOC、BeanDefinition、PostProcesser等相关概念进行解释
Spring框架中Bean是如何加载的?从底层源码入手,详细解读Bean的创建流程
|
23天前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
123 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
10天前
|
XML Java 数据格式
spring复习02,xml配置管理bean
详细讲解了Spring框架中基于XML配置文件管理bean的各种方式,包括获取bean、依赖注入、特殊值处理、属性赋值、集合类型处理、p命名空间、bean作用域及生命周期和自动装配。
spring复习02,xml配置管理bean
|
10天前
|
XML Java 数据格式
spring复习03,注解配置管理bean
Spring框架中使用注解配置管理bean的方法,包括常用注解的标识组件、扫描组件、基于注解的自动装配以及使用注解后的注意事项,并提供了一个基于注解自动装配的完整示例。
spring复习03,注解配置管理bean
|
25天前
|
存储 数据采集 Java
Spring Boot 3 实现GZIP压缩优化:显著减少接口流量消耗!
在Web开发过程中,随着应用规模的扩大和用户量的增长,接口流量的消耗成为了一个不容忽视的问题。为了提升应用的性能和用户体验,减少带宽占用,数据压缩成为了一个重要的优化手段。在Spring Boot 3中,通过集成GZIP压缩技术,我们可以显著减少接口流量的消耗,从而优化应用的性能。本文将详细介绍如何在Spring Boot 3中实现GZIP压缩优化。
68 6
|
4天前
|
存储 NoSQL Java
Spring Boot项目中使用Redis实现接口幂等性的方案
通过上述方法,可以有效地在Spring Boot项目中利用Redis实现接口幂等性,既保证了接口操作的安全性,又提高了系统的可靠性。
7 0
|
10天前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
2月前
|
缓存 Java Maven
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
|
3月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
下一篇
无影云桌面