目錄
SpringBoot簡介
- 創建獨立的Spring應用程序
- 直接嵌入Tomcat,Jetty或Undertow(無需部署WAR文件)
- 提供自以爲是的“入門”依賴項,以簡化構建配置
- 儘可能自動配置Spring和3rd Party庫
- 提供生產就緒的功能,例如指標,運行狀況檢查和外部配置
- 完全沒有代碼生成,也不需要XML配置
Hello world
- idea創建Springboot項目
項目目錄:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String helloWorld(){
return "Hello SpringBoot";
}
}
運行SpringbootProjectApplication
SpringBoot註解分析
- @SpringBootApplication: Spring Boot應用標註在某個類上說明這個類是SpringBoot的主配置類,SpringBoot就應該運行這個類的main方法來啓動SpringBoot應用;
@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 {
- @SpringBootConfiguration:SpringBoot配置類
- @EnableAutoConfiguration:開啓自動配置功能; 開啓自動配置功能才能使配置生效
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
- @AutoConfigurationPackage:自動配置包
-
@Import(AutoConfigurationPackages.Registrar.class):
Spring的底層註解@Import,給容器中導入一個組件;導入的組件由AutoConfigurationPackages.Registrar.class;
將主配置類(@SpringBootApplication標註的類)的所在包及下面所有子包裏面的所有組件掃描到Spring容器;
AutoConfigurationImportSelector:自動導入哪些選擇器組件,將所有需要導入的組件以全類名的方式返回;這些組件就會被添加到容器中;會給容器中導入非常多的自動配置類(xxxAutoConfiguration);就是給容器中導入這個場景需要的所有組件,並配置好這些組件;
- 導入配置文件中的自動配置類,路徑:META-INF/spring.factories
SpringBoot配置
- SpringBoot的配置文件有兩種一種是YML(YAML),另一種是proproperties,properties是經常使用的一些配置文件,這裏的配置主要介紹YML(YAML)的配置形式。
- YML(YAML)以數據爲中心
- YML語法
- k:(空格)v:表示一對鍵值對(空格必須有);
- 屬性和值也是大小寫敏感;
- 字符串默認不用加上單引號或者雙引號;
- 裏面可以寫Map list 布爾值 對象
server:
port: 8081
person:
name: 張三
age: 18
birth-day: 2020/01/01
boss: true
map: {a: aa,b: bb,c: cc}
list:
- 1
- 2
- 3
cat:
name: xiaomao
age: 2
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
private Date birthDay;
private boolean boss;
private Map<String,Object> map;
private List<String> list;
private Cat cat;
set.....
get.....
toString....
}
-
導入配置處理器,綁定了類後配置文件就會有提示
<!--導入配置處理器,綁定了類後配置文件就會有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-configuration-processor </artifactId>
<optional>true</optional>
</dependency>
-
@Value獲取值和@ConfigurationProperties獲取值比較
@value如果在程序中需要單獨獲取某一個值那麼就可以使用這個註解註解 @ConfigurationProperties @Value 功能 批量注入配置文件中的屬性 一個個指定 鬆散綁定(鬆散語法) 支持 不支持 SpEL 不支持 支持 JSR303數據校驗 支持 不支持 複雜類型封裝 支持 不支持
@ConfigurationProperties如果使用javabean和配置文件進行映射就可以使用這個註解
- 在配置文件中可以使用佔位符獲取前面數據指定的值,沒有的話可以給一個默認值
cat:
name: ${person.name}.xiaomao
age: 2
cat:
name: ${person.name1:李四}.xiaomao
age: 2
- 可以獲取隨機數
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}
-
Profile配置
Profile是Spring對不同環境提供不同配置功能的支持,可以通過激活指定參數等方式快速切換環境
多Profile文件時:我們在主配置文件編寫的時候,文件名可以是 application-{profile}.properties/yml默認使用application.properties的 配置;
- yml支持多文檔塊方式
spring:
profiles:
active: pro
---
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: pro
---
server:
port: 8083
spring:
profiles: test
- 激活指定的profile
1、在配置文件中指定 spring.profiles.active=dev
2、命令行:
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
可以直接在測試的時候,配置傳入命令行參數
3、虛擬機參數;
-Dspring.profiles.active=dev
-
配置文件加載位置
springboot 啓動會掃描以下位置的application.properties或者application.yml文件作爲Spring boot的默認配置文件
–file:./config/
–file:./
–classpath:/config/
–classpath:/
優先級由高到底,高優先級的配置會覆蓋低優先級的配置;
SpringBoot會從這四個位置全部加載主配置文件;互補配置;