Maison  >  Article  >  Java  >  4 façons de lire la configuration dans Spring Boot, recommandées à collecter !

4 façons de lire la configuration dans Spring Boot, recommandées à collecter !

Java后端技术全栈
Java后端技术全栈avant
2023-08-15 16:06:231223parcourir

Dans les projets Spring Boot, cela implique essentiellement de lire le contenu des fichiers de configuration. Cet article parlera de plusieurs façons courantes de lire les fichiers de configuration.

Annotation de valeur

dans application.propertiesÉléments de configuration du fichier de configuration : application.properties配置文件配置项:

name=tian

在java代码中读取:

/**
 * @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;
    }
}

验证:

GET

http://localhost:8089/configuration/index

返回参数:

tian

这类通常都是没有前缀,比较单一的配置项会采用这么读取。

如果有同一的前缀配置,那么我们可以使用下面这种方法。

ConfigurationProperties注解

application.properties

user.userName=tian1
user.age=21

Lire en code java :

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 省略
}

Vérification :

GET

http://localhost:8089/configuration/index

Paramètres de retour :

vip.userList[0].name=tian01
vip.userList[0].age=20
vip.userList[1].name=tian02
vip.userList[1].age=21

Ce type n'a généralement pas de préfixe et les éléments de configuration uniques seront lus comme ceci.

S'il existe la même configuration de préfixe, nous pouvons utiliser la méthode suivante.

🎜🎜Annotation ConfigurationProperties 🎜🎜🎜🎜 dans 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等。

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer