解决@Value注解不能注入static修饰的属性问题

目录
  • @Value注解不能注入static属性
    • 问题描述
    • 剖析
  • @Value(“${属性}“)注入被static修饰的属性
    • 问题描述
    • 解决方案

@Value注解不能注入static属性

问题描述

在application.yml中:

constant:
  key: hello
  value: world

工具类ConstantHelper:

@Component
public class ConstantHelper {
    @Value("${constant.value}")
    private static String value;
    private static String key;
    public static String getValue() {
        return value;
    }
    public void setValue(String value) {
        ConstantHelper.value = value;
    }
    public static String getKey() {
        return key;
    }
    @Value("${constant.key}")
    public void setKey(String key) {
        ConstantHelper.key = key;
    }
}

测试类:

@RequestMapping("/getConfig")
public Map<String, Object> getConfig(){
    Map<String,Object> map = new HashMap<>();
    map.put("key", ConstantHelper.getKey());
    map.put("value",ConstantHelper.getValue());
    return map;
}

结果:

{
    "value": null,
    "key": "hello"
}

可以发现,@Value注解放在属性上注入值失败,而@Value放在setter方法上(注意,该方法也不能是静态方法)却能注入成功。为什么??

剖析

答案就在AutowiredAnnotationBeanPostProcessor#buildAutowiringMetadata方法中,AutowiredAnnotationBeanPostProcessor主要处理了@Autowired和@Value注解等等:

private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
        List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
        Class<?> targetClass = clazz;
        do {
            final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>();
            ReflectionUtils.doWithLocalFields(targetClass, field -> {
                AnnotationAttributes ann = findAutowiredAnnotation(field);
                if (ann != null) {
          //here!!
                    if (Modifier.isStatic(field.getModifiers())) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Autowired annotation is not supported on static fields: " + field);
                        }
                        return;
                    }
                    boolean required = determineRequiredStatus(ann);
                    currElements.add(new AutowiredFieldElement(field, required));
                }
            });
            ReflectionUtils.doWithLocalMethods(targetClass, method -> {
                Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
                if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                    return;
                }
                AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);
                if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
          //here!!
                    if (Modifier.isStatic(method.getModifiers())) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Autowired annotation is not supported on static methods: " + method);
                        }
                        return;
                    }
                    if (method.getParameterCount() == 0) {
                        if (logger.isInfoEnabled()) {
                            logger.info("Autowired annotation should only be used on methods with parameters: " +
                                    method);
                        }
                    }
                    boolean required = determineRequiredStatus(ann);
                    PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                    currElements.add(new AutowiredMethodElement(method, required, pd));
                }
            });
            elements.addAll(0, currElements);
            targetClass = targetClass.getSuperclass();
        }
        while (targetClass != null && targetClass != Object.class);
        return new InjectionMetadata(clazz, elements);
    }

The conceptual problem here is that annotation-driven injection happens for each bean instance. So we shouldn’t inject static fields or static methods there because that would happen for every instance of that class. The injection lifecycle is tied to the instance lifecycle, not to the class lifecycle. Bridging between an instance’s state and static accessor - if really desired - is up to the concrete bean implementation but arguably shouldn’t be done by the framework itself.

从源码上发现,理论上spring是可以对静态域注入的,只是spring没有这样做,它认为依赖注入发生的时段是在实例的生命周期,而不是类的生命周期

@Value(“${属性}“)注入被static修饰的属性

场景:

通过httpclient调用第三方接口的时候,ip和端口不确定

需求:

写一个工具类,可以动态配置ip和端口来修改调用的地址和端口,要求工具类方法可以静态调用。

问题描述

static 不能和注解并用,被static修饰的成员变量,无法通过@Value注解动态获取到

解决方案

通过注入到set方法实现属性动态赋值

application.yml配置:

key:
  box:
    ip: 192.168.1.166
    port: 9987

错误代码:

@Value("${key.box.ip}")
private static String ip ;
@Value("${key.box.port}")
private static String port;

这样写的话,你会发现拿到的结果还是null

正确代码:

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
 * Created in 2021/6/29 15:07
 * @author
 */
@Slf4j
@Component
public class KeyBoxHttpClientUtil {
    private static String ip ;
    private static String port;
    @Value("${key.box.ip}")
    public void setIP(String ip) {
        KeyBoxHttpClientUtil.ip = ip;
    }
    @Value("${key.box.port}")
    public void setPort(String port) {
        KeyBoxHttpClientUtil.port = port;
    }
}

