
cglib的原理是生成一个被代理类的子类进行增强, 那么为什么子类访问不到父类的属性呢
@Service
public class AopTestService {
    public String value = "hello world";
    @Transactional
    public void imTest() {
        Object obj = SpringUtil.getBean(this.getClass());
        boolean bool1 = AopUtils.isAopProxy(obj);
        boolean bool2 = AopUtils.isAopProxy(this);
        System.err.println(StrUtil.format("bool1:{}, bool2:{}, value:{}", bool1, bool2, this.value));
    }
    @Transactional
    public final void noImTest() {
        Object obj = SpringUtil.getBean(this.getClass());
        boolean bool1 = AopUtils.isAopProxy(obj);
        boolean bool2 = AopUtils.isAopProxy(this);
        System.err.println(StrUtil.format("bool1:{}, bool2:{}, value:{}", bool1, bool2, this.value));
        System.err.println(this.getValue());
    }
    public String getValue() {
        return value;
    }
}@Autowired
AopTestService aopTestService;
@PostConstruct
public void test() {
    aopTestService.imTest();
    aopTestService.noImTest();
    System.err.println("test: " + aopTestService.value);
}输出
bool1:true, bool2:false, value:hello world
bool1:true, bool2:true, value:null
hello world
test: nullpublic final void imTest() {       
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;       
    if (var10000 == null) {            
        CGLIB$BIND_CALLBACKS(this);            
        var10000 = this.CGLIB$CALLBACK_0;       
    }        
    if (var10000 != null) {            
        var10000.intercept(this, CGLIB$imTest$2$Method, CGLIB$emptyArgs, CGLIB$imTest$2$Proxy);
    } else {            
        super.imTest();       
    }  
}所以, bool2为false, this.value正常输出
2. 第二行: bool2为true, 因为noImTest方法被final修饰, 无法被代理增强, 所以最终是通过cglib生成的子类去调用父类AopTestService的noImTest方法. 但是this.value输出null, 这是因为cglib生成的子类对象, 是通过objenesis这个库实例化的, objenesis这个库的作用是绕过构造方法实例化对象. 所以对象没有正常的初始化, 父类的value属性也就没有了
3. 第三行, this.getValue输出了. 这个方法也是被子类重写了, 最终也是通过源类AopTestService的对象去调用对应方法, 所以能够输出
public final String getValue() {       
    MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;       
    if (var10000 == null) {            
        CGLIB$BIND_CALLBACKS(this);            
        var10000 = this.CGLIB$CALLBACK_0;       
    }       
    return var10000 != null ? 
    (String)var10000.intercept(this, CGLIB$getValue$0$Method, CGLIB$emptyArgs, CGLIB$getValue$0$Proxy) 
    : super.getValue();  
}
4. 第四行: 原因同第二行
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。