Skip to content

Latest commit

 

History

History
214 lines (163 loc) · 5.51 KB

2_配置文件.md

File metadata and controls

214 lines (163 loc) · 5.51 KB

配置文件

YAML 文件

我们可以通过 application.properties / application.yml 对 Spring Boot 程序进行简单的配置。

YAML 是 "YAML Ain't a Markup Language"(YAML 不是一种标记语言)的递归缩写,是一种人类可读的数据序列化语言。相比于 properties 文件,YAML 配置的方式更加直观清晰,简介明了,有层次感。

properties 配置方式:

server.port=8330
spring.dubbo.applicaiton.name=dubbo-consumer
spring.dubbo.applicaiton.registry=zookeeper:127.0.0.1:2181

yaml 配置方式:

server:
 prot:8330
spring:
 dubbo:
   application:
      name:dubbo-consumer
      registry:zookeeper:127.0.0.1:2181

但是 YAML 配置的方式有一个缺点,那就是不支持 @PropertySource 注解导入自定义的 YAML 配置。

读取配置文件

@Value 方式

@Value("${xxx}")
String xxx;

根据传入的 xxx 获取相应的值,@Value 方式一般同于读取比较简单的配置信息,但是并不建议使用。

@ConfigurationProperties 方式

定义一个 LibraryProperties 类:

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

// 使用 @Component 注解声明,可以像使用普通 Bean 一样将其注入到类中使用。
@Component
@ConfigurationProperties(prefix = "library")
@Setter
@Getter
@ToString
class LibraryProperties {
    private String location;
    private List<Book> books;

    @Setter
    @Getter
    @ToString
    static class Book {
        String name;
        String description;
    }
}
package cn.javaguide.readconfigproperties;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ReadConfigPropertiesApplication implements InitializingBean {
    
    // 注入使用
    private final LibraryProperties library;
    
    public ReadConfigPropertiesApplication(LibraryProperties library) {
        this.library = library;
    }

    public static void main(String[] args) {
        SpringApplication.run(ReadConfigPropertiesApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println(library.getLocation());
        System.out.println(library.getBooks());
    }
}

@ConfigurationProperties 方式

@ConfigurationProperties 方式不仅可以读取数据,还可以对读取的数据进行校验。

user:
  name: xxx
  email: 123455@
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;

// 注意:这里 ProfileProperties 类没有加 @Component 注解。
@Getter
@Setter
@ToString
@ConfigurationProperties("user")
@Validated
public class ProfileProperties {
   @NotEmpty
   private String name;

   @Email
   @NotEmpty
   private String email;

   // 配置文件中没有读取到的话就用默认值
   private Boolean handsome = Boolean.TRUE;
}
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
// 使用 @EnableConfigurationProperties 注册我们的配置 Bean。
@EnableConfigurationProperties(ProfileProperties.class)
public class ReadConfigPropertiesApplication implements InitializingBean {
    private final ProfileProperties profileProperties;

    public ReadConfigPropertiesApplication(ProfileProperties profileProperties) {
        this.profileProperties = profileProperties;
    }

    public static void main(String[] args) {
        SpringApplication.run(ReadConfigPropertiesApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println(profileProperties.toString());
    }
}

邮箱格式不正确,所以程序运行的时候就报错,也就保证了数据类型的安全性。

@PropertySource 方式

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
// @PropertySource 注解方式读取指定的 .properties 文件
@PropertySource("classpath:website.properties")
@Getter
@Setter
class WebSite {
    @Value("${url}")
    private String url;
}

使用:

@Autowired
private WebSite webSite;

System.out.println(webSite.getUrl());

Spring Boot 加载配置文件的优先级

Spring 读取配置文件也是有优先级的,其中:

  • 项目目录下的 config 目录下的 applicaiton.yml 的优先级最高;
  • resources 目录下的 config 目录下的 application.yml 的优先级次之;
  • resources 目录下的 application.yml 优先级最低。