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

SpringBoot读取application.ymlapplication.properties中的自定义配置,第1张

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());
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5118961.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存