Spring 动态代理 @Value=null ?

问题描述

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

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();
    }

}
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的,所以可以成功被拦截

  • 14
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="addr" class="cn.sxt.vo.Address"> <property name="address" value="北京西三旗"/> </bean> <bean id="student" class="cn.sxt.vo.Student"> <property name="name" value="张三丰"></property> <property name="addr" ref="addr"/> <property name="books"> <array> <value>傲慢与偏见</value> <value>巴黎圣母院</value> <value>仲夏夜之梦</value> </array> </property> <property name="hobbies"> <list> <value>羽毛球</value> <value>乒乓球</value> <value>玻璃球</value> <value>溜溜球</value> </list> </property> <property name="cards"> <map> <entry key="交通银行" value="20200302"></entry> <entry> <key> <value>中信银行</value> </key> <value>6222620222266667</value> </entry> </map> </property> <property name="games"> <set> <value>lol</value> <value>第五人格</value> <value>王者荣耀</value> <value>五子棋</value> </set> </property> <property name="wife"> <null/> </property> <property name="info"> <props> <prop key="学号">20160233</prop> <prop key="sex">女</prop> <prop key="name">小球</prop> </props> </property> </bean> <!-- p命名空间注入属性依然要设置set方法 --> <bean id="user" class="cn.sxt.vo.User" p:name="风清扬" p:age="230"/> <!--c命名空间注入要求有对应参数的构造方法 --> <bean id="u1" class="cn.sxt.vo.User" c:name="Lynn" c:age="18"/> </beans>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值