栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot读取application.yml/application.properties中的自定义配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot读取application.yml/application.properties中的自定义配置

方式1:@ConfigurationProperties(prefix = “attachment.file”) + @EnableConfigurationProperties(AttachmentFileConfig.class)

① application.yml文件:

attachment:
  file:
    maxSize: 10
    types:
      jpeg: FFD8FF
      jpg: FFD8FF
      bmp: 424D
      png: 89504E47
      rtf: 7B5C727466
      txt: 75736167
      pdf: 255044462D312E
      doc: D0CF11E0
      docx: 504B030414000600080000002100

② 读取配置文件并封装成AttachmentFileConfig类:

@Data
@ConfigurationProperties(prefix = "attachment.file")
public class AttachmentFileConfig {
    private Double maxSize;
    private Map types;
}

③ 使用AttachmentFileConfig类:

@EnableConfigurationProperties(AttachmentFileConfig.class)
@Configuration
public class AttachmentConfig {
    @Bean
    public AttachmentTypes attachmentTypes(AttachmentFileConfig attachmentFileConfig) {
        return new AttachmentTypes(attachmentFileConfig);
    }
}

方式2:@ConfigurationProperties(prefix = “gulimall.thread”) + @Component

① application.properties文件:

gulimall.thread.core-size=20
gulimall.thread.max-size=200
gulimall.thread.keep-alive-time=10

② 读取配置文件并封装成ThreadPoolConfigProperties类:

@ConfigurationProperties(prefix = "gulimall.thread")
@Component
@Data
public class ThreadPoolConfigProperties {
    private Integer coreSize;
    private Integer maxSize;
    private Integer keepAliveTime;
}

③ 使用ThreadPoolConfigProperties类:

@Configuration
public class MyThreadConfig {
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties threadPoolConfigProperties){
        return new ThreadPoolExecutor(
                threadPoolConfigProperties.getCoreSize(),
                threadPoolConfigProperties.getMaxSize(),
                threadPoolConfigProperties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new linkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/434357.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号