@Value注解不能注入static修饰的属性
发布日期:2021-06-30 12:36:38
浏览次数:2
分类:技术文章
本文共 3718 字,大约阅读时间需要 12 分钟。
1:问题描述
配置文件application.yml:
person.name: nobodyperson.age: 20
实体类Person:
package com.nobody.pojo.entity;import java.io.Serializable;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic class Person implements Serializable { /** * */ private static final long serialVersionUID = -8245392957106320519L; @Value("${person.name}") private static String name; private static String age; @Value("${person.age}") public void setAge(String age) { Person.age = age; } public void setName(String name) { Person.name = name; } public static String getName() { return name; } public static String getAge() { return age; }}
测试接口类DemoController:
package com.nobody.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.nobody.ehr.pojo.entity.Person;@RequestMapping("demo")@RestControllerpublic class DemoController { @GetMapping public String demo() { System.out.println("name:" + Person.getName() + ", age:" + Person.getAge()); return ""; }}
测试结果:
@Value注解放在属性上注入值失败,而@Value放在setter方法上(注意,该方法也不能是静态方法)注入成功。name:null, age:20
2:分析
启动日志:
[19:37:45] INFO [restartedMain] o.s.b.f.a.AutowiredAnnotationBeanPostProcessor - Autowired annotation is not supported on static fields: private static java.lang.String com.nobody.pojo.entity.Person.name
源码分析:
private InjectionMetadata buildAutowiringMetadata(final Class clazz) { Listelements = new ArrayList<>(); Class targetClass = clazz; do { final List currElements = new ArrayList<>(); ReflectionUtils.doWithLocalFields(targetClass, field -> { AnnotationAttributes ann = findAutowiredAnnotation(field); if (ann != null) { //这里对静态属性注入做了过滤 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))) { //这里对静态方法做了过滤 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); }
3:结论
从源码上发现,理论上spring是可以对静态域注入的,只是spring没有这样做,它认为依赖注入发生的时段是在实例的生命周期,而不是类的生命周期。
转载地址:https://javalib.blog.csdn.net/article/details/105229998 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月24日 09时35分07秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
为什么要反对比特币,这不代表是空气币
2019-05-01
5G技术如何运用在数字资产方面
2019-05-01
区块链技术运用最广泛的还是保险
2019-05-01
新冠疫情影响了经济,却带动了区块链
2019-05-01
我们提出了DeFi项目如何成为聚合器,有两种路径
2019-05-01
SnapEx的新感觉,对新手很友好
2019-05-01
区块链有哪些潜在风险?
2019-05-01
聚合器运用在什么地方最合适
2019-05-01
区块链可以解决供应链的问题,包括食品方面
2019-05-01
首个聚合器怎么产生的,并运用领域在什么
2019-05-01
区块链资金安全吗?该怎么保护
2019-05-01
区块链的优势和劣势的对比
2019-05-01
新技术,区块链能用在什么领域
2019-05-01
区块链技术应用,最先医疗行业
2019-05-01
新币上市旧币会降价吗
2019-05-01
为什么币圈的基础设施越来越多
2019-05-01
区块链投资要注意这几点
2019-05-01
市场套利策略是什么
2019-05-01
跨市场套利怎么处理
2019-05-01
当博士进入币圈会怎么样
2019-05-01