Home > Java > javaTutorial > 4 ways to read configuration in Spring Boot, recommended to collect!

4 ways to read configuration in Spring Boot, recommended to collect!

Release: 2023-08-15 16:06:23
forward
1304 people have browsed it

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.propertiesConfiguration file configuration item:

name=tian
Copy after login

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;
    }
}
Copy after login

Verify:

GET

http://localhost:8089/configuration/index

Return parameters:

tian
Copy after login

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.propertiesConfiguration file configuration items:

user.userName=tian1
user.age=21
Copy after login

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 省略
}
Copy after login

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
Copy after login

定义一个User类:

public class User {
    private String name;
    private Integer age;
    
    //set get toString 省略
}
Copy after login

配置读取类:

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 省略
}
Copy after login

如果我们自定义一个custom.properties配置文件,那又该如何读取呢?

PropertySource注解

application.properties配置文件配置项:

realName=tian3
Copy after login

代码实现

@RestController
@RequestMapping("/propertySource")
@PropertySource("classpath:custom.properties")
public class PropertySourceController {

    @Value("${realName}")
    private String realName;
    @GetMapping("/index")
    public String test() {
        return realName;
    }
}
Copy after login

验证:

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!

Related labels:
source:Java后端技术全栈
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template