SpringBoot中使用@ConfigurationProperties

简介: SpringBoot中使用@ConfigurationProperties

一、使用场景介绍


我们需要获取配置文件一组数据时,我们可以使用@value来进行一一获取,但是比较麻烦,此时我们就可以使用@ConfigurationProperties来进行获取。


二、使用步骤


1.先编写对应接收类,代码如下

package com.ruoyi.common.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
 * 读取项目相关配置
 * 
 * @author ruoyi
 */
@Component
@ConfigurationProperties(prefix = "ruoyi")/* application里面的配置前缀*/
public class RuoYiConfig
{
    /** 项目名称 */
    private String name;
    /** 版本 */
    private String version;
    /** 版权年份 */
    private String copyrightYear;
    /** 实例演示开关 */
    private boolean demoEnabled;
    /** 上传路径 */
    @Value("${app.upload.location}")
    private static String profile;
    /** 获取地址开关 */
    private static boolean addressEnabled;
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getVersion()
    {
        return version;
    }
    public void setVersion(String version)
    {
        this.version = version;
    }
    public String getCopyrightYear()
    {
        return copyrightYear;
    }
    public void setCopyrightYear(String copyrightYear)
    {
        this.copyrightYear = copyrightYear;
    }
    public boolean isDemoEnabled()
    {
        return demoEnabled;
    }
    public void setDemoEnabled(boolean demoEnabled)
    {
        this.demoEnabled = demoEnabled;
    }
    public static String getProfile()
    {
        return profile;
    }
    public void setProfile(String profile)
    {
        RuoYiConfig.profile = profile;
    }
    public static boolean isAddressEnabled()
    {
        return addressEnabled;
    }
    public void setAddressEnabled(boolean addressEnabled)
    {
        RuoYiConfig.addressEnabled = addressEnabled;
    }
    /**
     * 获取头像上传路径
     */
    public static String getAvatarPath()
    {
        return getProfile() + "/avatar";
    }
    /**
     * 获取下载路径
     */
    public static String getDownloadPath()
    {
        return getProfile() + "/download/";
    }
    /**
     * 获取上传路径
     */
    public static String getUploadPath()
    {
        return getProfile() + "/upload";
    }
}


2.配置文件对应配置

# 项目相关配置
#对应接收类的前缀
ruoyi:
  # 名称
  name: RuoYi
  # 版本
  version: 3.5.0
  # 版权年份
  copyrightYear: 2021
  # 实例演示开关
  demoEnabled: true
  # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
  profile: D:/ruoyi/uploadPath
  # 获取ip地址开关
  addressEnabled: false
  # 验证码类型 math 数组计算 char 字符验证
  captchaType: math



相关文章
|
XML Java 数据格式
SpringBoot入门(五) 之 @ConfigurationProperties和@Value的区分与用法
SpringBoot入门(五) 之 @ConfigurationProperties和@Value的区分与用法
117 0
|
3月前
|
存储 Java 数据库连接
解锁Spring Boot的强大配置功能:@ConfigurationProperties与@PropertySources详解
解锁Spring Boot的强大配置功能:@ConfigurationProperties与@PropertySources详解
1048 0
|
前端开发 Java Nacos
《SpringBoot系列三》:自定义配置时@Value和@ConfigurationProperties孰优孰劣?
《SpringBoot系列三》:自定义配置时@Value和@ConfigurationProperties孰优孰劣?
638 1
《SpringBoot系列三》:自定义配置时@Value和@ConfigurationProperties孰优孰劣?
|
Java 开发者 Spring
《SpringBoot篇》07.@ConfigurationProperties注解实现第三方bean加载属性
《SpringBoot篇》07.@ConfigurationProperties注解实现第三方bean加载属性
219 0
《SpringBoot篇》07.@ConfigurationProperties注解实现第三方bean加载属性
|
JSON 前端开发 Java
《SpringBoot系列一》:yaml配置文件各种数据类型使用姿势(含@EnableConfigurationProperties、@ConfigurationProperties)
《SpringBoot系列一》:yaml配置文件各种数据类型使用姿势(含@EnableConfigurationProperties、@ConfigurationProperties)
1132 0
《SpringBoot系列一》:yaml配置文件各种数据类型使用姿势(含@EnableConfigurationProperties、@ConfigurationProperties)
|
Java 数据库连接 数据库
【SpringBoot2 从0开始】底层注解 - @ConfigurationProperties 配置绑定
【SpringBoot2 从0开始】底层注解 - @ConfigurationProperties 配置绑定
【SpringBoot2 从0开始】底层注解 - @ConfigurationProperties 配置绑定
|
Java Spring 容器
第九篇:SpringBoot 配置高级 ConfigurationProperties注解 宽松绑定/松散绑定
第九篇:SpringBoot 配置高级 ConfigurationProperties注解 宽松绑定/松散绑定
262 0
第九篇:SpringBoot 配置高级 ConfigurationProperties注解 宽松绑定/松散绑定
|
druid Java 数据库连接
Spring Boot常用注解@ConfigurationProperties、松散绑定、数据校验
Spring Boot常用注解@ConfigurationProperties、松散绑定、数据校验
896 0
Spring Boot常用注解@ConfigurationProperties、松散绑定、数据校验
|
Java
SpringBoot - @ConfigurationProperties 注解使用姿势,这一篇就够了(五)
SpringBoot - @ConfigurationProperties 注解使用姿势,这一篇就够了(五)
89 0
SpringBoot - @ConfigurationProperties 注解使用姿势,这一篇就够了(五)
|
Java
SpringBoot - @ConfigurationProperties 注解使用姿势,这一篇就够了(四)
SpringBoot - @ConfigurationProperties 注解使用姿势,这一篇就够了(四)
88 0
SpringBoot - @ConfigurationProperties 注解使用姿势,这一篇就够了(四)