天天看點

SpringBoot 屬性配置檔案資料注入配置和yml與properties差別

前言

我們知道

SpringBoot

通過配置類來解放一堆的xml檔案配置,通屬性配置檔案,來進行,系統全局屬性配置,這樣極大的簡化了我們開發過程,java web 也可以甜甜的從此
SpringBoot 屬性配置檔案資料注入配置和yml與properties差別
快速配置

Spring Boot

預設加載支援 application.properties、application.yaml和application*.yml三種拓展名結尾的全局屬性配置檔案處理

它們順序優先級為:

application*.properties

>

application*.yaml

application*.yml

即在application.properties或application.yml等檔案中添加屬性配置

可以使用@Value注解将屬性值注入到beans中,或使用@ConfigurationProperties注解将屬性值綁定到結構化的beans中

@Value是Spring架構提供的注解,用來讀取配置檔案中的屬性并逐個注入到Bean對象對應的屬性中,Spring Boot架構對Spring架構的@Value注解進行了預設繼承
  1. resources

    檔案下新增application.properties檔案,配置對應的屬性
student.name=kenx
student.age=23
           
  1. 新增java bean 把對應的屬性注入到java bean中對應字段使用@Value注解将屬性值注入到對應屬性上。
@Component
@Data
public class User {
    @Value("${student.name}")
    private String name;
    @Value("${student.age}")
    private Integer age;
}
           
@Component 添加到spring ioc容器中,@Data 添加getter,setter
  1. 寫用例測試
@SpringBootTest(
        webEnvironment = SpringBootTest.WebEnvironment.NONE,
        classes = cn.soboys.kmall.api.ApiApplication.class)
public class PropertiesTest {

    @Autowired
    private User properties;

    @Test
    public void a(){
        String a= String.format( "student name  is %s student age is %s",properties.getName(),properties.getAge());
        System.out.println(a);
    }
}
           

我看可以看到控制台正常列印,資料注入成功

2021-09-08 10:53:02 INFO  background-preinit org.hibernate.validator.internal.util.Version HV000001: Hibernate Validator 6.1.7.Final
2021-09-08 10:53:02 INFO  main PropertiesTest Starting PropertiesTest using Java 1.8.0_202 on xiangyongdeMacBook-Pro.local with PID 45463 (started by xiangyong in /Users/xiangyong/selfProject/project/kmall/kmall-api)
2021-09-08 10:53:02 INFO  main PropertiesTest The following profiles are active: test,mptest
 _ _   |_  _ _|_. ___ _ |    _ 
| | |\/|_)(_| | |_\  |_)||_|_\ 
     /               |         
                        3.4.1 
2021-09-08 10:53:08 INFO  main PropertiesTest Started PropertiesTest in 6.132 seconds (JVM running for 7.783)

student name  is kenx student age is 23
           
  1. @ConfigurationProperties

    注解将屬性值綁定到結構化的beans

上面通過

@Value

一個·一個注入很不友善

@Component
@Data
@ConfigurationProperties(prefix = "student")
public class User {
    private String name;
    private Integer age;
}
           
這樣極大簡化代碼,對于屬性比較多,結構化bean,很有必要可以通過

@ConfigurationProperties(prefix = "student")

這種方式指定字首

當然有時候我們需要自定義加載屬性配置檔案 使用

@PropertySource

加載配置檔案

test.id=100
test.name=lucy

           
package com.lzx.springboot01demo.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration		// 自定義配置類
@PropertySource("classpath:test.properties") 	// 指定自定義配置檔案位置和名稱
@EnableConfigurationProperties(MyProperties.class)		// 開啟對應配置類的屬性注入功能
@ConfigurationProperties(prefix = "test")		// 指定配置檔案注入屬性字首
public class MyProperties {

    private Integer id;
    private String name;

    // 省略getter/setter方法
    // 省略toString()方法
}

           

1.

@Configuration

注解表示目前類是一個自定義配置類,并添加為Spring容器的元件,也可使用傳統的

@Component

注解

  1. @PropertySource("classpath:test.properties")

    指定自定義配置檔案位置和名稱
  2. @ConfigurationProperties(prefix = "test")

    指定将配置檔案中字首為test的屬性注入到配置類的屬性中
  3. @EnableConfigurationProperties(MyProperties.class)

    表示開啟對應配置類的屬性注入功能,如果配置類上使用的是@Component注解而非@Configuration,@EnableConfigurationProperties(MyProperties.class)注解可以省略

application.properties配置檔案

#配置數字
person.id=1
#配置字元串
person.name=tom
#配置List集合
person.hoby=吃飯,睡覺,打豆豆
#配置String[]數組
person.family=father,mother
#配置map集合
person.map.k1=v1
person.map.k2=v2
#配置對象type屬性
person.pet.type=dog
#配置對象name屬性
person.pet.name=旺财
           

application.y(a)ml配置檔案

  1. value值為普通資料類型(例如:數字、字元串、布爾)
server:
  port: 8081
  path: /hello

           
  1. value值為數組或單列集合

主要有兩種寫法:縮進式寫法和行内式寫法;其中縮進式寫法又有兩種寫法:

縮進式寫法1

person:
  hobby:
    - play
    - read
    - sleep
           

縮進式寫法2

person:
  hobby:
    play,
    read,
    sleep

           

行内式寫法:

person:
  hobby: [play,read,sleep]

           
  1. value值為Map或對象

縮進式寫法

person:
  map:
    k1: v1
    k2: v2

           
person:
  map: {k1: v1, k2: v2}

           
注意 使用Spring Boot全局配置檔案設定屬性時,

如果配置的屬性是已有屬性,例如服務端口server.port,那麼Spring Boot會掃描并讀取這些配置屬性,

覆寫

已有的預設配置;

如果配置的是自定義屬性,則還需要在程式中

注入

這些配置屬性方可

生效

預設屬性和參數引用

# 配置随機值
my.secret=${random.value}
# 配置随機整數
my.number=${random.int}
# 配置随機long類型的整數
my.bigbumber=${random.long}
# 配置uuid
my.uuid=${random.uuid}
# 配置小于10的整數
my.number.less.than.ten=${random.int(10)}
# 配置範圍在[1024,65536]的随機整數
my.number.in.range=${random.int[1024,65536]}
           
# 參數間引用
app.name=MyApp
app.description=${app.name} is a Spring Boot application

           

繼續閱讀