问题描述
我们有一个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看看
controller的serviceImpl 
进入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.method2的this为代理对象(子类)的情况
2.为什么cglib代理对象获取不到@Value注入的值?
当 controller注入serviceImpl时,开始创建serviceImpl,populateBean这个方法里会处理@Value的注入,此时处理的对象类型是org.springframework.cloud.context.scope.GenericScope.LockedScopedProxyFactoryBean,而cglib代理对象是在下个方法initializeBean中才生成的,所以这个过程不会有@Value的什么事,代理对象的@Value的字段只能是默认值
源码跟踪
1.@RefreshScope注解影响BeanDefinition
@RefreshScope其实等价于@Scope("refresh"),在spring扫描时会生成两个BeanDefinition,比如上面的serviceImpl,会生成一个scopedTarget.serviceImpl的BeanDefinition和一个serviceImpl的BeanDefinition 
2.controller注入serviceImpl代理对象
当创建controller需要注入serviceImpl时,开始创建serviceImpl,拿到的是org.springframework.cloud.context.scope.GenericScope.LockedScopedProxyFactoryBean的BeanDefinition,接下来createBeanInstance实例化,populateBean设置属性,initializeBean初始化(包含创建代理) 



其中proxy是在initializeBean的invokeAwareMethods的当前bean的setBeanFactory方法生成的,即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的,所以可以成功被拦截