问题描述
我们有一个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
的,所以可以成功被拦截