当前位置: 首页 > news >正文

@Value注解的底层原理(一)

一.概述

        @Value注解是如何工作的,大致分为以下几个步骤:

1.获取Value值

2.解析${}

3.解析#{SEL表达式}(对于这种情况本文没有演示)

4.类型转换

二.测试

package com.example.springdemo.demos.a14;import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.stereotype.Component;import java.lang.reflect.Field;/*** @author zhou* @version 1.0* @description TODO* @date 2025/8/31 20:58*/
@Configuration
public class TestValue {public static void main(String[] args) throws NoSuchFieldException {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestValue.class);ContextAnnotationAutowireCandidateResolver resolver = new ContextAnnotationAutowireCandidateResolver();resolver.setBeanFactory(context.getDefaultListableBeanFactory());Field home = Bean1.class.getDeclaredField("home");extracted(context, resolver, home);Field age = Bean1.class.getDeclaredField("age");extracted2(context,resolver,age);}private static void extracted(AnnotationConfigApplicationContext context, ContextAnnotationAutowireCandidateResolver resolver, Field field) {DependencyDescriptor descriptor = new DependencyDescriptor(field,true);//获取@Value的内容String value = resolver.getSuggestedValue(descriptor).toString();System.out.println(value);//解析${}value = context.getEnvironment().resolvePlaceholders(value);System.out.println(value);}private static void extracted2(AnnotationConfigApplicationContext context, ContextAnnotationAutowireCandidateResolver resolver, Field field) {DependencyDescriptor descriptor = new DependencyDescriptor(field,true);//获取@Value的内容String value = resolver.getSuggestedValue(descriptor).toString();System.out.println(value);//解析${}value = context.getEnvironment().resolvePlaceholders(value);System.out.println(value);//类型转换Object age = context.getBeanFactory().getTypeConverter().convertIfNecessary(value, descriptor.getDependencyType());System.out.println(age.getClass());}public class Bean1{@Value("${JAVA_HOME}")private String home;@Value("18")private int age;}}

文中抽取了两个方法对@Value注解作测试

1.解析带有${}的属性

@Value("${JAVA_HOME}")
private String home;

        测试方法一里面详细介绍了步骤,首先需要拿到DependencyDescriptor对象,然后通过ContextAnnotationAutowireCandidateResolver对象到@Value里面的值。原始的值是${JAVA_HOME},随后通过context.getEnvironment().resolvePlaceholders方法解析里面的值。

2.类型不一致转换

@Value("18")
private int age;

@Value里面的值类型是String与int类型不一致,前面获取值的步骤是一致的,不同的地方在于最后一步,利用下面的类型转换器把String类型转换为Integer类型,后面自动拆箱为int类型。

 //类型转换Object age = context.getBeanFactory().getTypeConverter().convertIfNecessary(value, descriptor.getDependencyType());System.out.println(age.getClass());

测试结果:

http://www.xdnf.cn/news/1407673.html

相关文章:

  • (一) aws上微服务
  • C++ 快速复习指南(上半部分)
  • 我开发了一个自动还原源码的小工具
  • AI辅助编程日记和chat历史开源Series 1:VSCode + GitHub Copilot 自动下载及安装软件
  • 《打破 “慢“ 的黑箱:前端请求全链路耗时统计方案》
  • Vue3 响应式基础
  • 前端学习——JavaScript基础
  • 创维LB2004_安装软件教程
  • 37. 解数独
  • GaRe:面向非约束户外照片集的可重光照 3D 高斯溅射技术简要解析
  • Android开发-活动页面
  • C# .Net8 WinFormsApp使用日志Serilog组件
  • c++ Effective c++ 条款5
  • 机器学习之线性回归
  • 数据结构02:排序算法
  • PyQt5 进度条详细示例与性能优化
  • 电商系统的分布式事务调优
  • Knit-易用的prompt管理和调试工具
  • 第六章:透明度-Transparency《Unity Shaders and Effets Cookbook》
  • io进程线程;标准IO;0831
  • 【嵌入式】【调用函数图】手动绘制函数调用状态机
  • 【优先算法--前缀和】
  • 3DES加解密的算法Java Python Golang
  • CVPR上的多模态检索+视频理解,LLM助力提效翻倍
  • 8.1【Q】VMware相关
  • 吴恩达机器学习作业十一:异常检测
  • 大模型——利用RAG构建智能问答平台实战
  • 在Ubuntu服务器上安装KingbaseES V009R002C012(Orable兼容版)数据库过程详细记录
  • Qwen3_moe模型代码解析
  • FreeRTOS实战:任务创建与调度详解