广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot如何通过@Value,@ConfigurationProperties获取配置
  • 691
分享到

springboot如何通过@Value,@ConfigurationProperties获取配置

2024-04-02 19:04:59 691人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录通过@Value,@ConfigurationProperties获取配置Spring Boot 获取配置项值通过@Value 获取值通过@ConfigurationProper

通过@Value,@ConfigurationProperties获取配置

spring boot 获取配置项值

使用版本是1.5.4

举例一个线程池的配置:

在application.yml添加配置项及值

    # 线程池配置
    taskexecutor:
      corePoolSize: 5
      maxPoolSize: 10
      queueCapacity: 25

通过@Value 获取值

@Configuration
@EnableAsync
public class ExecutorConfig {
    @Value("${taskexecutor.corePoolSize}")
    private int corePoolSize;
    @Value("${taskexecutor.maxPoolSize}")
    private int maxPoolSize;
    @Value("${taskexecutor.queueCapacity}")
    private int queueCapacity;
    @Bean
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("TaskExecutor-");
        executor.initialize();
        return executor;
    }
    }

通过@ConfigurationProperties 获取值 

    @Configuration
    @EnableAsync
    @ConfigurationProperties(ignoreUnknownFields = false,prefix = "taskexecutor")
    public class ExecutorConfig {
    private int corePoolSize;
    private int maxPoolSize;
    private int queueCapacity;
    @Bean
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("TaskExecutor-");
        executor.initialize();
        return executor;
    }
    }

通过@ConfigurationProperties加载配置文件,将配置项与bean及属性关联,指定ignoreUnknownFields当有属性未匹配到值时会抛出异常,用prefix指定配置项的前缀。

@ConfigurationProperties还支持层级结构、 布尔、集合等类型的值注入

Http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-typesafe-configuration-properties

说下@ConfigurationProperties和@Value区别

 @Configuration@Value
功能批量注入配置文件中的属性一个个指定
松散绑定(松散语法)支持不支持
SPEL语法不支持支持
jsR303数据校验支持不支持
复杂类型封装支持不支持

配置文件yml还是properties他们都能获取到值;

如果说, 只是在某个业务逻辑中需要获取一项配置文件中的某项值, 使用@Value

如果说,专门编写了一个javaBean 来和配置文件进行映射,我们就直接使用@ConfigurationProperties;

配置文件注入值数据校验

@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
 
    
    //Value("${person.last-name}")
    //lastName必须为邮箱格式
    @Email
    private String lastName;
    //@Value("#{11*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> list;
    private Dog dog;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: springboot如何通过@Value,@ConfigurationProperties获取配置

本文链接: https://www.lsjlt.com/news/143077.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作