springboot3自定义配置和多配置文件

311 阅读1分钟

自定义配置

@Value配置

@Value("${配置值:默认值}")

对于数组、列表类型数据,写为一行,用逗号分隔元素,默认值是可选的。

application.yml

wnan:
  name: wnan.user
  age: 18
  item-list: book,cup,mouse,keyboard
  item-array: book,cup,mouse,keyboard
#  height:
#  wnan.item-list-2: 

自定义配置类

package wnan.explore.config;

import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ToString
public class CustomConfig {
    @Value("${wnan.name}")
    private String name;
    @Value("${wnan.age}")
    private int age;
    // 默认配置
    @Value("${wnan.height:180}")
    private int height;
    @Value("${wnan.item-list}")
    private List<String> item_list;
    @Value("${wnan.item-array}")
    private String[] item_array;
    @Value("${wnan.item-list-2:list21,list22,list23}")
    private List<String> item_list_2;

    public List<String> showItem(){
        return item_list;
    }
}
@Autowired
private CustomConfig customConfig;
@Test
void t1(){
    System.out.println(customConfig.toString());
    System.out.println(customConfig.showItem().get(0));
}

运行结果:

CustomConfig(name=wnan.user, age=18, height=180, item_list=[book, cup, mouse, keyboard], item_array=[book, cup, mouse, keyboard], item_list_2=[list21, list22, list23])
book

@ConfigurationProperties()注解

  1. 创建配置类:使用@ConfigurationProperties注解来创建一个配置类,将配置文件中的配置项映射到这个类的字段上。

  2. 启用配置属性:在Spring Boot的主类(main方法在的类)或者任何配置类上使用@EnableConfigurationProperties注解来启用配置属性。

  3. 必须要有get、set方法

@ConfigurationProperties注解用于将配置文件(如application.properties或application.yml)中的属性绑定到Java Bean上。该注解可以接收以下参数:

  1. prefix:这是最常用的参数,用于指定配置文件中属性的前缀,这样注解就会将具有该前缀的属性映射到配置类的字段上。
  2. ignoreInvalidFields:默认值为false。如果设置为true,则在绑定过程中,如果遇到无法绑定的字段(例如,因为类型不匹配),则忽略这些字段而不是抛出异常。
  3. ignoreUnknownFields:默认值为false。如果设置为true,则在绑定过程中,如果配置文件中存在配置类中没有对应字段的属性,则忽略这些未知字段而不是抛出异常。
  4. value:这是prefix参数的别名,因此可以用来代替prefix

@EnableConfigurationProperties注解用于启用对@ConfigurationProperties注解的支持,并将指定的配置属性类注册为Spring容器中的Bean。这个注解只有一个参数:

  1. value:这个参数是必须的,它接受一个或多个配置属性类的Class对象,用来指定哪些配置属性类应该被注册为Bean。这个参数可以有很多个,{xxx,xxx,xxx,xxx}
package wnan.explore.config;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import java.util.List;

@Configuration
@ConfigurationProperties(prefix = "wnan")
@ToString
@Getter
@Setter
public class CustomConfig2 {
    private String name;
    private int age;
    // 设置默认值
    private int height = 18;
    private List<String> item_list;
    private String[] itemArray;
}
// 在主类上面添加,也可以写在CustomConfig2类上面
@EnableConfigurationProperties({CustomConfig2.class})
@Autowired
private CustomConfig2 customConfig2;
@Test
void t2(){
    System.out.println(customConfig2.toString());
}

运行结果:

CustomConfig2(name=wnan.user, age=18, height=18, item_list=[book, cup, mouse, keyboard], item_array=[book, cup, mouse, keyboard])

多配置文件

  1. 配置文件为properties文件

@PropertySource("classpath:xxxx.properties") 这个注解指定了文件的位置,好像只能是properties类型的文件

dev-2.properties配置文件

dev.item-array=1,2,3,4,5
package wnan.explore.config;

import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:dev-2.properties") 
@ToString
public class CustomConfigOther {
    @Value("${dev.item-array}")
    private int[] itemArray;
}

@Autowired
private CustomConfigOther customConfigOther;
@Test
void t3(){
    System.out.println(customConfigOther.toString());
}

运行结果:

CustomConfigOther(itemArray=[1, 2, 3, 4, 5])
  1. 配置文件为yml文件

springboot里,自定义的yml文件,要自己去读取,没有直接的方法提供

dev.yml配置文件

dev:
  item-array: 1,2,3,4,5,45689

读取yml文件

package wnan.explore.config;

import lombok.ToString;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;

import java.util.Properties;

@Configuration
@ToString
public class CustomConfigOtherYml {

    private String itemArray;

    public Properties loadYamlAsProperties() {
        String path = "config/dev.yml";
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(new ClassPathResource(path));
        return factory.getObject();
    }

    public CustomConfigOtherYml(){
        Properties properties = loadYamlAsProperties();
        itemArray = String.valueOf(properties.get("dev.item-array"));
    }
}
@Autowired
private CustomConfigOtherYml customConfigOtherYml;
@Test
void t4(){
    System.out.println(customConfigOtherYml.toString());
}

运行结果:

CustomConfigOtherYml(itemArray=1,2,3,4,5,45689)

多环境配置文件

application.yml

# 多环境配置
spring:
  profiles:
    active: dev,test
    
server:
  port: 9099
  servlet:
    context-path: /api

application-dev.yml

server:
  port: 9092

application-test.yml

server:
  servlet:
    context-path: /api_test

image.png

没有多余的配置文件时: image.png

启动结果: image.png

源码--->>>springboot-explore