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

Spring Boot 框架注解:@ConfigurationProperties

@ConfigurationProperties(prefix = "sky.jwt") 是 Spring Boot 框架里的一个注解,其主要功能是把配置文件(像 application.properties 或者 application.yml)里的属性值绑定到一个 Java 类的字段上。下面详细阐述其作用:
表示当前的类为配置属性类,封装配置文件里面的一些配置项

1. 绑定配置属性

在 Spring Boot 应用里,配置文件(如 application.propertiesapplication.yml)用于存储应用的各种配置信息。借助 @ConfigurationProperties 注解,能够把配置文件中以特定前缀开头的属性值自动绑定到 Java 类的对应字段上。

在你给出的代码中,@ConfigurationProperties(prefix = "sky.jwt") 表示会把配置文件里以 sky.jwt 开头的属性值绑定到 JwtProperties 类的字段上。例如,若配置文件如下:

application.properties
sky.jwt.adminSecretKey=admin_secret_key_123
sky.jwt.adminTtl=3600
sky.jwt.adminTokenName=admin_token
sky.jwt.userSecretKey=user_secret_key_456
sky.jwt.userTtl=7200
sky.jwt.userTokenName=user_token
application.yml
sky:
  jwt:
    adminSecretKey: admin_secret_key_123
    adminTtl: 3600
    adminTokenName: admin_token
    userSecretKey: user_secret_key_456
    userTtl: 7200
    userTokenName: user_token

Spring Boot 会自动把这些属性值绑定到 JwtProperties 类的对应字段上:

@Component
@ConfigurationProperties(prefix = "sky.jwt")
@Data
public class JwtProperties {
    private String adminSecretKey;
    private long adminTtl;
    private String adminTokenName;
    private String userSecretKey;
    private long userTtl;
    private String userTokenName;
}

2. 类型安全

使用 @ConfigurationProperties 注解可以保证类型安全。Spring Boot 会自动依据 Java 类字段的类型进行属性值的转换。例如,adminTtluserTtllong 类型,Spring Boot 会把配置文件中的字符串值自动转换为 long 类型。

3. 集中管理配置

借助把配置属性绑定到一个 Java 类,能够对配置信息进行集中管理,增强代码的可读性和可维护性。在需要使用这些配置属性时,只需注入 JwtProperties 类的实例即可。

4. 示例代码使用

在其他组件中可以通过依赖注入的方式使用 JwtProperties 类的实例:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JwtService {

    private final JwtProperties jwtProperties;

    @Autowired
    public JwtService(JwtProperties jwtProperties) {
        this.jwtProperties = jwtProperties;
    }

    public void printJwtProperties() {
        System.out.println("Admin Secret Key: " + jwtProperties.getAdminSecretKey());
        System.out.println("Admin TTL: " + jwtProperties.getAdminTtl());
        System.out.println("Admin Token Name: " + jwtProperties.getAdminTokenName());
        System.out.println("User Secret Key: " + jwtProperties.getUserSecretKey());
        System.out.println("User TTL: " + jwtProperties.getUserTtl());
        System.out.println("User Token Name: " + jwtProperties.getUserTokenName());
    }
}

在这个示例中,JwtService 类通过构造函数注入了 JwtProperties 类的实例,进而可以使用配置文件中的属性值。

综上所述,@ConfigurationProperties(prefix = "sky.jwt") 的作用是把配置文件中以 sky.jwt 开头的属性值绑定到 JwtProperties 类的对应字段上,实现配置属性的集中管理和类型安全的绑定。

http://www.dtcms.com/a/117740.html

相关文章:

  • Java文件流操作 - 【Guava】IO工具
  • React 列表与 Keys 的深入探讨
  • 聊聊Spring AI的PgVectorStore
  • OpenCV 图形API(17)计算输入矩阵 src 中每个元素的平方根函数sqrt()
  • oklink js逆向(入口定位)
  • 1.2 测试设计阶段:打造高质量的测试用例
  • c++ 函数后面加const 作用
  • kaggle竞赛——房价预测
  • 轨迹预测Physical Plausibility-aware Trajectory Prediction via Locomotion Embodiment
  • 基于 Vue 3 + html2canvas 实现网页任意区域截图组件
  • 抓wifi无线空口包之Ubuntu抓包(二)
  • Linux-CentOS-7—— 安装MySQL 8
  • Kafka 中的幂等机制
  • SQLI打靶
  • 【嵌入式学习6】多任务版TCP服务器
  • 玄机-第六章-哥斯拉4.0流量分析的测试报告
  • 盛水最多的容器
  • Kafka负载均衡挑战解决
  • Jupyter Notebook不能自动打开默认浏览器怎么办?
  • IDEA快速入门
  • Airflow集成Lark机器人
  • 深入理解PCA降维:原理、实现与应用
  • 【Introduction to Reinforcement Learning】翻译解读2
  • Spring Boot 3.x 集成 MongoDB 的 默认配置项及默认值,以及 常用需要修改的配置项 的详细说明
  • nacos集群启动问题
  • CAS号:288574-78-7,Zinpyr-1可用作PET传感器
  • 【数据分享】2014-2025年全国监测站点的逐时空气质量数据(15个指标\Excel\Shp格式)
  • (PROFINET 转 EtherCAT)EtherCAT/Ethernet/IP/Profinet/ModbusTCP协议互转工业串口网关
  • Linux终止进程(kill process)的一些玩法
  • Jetpack Compose 基础组件学习2.0