温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot怎么通过@Value,@ConfigurationProperties获取配置

发布时间:2022-03-19 08:56:26 来源:亿速云 阅读:187 作者:iii 栏目:开发技术

本篇内容主要讲解“springboot怎么通过@Value,@ConfigurationProperties获取配置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot怎么通过@Value,@ConfigurationProperties获取配置”吧!

通过@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还支持层级结构、 布尔、集合等类型的值注入

说下@ConfigurationProperties和@Value区别

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

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

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

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

配置文件注入值数据校验

@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
 
    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面值/${key} 从环境变量,配置文件中获取值/#{Spel}"></property>
     * </bean>
     */
    //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;

到此,相信大家对“springboot怎么通过@Value,@ConfigurationProperties获取配置”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI