Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does springboot get the configuration through @ Value,@ConfigurationProperties

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to get the configuration of springboot through @ Value,@ConfigurationProperties". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how springboot gets the configuration through @ Value,@ConfigurationProperties".

Get the configuration item value through @ Value,@ConfigurationProperties to get the configuration spring boot

The using version is 1.5.4

For example, the configuration of a thread pool:

Add configuration items and values in application.yml

# Thread pool configuration taskexecutor: corePoolSize: 5 maxPoolSize: 10 queueCapacity: 25 get the value @ Configuration@EnableAsyncpublic 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;}} get the value @ Configuration @ EnableAsync @ ConfigurationProperties (ignoreUnknownFields = false,prefix = "taskexecutor") public class ExecutorConfig {private int corePoolSize; private int maxPoolSize; private int queueCapacity by @ ConfigurationProperties @ Bean public Executor getAsyncExecutor () {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor (); executor.setCorePoolSize (corePoolSize); executor.setMaxPoolSize (maxPoolSize); executor.setQueueCapacity (queueCapacity); executor.setThreadNamePrefix ("TaskExecutor-"); executor.initialize (); return executor;}}

Load the configuration file through @ ConfigurationProperties, associate the configuration item with bean and attributes, specify that ignoreUnknownFields will throw an exception when any attribute does not match the value, and specify the prefix of the configuration item with prefix.

@ ConfigurationProperties also supports value injection of hierarchy, Boolean, collection, and so on.

Let's talk about the difference between @ ConfigurationProperties and @ Value the attributes in the @ Configuration@Value bulk injection configuration file specify whether loose binding (loose syntax) supports SPEL syntax does not support JSR303 data check whether complex type encapsulation is supported

They can get the value of the configuration file yml or properties.

If you just need to get a value in a configuration file in some business logic, use @ Value

If we write a javaBean specifically to map to the configuration file, we will directly use @ ConfigurationProperties

Configuration file injection value data check @ Component@ConfigurationProperties (prefix = "person") @ Validatedpublic class Person {/ * * / / Value ("${person.last-name}") / / lastName must be in the mailbox format @ Email private String lastName; / / @ Value ("# {11q2}") private Integer age; / / @ Value ("true") private Boolean boss Private Date birth; private Map maps; private List list; private Dog dog; so far, I believe you have a better understanding of "how springboot gets the configuration through @ Value,@ConfigurationProperties". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report