spring源码解析----AbstractAutowireCapableBeanFactory类applyBeanPostProcessorsAfterInitialization

本文探讨了Spring框架中的AbstractAutowireCapableBeanFactory类如何应用BeanPostProcessorsAfterInitialization过程。在Spring容器中,BeanPostProcessor允许自定义处理,尤其在集成第三方框架时。当涉及AOP切面时,Spring会对目标类创建代理,以实现额外的功能。以一个具体的例子展示了如何在运行时,AnnotationAwareAspectJAutoProxyCreator将原始bean转换为由CGLIB生成的代理bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AbstractAutowireCapableBeanFactory.java部分代码如下

    //对bean应用后置处理器中的postProcessAfterInitialization方法
    @Override
	public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
			throws BeansException {

		Object result = existingBean;
		for (BeanPostProcessor processor : getBeanPostProcessors()) {
			Object current = processor.postProcessAfterInitialization(result, beanName);
			if (current == null) {
				return result;
			}
			result = current;
		}
		return result;
	}

 

 

查看spring容器管理的BeanPostProcessor,BeanPostProcessor可以手动添加自定义的BeanPostProcessor,在引入第三方框架的时候可能也会增加相关的BeanPostProcessor

如果在使用过程中,定义了切面,即采用了aop,则对对应的类会进行代理,即相关的代理操作,进而生成相应的代理类。

示例如下:

项目代码结构:

MyAsPect.java代码如下:

package com.yd.scan.wsnew.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author levey
 * @date 2020/12/10
 */
@Component
@Aspect
public class MyAspect {
    @Pointcut("execution(public * com.yd.scan.wsnew.controller.MyController.*(..))")
    public void funcPoint(){
    }
    @Before("funcPoint()")
    public void before(){
        System.out.println("````````````````````before");
    }
    @Around("funcPoint()")
    public void aroud(){
        System.out.println("````````````````````aroud");
    }
}

SecAspect.java代码如下:

package com.yd.scan.wsnew.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author levey
 * @date 2020/12/11
 */
@Component
@Aspect
public class SecAspect {

    @Pointcut("execution(public * com.yd.scan.wsnew.controller.TestRefreshSpring.*(..))")
    public void secPoint(){
    }
}

MyController代码如下:

package com.yd.scan.wsnew.controller;

import com.yd.scan.wsnew.bean.avro.Scan;
import com.yd.scan.wsnew.bean.avro.req.getsigntype.Req4Getsigntype;
import com.yd.scan.wsnew.bean.avro.res.Head;
import com.yd.scan.wsnew.bean.avro.res.getsigntype.Res4Getsigntype;
import com.yd.scan.wsnew.bean.avro.res.getsigntype.type;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Collections;

@RequestMapping
@Controller
public class MyController {
    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        return "test";
    }

    @RequestMapping("/seravro")
    @ResponseBody
    public Res4Getsigntype seravro(@RequestBody Req4Getsigntype req4Getsigntype) throws IOException {
        System.out.println(req4Getsigntype.getHead().getVer());
        Res4Getsigntype res4Getsigntype=new Res4Getsigntype();
        Head h=new Head();
        type t1=new type();t1.setName("本人");
        res4Getsigntype.setData(Collections.singletonList(t1));
        res4Getsigntype.setHead(h);
        return res4Getsigntype;
    }

    @RequestMapping("/scan")
    @ResponseBody
    public Scan scan(@RequestBody Scan scan) throws IOException {
        return scan;
    }

    @RequestMapping("/scanp")
    @ResponseBody
    public com.yd.scan.wsnew.bean.proto.Scan.Result  scanp(@RequestBody com.yd.scan.wsnew.bean.proto.Scan.Result result) throws IOException {
        return result;
    }
}

在运行到如下的BeanPostProcessor【此处为AnnotationAwareAspectJAutoProxyCreator】的方法时,将原生的普通bean【此处为result】赋值成了一个代理的bean【此处为current】,看得出这里是用的cglib进行的动态代理,因为是对类进行的代理,而非接口

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值