4 ways to read configuration in Spring Boot, recommended to collect!
In Spring Boot projects, reading the contents of configuration files is basically involved. This article will talk about several common ways to read configuration files.
Value annotation
In application.properties
Configuration file configuration item:
name=tian
Read in java code:
/** * @author tianwc 公众号:java后端技术全栈、面试专栏 * @version 1.0.0 * @date 2023年07月02日 21:00 * 博客地址:<a href="http://woaijava.cc/">博客地址</a> */ @RestController @RequestMapping("/configuration") public class ConfigurationController { @Value("${name}") private String name; @GetMapping("/index") public String test() { return name; } }
Verify:
GET
http://localhost:8089/configuration/index
Return parameters:
tian
This type usually does not have a prefix, and single configuration items will be read in this way.
If there is the same prefix configuration, then we can use the following method.
ConfigurationProperties annotation
In application.properties
Configuration file configuration items:
user.userName=tian1 user.age=21
In javadiam Read in:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author tianwc 公众号:java后端技术全栈、面试专栏 * @version 1.0.0 * @date 2023年07月02日 21:07 * 博客地址:<a href="http://woaijava.cc/">博客地址</a> */ @Component @ConfigurationProperties(prefix = "user") public class PreConfiguration { private String userName; private Integer age; //set get 省略 }
Verification:
GET
http://localhost:8089/preconfiguration/index
us Usually, a type of configuration items is set as a prefix, processed uniformly, and object-oriented.
But, how to deal with multiple reads (array type)?
We can use the following method.
也是ConfigurationProperties注解
在application.properties
配置文件配置项:
vip.userList[0].name=tian01 vip.userList[0].age=20 vip.userList[1].name=tian02 vip.userList[1].age=21
定义一个User类:
public class User { private String name; private Integer age; //set get toString 省略 }
配置读取类:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; /** * @author tianwc 公众号:java后端技术全栈、面试专栏 * @version 1.0.0 * @date 2023年07月02日 21:24 * 博客地址:<a href="http://woaijava.cc/">博客地址</a> */ @Component @ConfigurationProperties(prefix = "vip") public class UserListConfiguration { private List<User> userList; // set get 省略 }
如果我们自定义一个custom.properties
配置文件,那又该如何读取呢?
PropertySource注解
在application.properties
配置文件配置项:
realName=tian3
代码实现
@RestController @RequestMapping("/propertySource") @PropertySource("classpath:custom.properties") public class PropertySourceController { @Value("${realName}") private String realName; @GetMapping("/index") public String test() { return realName; } }
验证:
GET
http://localhost:8089/propertySource/index
返回参数:tian3
那么,问题又来了,我们可能会因为一个配置项需要改,或者是上线后发现配置项不对,这时候就需要重启服务了,为了避免这样重启服务,我们可以引入分布式配置中心。
分布式配置中心有很多实现方案,比如Nacos、Zookeeper、Qconf、disconf、Apache Commons Configuration、Spring Cloud Config等。
The above is the detailed content of 4 ways to read configuration in Spring Boot, recommended to collect!. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool