SpringBoot
SpringBoot概念
SpringBoot是基於Spring開發的,Spring Boot的設計是爲了讓你儘可能快的跑起來Spring應用程序並且儘可能減少你的配置。
它的設計思想是:約定大於配置
SpringBoot爲了解決Spring項目依賴多,配置繁瑣的問題,對Spring進行了改善和優化
- 起步依賴
起步依賴本質上是一個Maven項目對象模型,簡單的說就是把某些功能的依賴包都打包到一起,你引入一個就行了。
- 自動配置
springboot的自動配置指的是springboot會自動的將一些配置類的bean註冊進ioc容器。
“自動”的表現形式就是我們只需要引入我們想用功能的包,相關配置不用管,springboot就會自動注入這些配置bean。
SpringBoot項目構建
-
使用Spring Initializr方式構建Spring Boot項目
-
填寫包名
- 選擇對應的版本,和根據場景選擇依賴
- 點擊下一步,完成就可以了。
項目結構介紹:
附:解決中文亂碼
#設置響應爲utf-8
spring.http.encoding.force-response=true
配置詳解
1. application.properties配置文件
我們可以在application.properties文件中定義Spring Boot項目的相關屬性,當然,這些相關屬性可以
是系統屬性、環境變量、命令參數等信息,也可以是自定義配置文件名稱和位置
server.port=8081
spring.config.location=
spring.config.name=application
2. application.yaml配置文件
YAML文件格式是Spring Boot支持的一種JSON超集文件格式,相較於傳統的Properties配置文件,
YAML文件以數據爲核心,是一種更爲直觀且容易被電腦識別的數據序列化格式
- YAML文件的擴展名可以使用.yml或者.yaml
- application.yml文件使用 “key:(空格)value”格式配置屬性,使用縮進控制層級關係。
對於不同類型的屬性值,yml的配置格式不同
- value值爲普通數據類型
server:
port: 8081
- value值爲數組和單列集合
person:
hobby:
- aa
- bb
- cc
或
person:
hobby:
aa,
bb,
cc
或
person:
hobby: [aa,bb,cc]
- value值爲Map集合和對象
person:
map:
k1: v1
k2: v2
或
person:
map: {k1: v1,k2: v2}
3. 配置文件屬性值注入
- 使用@ConfigurationProperties注入屬性
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
配置文件:
person:
id: 2
name: 李明
- 使用@Value
package com.mmc.springbootdemo.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
public class Person {
@Value("${person.id}")
private Integer id;
@Value("${person.name}")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
4. 自定義配置文件
之前我們的配置文件都是寫在application.properties或application.yml裏的,如果我們自己新建的配置文件怎麼讀取呢?
使用@PropertySource
@Component
@ConfigurationProperties(prefix = "test")
@PropertySource("classpath:test.properties")
public class MyProperties {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5. 編寫配置類
在Spring Boot框架中,推薦使用配置類的方式向容器中添加和配置組件
在Spring Boot框架中,通常使用@Configuration註解定義一個配置類,Spring Boot會自動掃描和識別
配置類,從而替換傳統Spring框架中的XML配置文件。
@Configuration // 定義該類是一個配置類
public class MyConfig {
@Bean // 將返回值對象作爲組件添加到Spring容器中,該組件id默認爲方法名
public MyService myService(){
return new MyService();
}
}