解决 Spring 配置文件警告

Spring 项目默认是使用.properties文件作为配置文件的,但是我们习惯上会更多的使用.yml文件,因为其可以体现出配置项的树形结构,可读性较强。

配置文件乱象

一般在配置数据源、端口号、服务名称这些配置时并不会出现黄色警告,但是一些自定义的配置项则会出现一片黄色的骇人景象

正常的项目配置

自定义的配置项

虽然有警告,但是这并不是致命错误,因为你仍然可以正常的使用 @Value("${test.a}") 来取值,但是作为有代码洁癖的人,这不能忍!而且 @Value 这种方式很容易因大意导致名称不一致,进而引起无法取值。

主角登场

我们先去没有警告的配置项 spring.datasource.url 代码内部,看看是怎么写的,能不能找点蛛丝马迹。

@ConfigurationProperties(
    prefix = "spring.datasource"
)
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {
    private ClassLoader classLoader;
    private boolean generateUniqueName = true;
    private String name;
    private Class<? extends DataSource> type;
    private String driverClassName;
    private String url;
    private String username;
    private String password;
    private String jndiName;
    //省略后部代码........
}

英语稍懂一些就会立刻注意到 @ConfigurationProperties 这个注解,因为它与我们此次要解决的问题非常相近,翻译过来就是 配置属性

基本使用

没错,@ConfigurationProperties 注解就是解决配置文件警告的关键,那么他是如何使用的?

关系映射实体类

像使用 MyBatisPlus 那样,我们需要先创建一个 关系映射实体类,目的是告诉 Spring,服务启动时你把哪些配置项的值给我塞到这个实体类中。

package com.zyan.test.pojo.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "test")
public class TestProperties {

    private String aProperty;

    private String b;

    private Boolean enable = Boolean.FALSE;
}

需要注意这个实体类必须包含 SetterGetter 方法,否则你将取不到值和无法取值。并且这个类必须使用 @Component 注解交由 Spring 管理,或者在服务启动类上使用 @ConfigurationPropertiesScan 注解进行扫描。

@ConfigurationProperties 注解的 prefix 用于指定配置项前缀,可以使用 . 进行多级指定。不能使用驼峰命名,只能使用 - 进行分词。

可以直接在此类中为属性赋上默认值。

取值

@Service
@RequiredArgsConstructor
public class TestService {

    private final TestProperties testProperties;

    @Override
    public void test() {
         System.out.println("配置项 test.enable 取值:" + testProperties.getEnable());
    }
    
}

TestProperties 注入到适当的位置,通过 Getter 方法进行取值。此处使用 @RequiredArgsConstructor 进行构造器注入,想要详细了解使用方法,可阅读 如何优雅的进行 Bean 注入

配置

test:
  a-property: xxx
  b: xxx
  enable: false

按照习惯,建议在 .yml 文件中使用 - 进行分词,在实体类中使用驼峰命名,不要担心这样无法取值,Spring 会帮你进行转化,当然,你也可以选择不这么做。

进阶用法

多级配置

多级配置只需要引入静态内部类即可

映射类写法

@Data
@Component
@ConfigurationProperties(prefix = "test")
public class TestProperties {

    private Other other;

    @Data
    public static class Other {
        private String a;
        private String b;
    } 
}

yml 文件写法

test:
  other:
    a: xxx
    b: xxx

列表配置

映射类写法

@Data
@Component
@ConfigurationProperties(prefix = "test")
public class TestProperties {

    private List<Other> otherList;

    @Data
    public static class Other {
        private String a;
        private String b;
    } 
}

yml 文件写法

test:
  other-list:
    -
      a: xxx
      b: xxx
    -
      a: xxx
      b: xxx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值