>  기사  >  Java  >  Spring Boot에서 구성을 읽는 4가지 방법, 수집을 권장합니다!

Spring Boot에서 구성을 읽는 4가지 방법, 수집을 권장합니다!

Java后端技术全栈
Java后端技术全栈앞으로
2023-08-15 16:06:231223검색

Spring Boot 프로젝트에서는 기본적으로 구성 파일의 내용을 읽는 작업이 포함됩니다. 이 기사에서는 구성 파일을 읽는 몇 가지 일반적인 방법에 대해 설명합니다.

: rgba(27, 31, 35, 0.05); 글꼴 계열: "Operator Mono", Consolas, Monaco, Menlo, monospace; 단어 나누기: break-all;color: rgb(239, 112, 96);"> application.properties구성 파일 구성 항목: 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

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

확인:

GET

http://localhost:8089/configuration/index

반환 매개변수:

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

이 유형에는 일반적으로 접두사가 없으며 단일 구성 항목은 다음과 같이 읽혀집니다.

동일한 접두사 구성이 있는 경우 다음 방법을 사용할 수 있습니다.

🎜🎜ConfigurationProperties 주석 🎜🎜🎜🎜 응용 프로그램. 속성구성 파일 구성 항목: 🎜
public class User {
    private String name;
    private Integer age;
    
    //set get toString 省略
}
🎜Read in javadiam: 🎜
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 省略
}
🎜확인: 🎜🎜🎜GET🎜🎜http://localhost:8089/preconfiguration/index🎜🎜🎜우리는 일반적으로 카테고리를 넣습니다. 구성 항목 접두사로 설정되어 균일하게 처리되며 객체 지향적입니다. 🎜🎜그런데 다중 읽기(배열 유형)는 어떻게 처리하나요? 🎜🎜다음 방법을 사용할 수 있습니다. 🎜

也是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等。

위 내용은 Spring Boot에서 구성을 읽는 4가지 방법, 수집을 권장합니다!의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 Java后端技术全栈에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제