Tips:调整代码之后,工具类必须使用@Component注解来修饰,否则依然无法获取到结果。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • springboot使用@value注入配置失败的解决

    目录 springboot使用@value注入配置文件失败 问题解决方向一 问题解决方向二 @Value注入失败,注入值为null的问题 大概就是下面这样 结果不知道为什么,@Value注入一直为空?? 原因如下 解决办法 springboot使用@value注入配置文件失败 遇到的问题原因是:类中注入对象不能用static. 问题解决方向一 1.改为如图示,去掉static 问题解决方向二 1.仍然定义静态变量,但在其set方法上使用@Value进行赋值 2.仍然定义静态变量,同时定义一个普通

  • SpringBoot详解如果通过@Value注解给静态变量注入值

    目录 前序 方案一 方案二 方案三 使用场景 总结 最近做项目的时候,给static变量赋值, 使用 @value注解 ,结果 获取一直为null , 1.spring不允许/不支持把值注入到静态变量中 2.Spring的@Value依赖注入是依赖set方法 3.set方法是普通的对象方法 4.static变量是类的属性,static没有set方法 前序 SpringBoot中使用@Value()只能给普通变量注入值,不能直接给静态变量赋值 例如,application-dev.properti

  • spring框架下@value注解属性static无法获取值问题

    目录 @value注解属性static无法获取值 解决办法 @Value注解取不到值的几种情况 几种获取不到值的特殊情况如下 @value注解属性static无法获取值 @Value("${appId}") private static String appid; 这样是无法直接获得值的 解决办法 需要这样写 private static String appid; @Value("${appId}") public void setAppid(String app

  • 解决@Value注解不能注入static修饰的属性问题

    目录 @Value注解不能注入static属性 问题描述 剖析 @Value(“${属性}“)注入被static修饰的属性 问题描述 解决方案 @Value注解不能注入static属性 问题描述 在application.yml中: constant:   key: hello   value: world 工具类ConstantHelper: @Component public class ConstantHelper {     @Value("${constant.value}"

  • 解决@Autowired注入static接口的问题

    目录 @Autowired注入static接口问题 @Autowired自动注入普通service很方便 但是如果注入static修饰的service则注入不了 后来网上百度了一下,看到了一个方法 标签解释 导致@Autowired注入失败的问题 背景 原因 @Autowired注入static接口问题 @Autowired自动注入普通service很方便 如: @Component public class WarningMatterUtil { //报警表 @Autowired privat

  • 基于@PostConstruct注解的使用,解决向静态变量注入值

    目录 @PostConstruct注解的使用,向静态变量注入值 说说思路 @PostConstruct和静态变量注入和spring初始化 执行顺序 关于spring初始化操作 @PostConstruct注解的使用,向静态变量注入值 今天在编写工具类时遇到了一个问题,一般在定义工具类方时,我们会将工具类中的方法定义成static类型,使用时可以通过类名.方法名获取该方法,无需实例化出对象才能使用其内部方法,但是当有些参数在配置文件中定义时,我们需要拿到这些参数就需要在工具类方法使用@Value注

  • spring中使用@Autowired注解无法注入的情况及解决

    目录 spring @Autowired注解无法注入 问题简述 原因:(此处只说第二种) 解决方案 @Autowired注解注入失败,提示could not autowire spring @Autowired注解无法注入 问题简述 在使用spring框架的过程中,常会遇到这种两情况: 1.在扫描的包以外使用需要使用mapper 2.同目录下两个controller或者两个service,在使用@Autowired注解注入mapper或者service时,其中一个可以注入,另一个却为空. 原因:

  • 基于@RequestBody注解只能注入对象和map的解决

    目录 @RequestBody注解只能注入对象和map的问题 1.自定义一个适应于这种情况的注解@RequestJson 2.自定义RequestJsonHandlerMethodArgumentResolver 3.将上述配置应用到spring项目中 4.配置完成了,简单使用 @RequestBody注解的使用问题 先看一下@RequestBody的作用 个人总结: @RequestBody注解只能注入对象和map的问题 前后端分离开发模式下,前后端数据交互全部采用json,所以在后端在采用s

  • 解决SpringBoot2多线程无法注入的问题

    1.情况描述 使用springboot2多线程,线程类无法实现自动注入需要的bean,解决思路,通过工具类获取需要的bean 如下 package com.ps.uzkefu.apps.ctilink.handler; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.ps.uzkefu.apps.callcenter.entity.CallRecord; import com.ps.uzkefu.apps.call

  • Springboot 读取自定义pro文件注入static静态变量方式

    Springboot 读取pro文件注入static静态变量 mailConfig.properties #服务器 mail.host=smtp.qq.com #端口号 mail.port=587 #邮箱账号 mail.userName=hzy_daybreak_lc@foxmail.com #邮箱授权码 mail.passWord=vxbkycyjkceocbdc #时间延迟 mail.timeout=25000 #发送人 mail.emailForm=hzy_daybreak_lc@foxm

  • 解决Springboot @Autowired 无法注入问题

    特别提醒:一定要注意文件结构 WebappApplication 一定要在包的最外层,否则Spring无法对所有的类进行托管,会造成@Autowired 无法注入. 1. 添加工具类获取在 Spring 中托管的 Bean (1)工具类 package com.common; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionE

  • JAVA解决在@autowired,@Resource注入为null的情况

    使用SpringMVC或者SSH过程中,有时可能会遇到这么一个问题.就是在一个普通的JAVA类(不是controller也不是action类)中无法注入在spring配置文件中配置的bean. 比如你在一个普通java类想调用某个在spring中配置的service,你会发现不管你用@Resource还是@Autowired注解都无法注入,对象始终是null. 那是因为一般普通的Java类没有被spring代理,自然无法通过spring注入相关的对象.难道这样就不能调用了吗?这里提供下面一个类来

  • Spring 注入static属性值方式

    目录 Spring 注入static属性值 1. 问题 2. 解决方案 Spring依赖注入static静态变量相关问题 1.Spring不支持依赖注入static静态变量 2.Spring如何给静态变量注入值 3.Spring静态注入的三种方式 去网上搜了下解决办法,简单总结一下几种实现方式 Spring 注入static属性值 本文介绍Spring中如何从属性文件给static字段注入值.实际应用中一些工具类中static属性值需读取配置文件,实现该功能可以让工具类提供静态方法更易使用. 1

随机推荐