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
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
yaml 复制代码
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 类的对应字段上:

java 复制代码
@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 类的实例:

java 复制代码
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 类的对应字段上,实现配置属性的集中管理和类型安全的绑定。

相关推荐
一只叫煤球的猫11 分钟前
普通程序员,从开发到管理岗,为什么我越升职越痛苦?
前端·后端·全栈
一只鹿鹿鹿16 分钟前
信息化项目验收,软件工程评审和检查表单
大数据·人工智能·后端·智慧城市·软件工程
专注VB编程开发20年1 小时前
开机自动后台运行,在Windows服务中托管ASP.NET Core
windows·后端·asp.net
程序员岳焱1 小时前
Java 与 MySQL 性能优化:MySQL全文检索查询优化实践
后端·mysql·性能优化
一只叫煤球的猫1 小时前
手撕@Transactional!别再问事务为什么失效了!Spring-tx源码全面解析!
后端·spring·面试
望获linux1 小时前
【实时Linux实战系列】CPU 隔离与屏蔽技术
java·linux·运维·服务器·操作系统·开源软件·嵌入式软件
JosieBook1 小时前
【Java编程动手学】使用IDEA创建第一个HelloJava程序
java·开发语言·intellij-idea
Thomas_YXQ1 小时前
Unity3D DOTS场景流式加载技术
java·开发语言·unity
喜欢敲代码的程序员2 小时前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis
summer夏1232 小时前
2025.07 做什么
java·android studio