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<String> valueList;

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

@Value("#{${data.aMap}}")
private Map<String, Object> 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<Integer> aList;
    private Set<Integer> aSet;
    private Map<String, String> 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<Integer> getaList() {
        return aList;
    }

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

    public Set<Integer> getaSet() {
        return aSet;
    }

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

    public Map<String, String> getaMap() {
        return aMap;
    }

    public void setaMap(Map<String, String> 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包

 MybatisPlus spring.factories 示例

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值