Spring 动态代理 @Value=null ?

问题描述

我们有一个spirngboot项目准备把Apollo配置中心换成Nacos配置中心,替换过程中遇到个问题,使用某个service对象调用service中的方法,出现了获取不到@Value的值的情况,以下是根据当时的代码整理出来的伪代码:

java 复制代码
package com.lewis.demo.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Lewis
 */
@RestController
@RequestMapping
public class Controller {

    @Autowired
    private ServiceImpl serviceImpl;

    @GetMapping("/method1")
    public void method1() {
        serviceImpl.method1();
    }

    @GetMapping("/method2")
    public void method2() {
        serviceImpl.method2();
    }

}
java 复制代码
package com.lewis.demo.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

/**
 * @author Lewis
 */
@RefreshScope
@Service
public class ServiceImpl {

    @Value("${test.value}")
    private String value;

    public void method1() {
        System.out.println("value = " + value);
    }

    public final void method2() {
        System.out.println("value = " + value);
    }
}

调用接口/method1,输出的是:value = value,调用method2,输出的是:value = null

为什么method1能够正常获取到value的值,而method2却获取不到呢?细心的同学应该已经看到两个方法有什么不同了,没错,罪魁祸首就是method2相比method1多了个final,那么问题就来了,为什么会这样?

接下来我们进入debug看看

controllerserviceImpl

进入serviceImpl.method1

进入serviceImpl.method2

从图中可以看到,controller中注入的serviceImpl是cglib代理对象,进入serviceImpl.method1时,this是原本的serviceImpl对象,而进入serviceImpl.method2时,this是cglib代理对象,为什么会这样呢?

我从调用method1的栈帧往前推,看到了org.springframework.aop.framework.CglibAopProxy.DynamicAdvisedInterceptor#intercept这个方法,注意看,target = targetSource.getTarget();这个target就是要调用的目标对象(serviceImpl)

再看看method2的,发现并没有走到上面method1提到的代码,栈帧少了好几个,从controller直接就到了serviceImpl

先说结论

1.调用method2时,this为什么是代理对象?

serviceImpl使用了@RefreshScope@RefreshScope默认会使用cglib代理,也就是继承目标对象,而继承是不会重写final修饰的方法的,明白这一点很重要。当使用代理对象调用重写的方法时,代理对象拿到目标对象对目标方法进行调用,而使用代理对象调用非重写方法时,只能直接调用父类方法,所以就出现了调用serviceImpl.method2this为代理对象(子类)的情况

2.为什么cglib代理对象获取不到@Value注入的值?

controller注入serviceImpl时,开始创建serviceImplpopulateBean这个方法里会处理@Value的注入,此时处理的对象类型是org.springframework.cloud.context.scope.GenericScope.LockedScopedProxyFactoryBean,而cglib代理对象是在下个方法initializeBean中才生成的,所以这个过程不会有@Value的什么事,代理对象的@Value的字段只能是默认值

源码跟踪

1.@RefreshScope注解影响BeanDefinition

@RefreshScope其实等价于@Scope("refresh"),在spring扫描时会生成两个BeanDefinition,比如上面的serviceImpl,会生成一个scopedTarget.serviceImplBeanDefinition和一个serviceImplBeanDefinition

2.controller注入serviceImpl代理对象

当创建controller需要注入serviceImpl时,开始创建serviceImpl,拿到的是org.springframework.cloud.context.scope.GenericScope.LockedScopedProxyFactoryBeanBeanDefinition,接下来createBeanInstance实例化,populateBean设置属性,initializeBean初始化(包含创建代理)

其中proxy是在initializeBeaninvokeAwareMethods的当前beansetBeanFactory方法生成的,即org.springframework.aop.scope.ScopedProxyFactoryBean#setBeanFactory

3.调用目标对象被代理方法

如果目标对象方法被代理,则会被拦截,进入org.springframework.cglib.proxy.MethodInterceptor#intercept,这里对应的是实现是org.springframework.aop.framework.CglibAopProxy.DynamicAdvisedInterceptor#intercept,最终走到org.springframework.cloud.context.scope.GenericScope.LockedScopedProxyFactoryBean#invoke反射调用目标方法。

总结

1.没有特殊情况需要,尽量不要给方法加上final,这样代理对象就能成功拦截这些方法的调用,避免出现意想不到的情况 2.使用@RefreshScope的情况下,如果目标方法是重写接口的方法,则可以指定@RefreshScope(proxyMode = ScopedProxyMode.INTERFACES),因为接口中的方法只会是public且非final的,所以可以成功被拦截

相关推荐
EnigmaCoder14 分钟前
Java多线程:核心技术与实战指南
java·开发语言
攀小黑17 分钟前
阿里云 使用TST Token发送模板短信
java·阿里云
麦兜*23 分钟前
Spring Boot秒级冷启动方案:阿里云FC落地实战(含成本对比)
java·spring boot·后端·spring·spring cloud·系统架构·maven
自由鬼1 小时前
正向代理服务器Squid:功能、架构、部署与应用深度解析
java·运维·服务器·程序人生·安全·架构·代理
fouryears_234172 小时前
深入拆解Spring核心思想之一:IoC
java·后端·spring
codervibe2 小时前
使用 Spring Boot + JWT 实现多角色登录认证(附完整流程图)
java·后端
坚持学习永不言弃2 小时前
Ehcache、Caffeine、Memcached和Redis缓存
java
阿劲2 小时前
从业务卡顿到数据库连接池耗尽:Spring Boot项目HikariCP超时问题实战排查
java·后端·面试
亮1112 小时前
Maven 编译过程中发生了 Java Heap Space 内存溢出(OutOfMemoryError)
java·开发语言·maven
添乱2 小时前
「Java案例」求PI的值
java