Springboot入门之配置文件

1.全局固定配置文件

1.1)application.properties

1.2)application.yml

1.3)bootstrap vs application [.yml/properties]

boostrap 被父ApplicationContext加载,加载顺序优先与application[使用ApplicationContext加载],boostrap中配置的信息无法被修改覆盖【常用于Springcloud配置中心,加解密秘钥等配置】,application中的配置信息可以被修改覆盖【用于项目的自动化配置】。

 

2.yml使用

2.1)配置

data:
  aInteger: 1
  aLong: 10000
  aFloat: 3.14
  aDouble: 3.14159
  aBoolean: true
  aString: adcd
  aArray: 1,2,3
  aList: 1,2,3
  aSet: 1,2,3
  aMap: {name: "zha", age: 18}

2.2)读取

2.2.1)@Value读取

@Value("${data.aInteger}")
private Integer intValue;

@Value("${data.aLong}")
private Long longValue;

@Value("${data.aFloat}")
private Float floatValue;

@Value("${data.aDouble}")
private Double doubleValue;

@Value("${data.aBoolean}")
private Boolean booleanValue;

@Value("${data.aString}")
private String stringValue;

@Value("#{'$value.aArray}'.split(',')}")
private String[] values;

@Value("#{'${data.aList}'.split(',')}")
private List valueList;

@Value("#{'${data.aSet}'.split(',')}")
private Set valueSet;

@Value("#{${data.aMap}}")
private Map valueMap;

2.2.2)@ConfigurationProperties配置

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
@ConfigurationProperties(prefix = "data")
public class DataConfig {

    private Integer aInteger;
    private Long aLong;
    private Float aFloat;
    private Double aDouble;
    private Boolean aBoolean;
    private String aString;
    private Integer[] aArray;
    private List aList;
    private Set aSet;
    private Map aMap;

    public Integer getaInteger() {
        return aInteger;
    }

    public void setaInteger(Integer aInteger) {
        this.aInteger = aInteger;
    }

    public Long getaLong() {
        return aLong;
    }

    public void setaLong(Long aLong) {
        this.aLong = aLong;
    }

    public Float getaFloat() {
        return aFloat;
    }

    public void setaFloat(Float aFloat) {
        this.aFloat = aFloat;
    }

    public Double getaDouble() {
        return aDouble;
    }

    public void setaDouble(Double aDouble) {
        this.aDouble = aDouble;
    }

    public Boolean getaBoolean() {
        return aBoolean;
    }

    public void setaBoolean(Boolean aBoolean) {
        this.aBoolean = aBoolean;
    }

    public String getaString() {
        return aString;
    }

    public void setaString(String aString) {
        this.aString = aString;
    }

    public Integer[] getaArray() {
        return aArray;
    }

    public void setaArray(Integer[] aArray) {
        this.aArray = aArray;
    }

    public List getaList() {
        return aList;
    }

    public void setaList(List aList) {
        this.aList = aList;
    }

    public Set getaSet() {
        return aSet;
    }

    public void setaSet(Set aSet) {
        this.aSet = aSet;
    }

    public Map getaMap() {
        return aMap;
    }

    public void setaMap(Map aMap) {
        this.aMap = aMap;
    }

    @Override
    public String toString() {
        return "DataConfig{" +
                "aInteger=" + aInteger +
                ", aLong=" + aLong +
                ", aFloat=" + aFloat +
                ", aDouble=" + aDouble +
                ", aBoolean=" + aBoolean +
                ", aString='" + aString + '\'' +
                ", aArray=" + Arrays.toString(aArray) +
                ", aList=" + aList +
                ", aSet=" + aSet +
                ", aMap=" + aMap +
                '}';
    }
}

3.多个profile appllication-{profile}.properties/yml

3.1)appllication.yml

server:
  port: 80

spring:
  profiles:
    active: dev

3.2)appllication-dev.yml

server:
  port: 8080

3.3)appllication-prod.yml

server:
  port: 8081

3.4)激活制定profile

3.4.1)spring.profiles.active=dev

3.4.2) java -jar xxx.jar -spring.profiles.active=dev

3.4.3) 设置java虚拟机参数 -DSpring.profiles.active=dev

4.配置文件加载顺序

-file:./config

-file:./

-classpath:./config

-classpath:./

优先级由高到底,加载application.yml/properties

外部配置文件        java -jar xxx.jar --server.port=8080 --server.context=/abc

5.核心注解

@SpringBootApplication(启动相关) = @SpringBootConfiguration(@Configuration) + @EnableAutoConfiguration + @ComponentScan(可无 手动加载Bean)


@SpringBootConfiguration     配置加载注解
@EnableAutoConfiguration    自动化配置注解
@ComponentScan                 扫描+加载Bean

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {

}

6.自动化配置流程【META-INF/spring.factories】

@EnableAutoConfiguration
=> @Import({AutoConfigurationImportSelector.class})
=> AutoConfigurationImportSelector.selectImports
=> AutoConfigurationImportSelector.getAutoConfigurationEntry
=> AutoConfigurationImportSelector.getCandidateConfigurations
=> SpringFactoriesLoader.loadFactoryNames        扫描具有META-INF/spring.factories包

Springboot入门之配置文件_第1张图片

Springboot入门之配置文件_第2张图片

Springboot入门之配置文件_第3张图片

Springboot入门之配置文件_第4张图片

 MybatisPlus spring.factories 示例

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration,\
  com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration

你可能感兴趣的:(SpringBoot,spring,boot)