天天看點

SpringBoot(二)——配置相關1 熱部署(熱啟動)2 配置

文章目錄

  • 1 熱部署(熱啟動)
    • 1.1 導入依賴
    • 1.2 設定IDEA
    • 1.3 熱部署時機
    • 1.4 排除檔案
  • 2 配置
    • 2.1 配置檔案位置
    • 2.2 配置檔案名字
    • 2.3 yaml具體配置
    • 2.4 屬性綁定
    • 2.5 屬性綁定校驗
    • 2.6 兩個注解的差別
    • 2.7 java spi

1 熱部署(熱啟動)

SpringBoot提供熱部署的功能(重新啟動項目時,隻加載改變過的類檔案,是一種比較快的啟動方式)

1.1 導入依賴

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
           

1.2 設定IDEA

首先設定自動建構項目(發生改變時):

SpringBoot(二)——配置相關1 熱部署(熱啟動)2 配置

然後設定在項目運作時也重新建構項目(發生改變時)

ctrl+shift+alt+/

選擇Registry,找到compiler.automake.allow.when.app.running并選擇。

SpringBoot(二)——配置相關1 熱部署(熱啟動)2 配置

1.3 熱部署時機

IDEA并不會實時将項目重新啟動,而是會選擇項目有改動并且焦點移出編譯器時将項目重新啟動。

1.4 排除檔案

在yaml中配置需要排除自動部署的檔案夾

#熱部署排除(自動排除config下的檔案)
#spring.devtools.restart.exclude=config/**
           

同時,springboot預設一下檔案夾下的檔案都不會自動部署(因為是跟web相關的檔案,沒有必要自動部署),而是配置了ReLoad,也就是浏覽器的重新整理。

/META-INF/maven

,

/META-INF/resources

,

/resources

,

/static

,

/public

,

/templates

2 配置

SpringBoot的全局配置檔案,預設為:

  • Application.properties
  • Application.yaml

以上兩種檔案皆可。

2.1 配置檔案位置

對于maven項目,配置檔案的位置預設在resources目錄下,也就是class根目錄下,當然也可以在其他目錄下:

  • file:./config/
  • file:./(項目的根路徑,如果有父工程,則需要放到父工程的根路徑下)
  • classpath:/config/
  • classpath:/

優先級由高到低,高優先級的配置會覆寫低優先級的配置;

SpringBoot會從這四個位置全部加載主配置檔案;

互補配置;

2.2 配置檔案名字

配置檔案的名字預設是Application,如果要用其他的名字,需要通過參數指定配置檔案(當項目myproject的配置檔案名字為myproject時):

$java -jar myproject.jar --spring.config.name=myproject
           

項目開發時在如下地方配置:

SpringBoot(二)——配置相關1 熱部署(熱啟動)2 配置

配置檔案在特定位置

$java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
           

2.3 yaml具體配置

yaml學習

2.4 屬性綁定

/**
 * 将配置檔案中的屬性值,映射到這個元件中
 * @ConfigurationProperties: 告訴SpringBoot将本類中所有屬性和配置檔案中相關的配置進行綁定
 *          prefix = "song":配置檔案中哪個下面的所有屬性進行一一映射
 */
@ConfigurationProperties(prefix = "song")
@Component("s")
public class song implements Serializable {
    private String name;
    private Singer singer;
    private Long time;
    private Date publishTime;
    /*
    * setter and getter
    */
}
           

構造器綁定

使用

@ConstructorBinding

進行構造器綁定

@ConstructorBinding
@ConfigurationProperties(prefix = "song")
public class song implements Serializable {
    private String name;
    private Singer singer;
    private Long time;
    private Date publishTime;

    private Map<String,String> maps;
    private List<comment> comments;

    public song(String name, Singer singer, Long time, Date publishTime, Map<String, String> maps, List<comment> comments) {
        this.name = name;
        this.singer = singer;
        this.time = time;
        this.publishTime = publishTime;
        this.maps = maps;
        this.comments = comments;
    }
/*
* setter and getter
*/
}
           

在需要使用該配置的類上加上

@EnableConfigurationProperties(song.class)

開啟配置

@RestController
@RequestMapping("/demo")
@EnableConfigurationProperties(song.class)
public class DemoController {

    @Autowired
    private song s;

    @GetMapping("/echo")
    public song echo(){
        return s;
    }
}
           

第三方元件注入

除了使用

@ConfigurationProperties

來讓類綁定配置檔案中的值,還可以增加

@Bean

來給方法傳回的類綁定值,特别是要将屬性綁定到第三方元件中時。

@Component
public class MyService{
	@ConfigurationProperties("acme")
	@Bean
	public AnotherComponent anotherComponent(){
		return new AnotherComponent();
	}
}
           

松散綁定

Springboot使用一些寬松的規則将環境屬性綁定到

@ConfigurationProperties

,是以,環境屬性名和bean屬性名之間不需要完全比對。

@Data
@Component
//在這,不能用駝峰式,隻能用羊肉串模式
@ConfigurationProperties("acme.my-person.person")
public class OwnerProperties{
	private String firstName;
}
           
acme:
	my-person:
		person:
			first-name: Meteor
           
屬性檔案中配置 說明

acme.my-project.person.first-name

羊肉串模式,常用

acme.myProject.person.firstName

标準駝峰模式

acme.my_project.person.first_name

下劃線模式

ACME_MYPROJECT_PERSON_FIRSTNAME

大寫下劃線,系統環境推薦使用

2.5 屬性綁定校驗

檢驗配置檔案中傳過來的值是否有效。

需要的依賴:

<!--        配置綁定檢驗規範-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.0.Final</version>
        </dependency>
           

使用:

@ConstructorBinding
@ConfigurationProperties(prefix = "song")
@Validated //在類上加這個注解
@Data
public class song implements Serializable {

    @NotNull //不能為空
    private String name;
    @Max(35)  //最大值為35
    private int age;
    @Email  //格式為郵箱格式
    private String email;
}
           

如果出錯,控制台報錯:

SpringBoot(二)——配置相關1 熱部署(熱啟動)2 配置

當類的屬性是一個對象時,為了能讓該對象對應的類的屬性檢驗生效,需要給對象類型的屬性加

@valid

注解:

@ConstructorBinding
@ConfigurationProperties(prefix = "song")
@Validated //在類上加這個注解
@Data
public class song implements Serializable {

    @NotNull //不能為空
    private String name;
    @Max(35)  //最大值為35
    private int age;
    @Email  //格式為郵箱格式
    private String email;

	@valid
	private Singer singer;

	class Singer{
		@NotNull
		private String name;
	}
}
           

2.6 兩個注解的差別

@ConfigurationProperties

@Value

兩個注解都可以用于配置檔案屬性的綁定,但二者有一定的差別:

特點

@ConfigurationProperties

@Value

松散綁定 Yes Limit
中繼資料支援 Yes No
SpEL表達式

{}

No Yes
複雜類型綁定 Yes No
校驗 Yes No
應用場景 Boot裡面多個屬性的綁定 單個屬性的綁定

2.7 java spi

java spi 是“基于接口的程式設計+政策模式+配置檔案”組合實作的動态加載機制。

例如,mysql的jdbc(實作類A)和orcle的jdbc(實作類B)都實作了連接配接資料庫的接口功能(标準服務接口),我們(調用方)可以通過依賴jdbc來連接配接資料庫,這樣就實作了可插拔的子產品調用。

SpringBoot(二)——配置相關1 熱部署(熱啟動)2 配置

詳細見下面的文章:

java spi