SpringBoot高级配置

举报
兮动人 发表于 2022/05/29 16:41:47 2022/05/29
【摘要】 1. ConfigurationProperties 2. @EnableConfigurationProperties 3. 宽松绑定 4. 常用计量单位 4.1 时间单位 4.2 空间单位 5. 数据校验 6. yml 字面值表达方式ConfigurationProperties可以为第三方bean绑定属性 1. ConfigurationProperties@Configuratio...

ConfigurationProperties可以为第三方bean绑定属性

1. ConfigurationProperties

  • @ConfigurationProperties 不仅能为自己开发的组件做bean的属性绑定

application.yml

servers:
  ipAddress: 192.168.1.1
  port: 1234
  timeout: -1
@Component
@Data
@ConfigurationProperties(prefix = "servers")
public class ServerConfig {
    private String ipAdDress;
    private int port;
    private long timeout;
}
public static void main(String[] args) {
    ConfigurableApplicationContext ctx = SpringApplication.run(Sringboot13ConfigurationApplication.class, args);
    ServerConfig bean = ctx.getBean(ServerConfig.class);
    System.out.println(bean);
}

在这里插入图片描述

  • 还可以为第三方的bean绑定属性
    在这里插入图片描述
@SpringBootApplication
public class Sringboot13ConfigurationApplication {

    @Bean
    @ConfigurationProperties(prefix = "datasource")
    public DruidDataSource dataSource() {
        DruidDataSource ds = new DruidDataSource();
        return ds;
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(Sringboot13ConfigurationApplication.class, args);
        ServerConfig bean = ctx.getBean(ServerConfig.class);
        System.out.println(bean);
        DruidDataSource ds = ctx.getBean(DruidDataSource.class);
        System.out.println(ds.getDriverClassName());
    }

}

在这里插入图片描述

2. @EnableConfigurationProperties

  • @EnableConfigurationProperties注解可以将使用@ConfigurationProperties注解对应的类加入Spring容器管控

  • @ConfigurationProperties 是做具体属性绑定的

  • @EnableConfigurationProperties 是开启属性绑定,并设定对应的目标是谁

在这里插入图片描述
在这里插入图片描述

  • @EnableConfigurationProperties与@Component不能同时使用

  • 解除使用@ConfigurationProperties注释警告
    在这里插入图片描述

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

3. 宽松绑定

  • @ConfigurationProperties绑定属性支持属性名宽松绑定
    在这里插入图片描述

  • 官方推荐使用中划线模式,一般常见采用驼峰模式

  • 宽松绑定不支持注解@Value引用单个属性的方式
    在这里插入图片描述

  • 绑定前缀名命名规范:仅能使用纯小写字母、数字、下划线作为合法的字符

4. 常用计量单位

  • SpringBoot支持JDK8提供的时间与空间计量单位
  • JDK8支持的时间与空间计量单位

4.1 时间单位

  • 默认使用毫秒单位
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

4.2 空间单位

  • 默认使用 B 为单位
    在这里插入图片描述
  • 设置 MB 单位
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

结果为:10485760B = 10MB

5. 数据校验

  • 开启数据校验有助于系统安全性,J2EE规范中JSR303规范定义了一组有关数据校验相关的API

  • 开启Bean数据校验

①: 添加JSR303规范坐标与Hibernate校验框架对应坐标

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
</dependency>

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
</dependency>

②:对Bean开启校验功能
在这里插入图片描述

③:设置校验规则
在这里插入图片描述
在这里插入图片描述

  • 运行后:
    在这里插入图片描述

  • 在 validation 中也可以看到属性
    在这里插入图片描述

6. yml 字面值表达方式

在这里插入图片描述

  • 注意yaml文件中对于数字的定义支持进制书写格式,如需使用字符串请使用引号明确标注
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。