SpringBoot - 快速入門(SpringBoot 1.5.9.RELEASE爲例)

一、Spring Boot 入門

1、Spring Boot 簡介

  • 簡化Spring應用開發的一個框架;
  • 整個Spring技術棧的一個大整合;
  • J2EE開發的一站式解決方案;

2、微服務

2014,martin fowler提出

  • 微服務:架構風格(服務微化)

  • 一個應用應該是一組小型服務;可以通過HTTP的方式進行互通;

單體應用:ALL IN ONE

微服務:每一個功能元素最終都是一個可獨立替換和獨立升級的軟件單元;

詳細參照微服務文檔:https://martinfowler.com/articles/microservices.html#MicroservicesAndSoa

3、Spring Boot HelloWorld

環境準備:

  • 這裏以 SpringBoot 1.5.9.RELEASE版本爲例。

MAVEN設置:

  • 給maven 的settings.xml配置文件的profiles標籤添加
<profile>
	<id>jdk‐1.8</id>
	<activation>
		<activeByDefault>true</activeByDefault>
		<jdk>1.8</jdk>
	</activation>
	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
	</properties>
</profile>

基本功能:

  • 瀏覽器發送hello請求,服務器接受請求並處理,響應Hello World字符串;

1、創建一個maven工程

2、導入spring boot相關的依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.onefine</groupId>
    <artifactId>springboot_maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!-- 父項目:真正管理springboot應用裏的所有依賴版本
            也稱爲springboot的版本仲裁中心,即以後導入依賴默認不需要指明版本號
            但若沒有在dependencies裏面管理的依賴自然需要聲明版本號
    -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <!-- 啓動器
             spring-boot-starter:spring-boot場景啓動器,幫我們導入了web模塊正常運行所依賴的組件;
             Spring Boot將所有的功能場景都抽取出來,做成一個個的starters(啓動器)
             只需要在項目裏面引入這些starter 相關場景的所有依賴都會導入進來。要用什麼功能就導入什麼場景的啓動器
         -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- 引入此插件可以將應用打包成一個可執行的jar包 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、編寫一個主程序:啓動Spring Boot應用

package top.onefine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication  // 標註一個主程序,用來說明這是一個springboot應用
public class HelloWorldMainApplication {

    //
    public static void main(String[] args) {
        // 啓動springboot應用程序
        SpringApplication.run(HelloWorldMainApplication.class, args);
    }
}

4、編寫相關的Controller或Service

package top.onefine.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return "Hello SpringBoot";
    }
}

或者:

package top.onefine.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

//@ResponseBody  // 作用在類上表示這個類的所有方法返回的數據直接寫給瀏覽器,和作用在單獨的方法上效果一致
//@Controller
@RestController  // = @ResponseBody + @Controller
public class HelloController {

//    @ResponseBody  // 作用在方法上表示這個方法返回的數據直接寫給瀏覽器(如果是對象還能轉爲json數據)
    @RequestMapping("/hello")
    public String hello() {
        return "hello world quick!";
    }
}

5、運行主程序測試

執行main方法,瀏覽器中輸入127.0.0.1:8080/hello
在這裏插入圖片描述

6、簡化部署

<!-- 引入此插件可以將應用打包成一個可執行的jar包; -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

將這個應用打成jar包:Maven -> 項目名稱 -> Lifecycle -> package

找到jar包導出位置:項目名稱/target/項目名-版本號.jar

直接使用java -jar的命令進行執行:

# 查看指定端口被佔用情況
D:\projects\java\study_springboot\springboot_maven\target>netstat -ano|findstr "8080"
  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       9392
  TCP    [::]:8080              [::]:0                 LISTENING       9392

# 關閉進程號對應的進程
D:\projects\java\study_springboot\springboot_maven\target>taskkill -PID 9392 -F
成功: 已終止 PID 爲 9392 的進程。

D:\projects\java\study_springboot\springboot_maven\target>java -jar springboot_maven-1.0-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

2020-05-23 11:01:58.183  INFO 10016 --- [           main] top.onefine.HelloWorldMainApplication    : Starting HelloWorldMainApplication v1.0-SNAPSHOT on DESKTOP-LNTOAM6 with PID 10016 (D:\projects\java\study_springboot\springboot_maven\target\springboot_maven-1.0-SNAPSHOT.jar started by Lenovo in D:\projects\java\study_springboot\springboot_maven\target)
2020-05-23 11:01:58.187  INFO 10016 --- [           main] top.onefine.HelloWorldMainApplication    : No active profile set, falling back to default profiles: default
2020-05-23 11:01:58.258  INFO 10016 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1698c449: startup date [Sat May 23 11:01:58 CST 2020]; root of context hierarchy
2020-05-23 11:01:59.736  INFO 10016 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2020-05-23 11:01:59.754  INFO 10016 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-05-23 11:01:59.757  INFO 10016 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2020-05-23 11:01:59.870  INFO 10016 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-05-23 11:01:59.870  INFO 10016 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1615 ms
2020-05-23 11:01:59.983  INFO 10016 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2020-05-23 11:01:59.987  INFO 10016 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2020-05-23 11:01:59.987  INFO 10016 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-05-23 11:01:59.987  INFO 10016 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2020-05-23 11:01:59.987  INFO 10016 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2020-05-23 11:02:00.294  INFO 10016 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1698c449: startup date [Sat May 23 11:01:58 CST 2020]; root of context hierarchy
2020-05-23 11:02:00.415  INFO 10016 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String top.onefine.controller.HelloController.hello()
2020-05-23 11:02:00.421  INFO 10016 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2020-05-23 11:02:00.422  INFO 10016 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2020-05-23 11:02:00.468  INFO 10016 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-05-23 11:02:00.469  INFO 10016 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-05-23 11:02:00.527  INFO 10016 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-05-23 11:02:00.651  INFO 10016 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2020-05-23 11:02:00.732  INFO 10016 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2020-05-23 11:02:00.740  INFO 10016 --- [           main] top.onefine.HelloWorldMainApplication    : Started HelloWorldMainApplication in 2.904 seconds (JVM running for 3.238)

4、主程序類、主入口類探究

@SpringBootApplication  // 標註一個主程序,用來說明這是一個springboot應用
public class HelloWorldMainApplication {

    //
    public static void main(String[] args) {
        // 啓動springboot應用程序
        SpringApplication.run(HelloWorldMainApplication.class, args);
    }
}

@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:Spring Boot的配置類;標註在某個類上,表示這是一個Spring Boot的配置類;

@Configuration:配置類上來標註這個註解;

  • 配置類(和配置文件一樣的作用);
  • 配置類也是容器中的一個組件:@Component

@EnableAutoConfiguration:開啓自動配置功能;
以前我們需要配置的東西,現在Spring Boot幫我們自動配置;@EnableAutoConfiguration告訴SpringBoot開啓自
動配置功能,這樣自動配置才能生效;@EnableAutoConfiguration源碼如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
	// ...
}

解釋:

@AutoConfigurationPackage:自動配置包

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {

}

@Import(AutoConfigurationPackages.Registrar.class)

  • Spring的底層註解@Import,給容器中導入一個組件;
  • 導入的組件由AutoConfigurationPackages.Registrar.class 將主配置類(@SpringBootApplication標註的類)的所在包及下面所有子包裏面的所有組件掃描到Spring容器;

@Import(EnableAutoConfigurationImportSelector.class):給容器中導入組件EnableAutoConfigurationImportSelector

  • EnableAutoConfigurationImportSelector:導入哪些組件的選擇器;
  • 將所有需要導入的組件以全類名的方式返回,這些組件就會被添加到容器中;
  • 會給容器中導入非常多的自動配置類(xxxAutoConfiguration):作用就是給容器中導入這個場景需要的所有組件,並配置好這些組件;

在這裏插入圖片描述

  • 有了自動配置類,免去了我們手動編寫配置注入功能組件等的工作;SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader):Spring Boot在啓動的時候從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值,將這些值作爲自動配置類導入到容器中,自動配置類就生效,幫我們進行自動配置工作;以前我們需要自己配置的東西,自動配置類都幫我們;J2EE的整體整合解決方案和自動配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar中做好了。

5、resources文件夾中目錄結構

  • static:保存所有的靜態資源; js、css、images;
  • templates:保存所有的模板頁面;Spring Boot默認jar包使用嵌入式的Tomcat,默認不支持JSP頁面;可以使用模板引擎(freemarker、thymeleaf);
  • application.properties:Spring Boot應用的配置文件;可以修改一些默認設置;

二、配置文件

SpringBoot使用一個全局的配置文件,配置文件名是固定的;

  • application.properties
  • application.yml

配置文件的作用:由於SpringBoot在底層都給我們自動配置好大多數配置,配置文件用於修改SpringBoot自動配置的這些默認值;

YAML(YAML Ain’t Markup Language)

  • YAML A Markup Language:是一個標記語言
  • YAML isn’t Markup Language:不是一個標記語言;

標記語言

  • 以前的配置文件;大多都使用的是 xxxx.xml文件;
  • YAML:以數據爲中心,比json、xml等更適合做配置文件;

YAML:配置例子

server:
	port: 8081

對應XML形式:

<server>
	<port>8081</port>
</server>

對應properties形式:

server.port=8081

1、YAML語法:

1.1、基本語法

k:(空格)v:表示一對鍵值對(空格必須有);

  • 以空格的縮進來控制層級關係;只要是左對齊的一列數據,都是同一個層級的
server: 
	port: 8081
	path: /hello
  • 屬性和值也是大小寫敏感;

1.2、值的寫法

字面量:普通的值(數字,字符串,布爾)

  • k: v:字面直接來寫;
  • 字符串默認不用加上單引號或者雙引號;
    • "":雙引號;不會轉義字符串裏面的特殊字符;特殊字符會作爲本身想表示的意思
      name: "zhangsan \n lisi":輸出;zhangsan 換行 lisi
    • '':單引號;會轉義特殊字符,特殊字符最終只是一個普通的字符串數據
      name: ‘zhangsan \n lisi’:輸出;zhangsan \n lisi

對象、Map(屬性和值)(鍵值對)

  • k: v:在下一行來寫對象的屬性和值的關係;注意縮進!

  • 對象還是k: v的方式

friends:
	lastName: zhangsan
	age: 20
  • 行內寫法:
friends: {lastName: zhangsan,age: 18}

數組(List、Set)

  • - 值表示數組中的一個元素
pets:
	‐ cat
	‐ dog
	‐ pig
  • 行內寫法
pets: [cat,dog,pig]

2、配置文件值注入

配置文件src\main\resources\application.yml

server:
  port: 8090

person:
  lastName: one fine中文
  age: 18
  boss: false
  birth: 1996/3/1
  map: {k1: v1, k2: 12}
  list:
    - li si
    - wang wu
  dog:
    name: 小狗
    age: 2

javaBean:

package top.onefine.bean;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 將配置文件中配置的每一個屬性的值映射到這個組件中
 *
 * @ ConfigurationProperties
 *      告訴springboot將本類中的所有屬性和配置文件中相關的配置進行綁定
 * prefix = "person"
 *      配置文件中哪個下面的所有屬性進行一一映射
 *
 *  注意:只有當這個組件是容器中的組件,才能使用容器提供的@ ConfigurationProperties功能
 *      @ Component 將組件加入到容器中
 */
@Getter
@Setter
@ToString
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;

    private Map<String, Object> map;
    private List<Object> list;
    private Dog dog;
}
package top.onefine.bean;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class Dog {

    private String name;
    private String age;
}

我們可以導入配置文件處理器,以後編寫配置就有提示了:

<!‐‐導入配置文件處理器,配置文件進行綁定就會有提示‐‐>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring‐boot‐configuration‐processor</artifactId>
	<optional>true</optional>
</dependency>

測試類:

package top.onefine;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import top.onefine.bean.Person;

/**
 * springboot單元測試
 *
 */
@SpringBootTest
class SpringbootQuickApplicationTests {

    @Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }
}

結果:

Person(lastName=one fine中文, age=18, boss=false, birth=Fri Mar 01 00:00:00 CST 1996, map={k1=v1, k2=12}, list=[li si, wang wu], dog=Dog(name=小狗, age=2))

附:完整pom.xml(這裏使用springboot 2.3.0.RELEASE):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>top.onefine</groupId>
    <artifactId>springboot_quick</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>springboot_quick</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- springboot進行單元測試的模塊 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 配置文件處理器,導入後配置文件進行綁定時就會有提示 -->
        <!--
        https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、properties形式

server.port=8091

person.last-name=one中文
#[email protected]
person.age=18
person.birth=1996/3/1
person.boss=false
person.map.k1=v1
person.map.k2=v2
person.list=a,b,c
person.dog.name=dog
person.dog.age=15

properties配置文件在idea中默認utf-8可能會亂碼,更改idea設置:

在這裏插入圖片描述

4、@Value獲取值

package top.onefine.bean;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@Component
//@ConfigurationProperties(prefix = "person")
public class Person {

    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面量/${key}從環境變量、配置文件中獲取值/{SpEL}"></property>
     * <bean/>
     */
    @Value(value = "${person.last-name}")  // ${key}從環境變量、配置文件中獲取值
    private String lastName;
    @Value("#{9 * 2}")  // spring表達式語言:{SpEL}
    private Integer age;
    @Value("true")  // 字面量
    private Boolean boss;
    private Date birth;

    private Map<String, Object> map;
    private List<Object> list;
    private Dog dog;
}

運行測試:
在這裏插入圖片描述

@Value獲取值和@ConfigurationProperties獲取值比較:
@ConfigurationProperties @Value
功能 批量注入配置文件中的屬性 一個個指定
鬆散綁定(鬆散語法) 支持 不支持
SpEL 不支持 支持
JSR303數據校驗 支持 不支持
複雜類型封裝 支持 不支持

鬆散綁定
在這裏插入圖片描述

JSR303數據校驗

maven中導入座標:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.3.6.Final</version>
</dependency>
package top.onefine.bean;

import lombok.Data;
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@Component
@Validated  // 數據校驗
@ConfigurationProperties(prefix = "person")
public class Person {

//    @Value(value = "${person.last-name}")  // ${key}從環境變量、配置文件中獲取值
    @Email  // 數據校驗:lastName必須是郵箱格式
    private String lastName;
//    @Value("#{9 * 2}")  // spring表達式語言:{SpEL}
    private Integer age;
//    @Value("true")  // 字面量
    private Boolean boss;
    private Date birth;

    private Map<String, Object> map;
    private List<Object> list;
    private Dog dog;
}

執行:

在這裏插入圖片描述

關於複雜類型封裝

package top.onefine.bean;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@Component
//@ConfigurationProperties(prefix = "person")
public class Person {

    @Value(value = "${person.last-name}")  // ${key}從環境變量、配置文件中獲取值
    private String lastName;
    @Value("#{9 * 2}")  // spring表達式語言:{SpEL}
    private Integer age;
    @Value("true")  // 字面量
    private Boolean boss;
    private Date birth;

    @Value("${person.dog.name}")
    private Map<String, Object> map;
    private List<Object> list;
    private Dog dog;
}

在這裏插入圖片描述

總結

  • 配置文件yml還是properties他們都能獲取到值;
  • 若只是在某個業務邏輯中需要獲取一下配置文件中的某項值,使用@Value
  • 若專門編寫了一個javaBean來和配置文件進行映射,我們就直接使用@ConfigurationProperties

某個業務邏輯舉例:

package top.onefine.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HiDemo {

    @Value("${person.last-name}")
    private String name;

    @RequestMapping("/sayHi")
    public String sayHello() {
        return "hello " + name;
    }
}
@PropertySource&@ImportResource&@Bean

@ConfigurationProperties(prefix = "person")默認從全局配置文件中獲取值。

@PropertySource:加載指定的配置文件;

配置文件person.properties:

person.last-name=fine配置
person.age=18
person.birth=1996/3/1
person.boss=false
person.map.k1=v1
person.map.k2=v2
person.list=a,b,c
person.dog.name=小狗
person.dog.age=15
package top.onefine.bean;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@Component
@ConfigurationProperties(prefix = "person")  // 默認從全局配置文件中獲取值
@PropertySource(value = {"classpath:person.properties"})  // 修改加載指定配置文件位置,不從全局配置文件中加載值
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String, Object> map;
    private List<Object> list;
    private Dog dog;
}

在這裏插入圖片描述

注:加載順序爲application.properties->application.yml->person.properties

@ImportResource:導入Spring的配置文件,讓配置文件裏面的內容生效;

  • Spring Boot裏面沒有Spring的配置文件,我們自己編寫的配置文件,也不能自動識別;想使用Spring的配置文件加載進來生效:使用@ImportResource標註在一個配置類上:

src\main\java\top\onefine\service\HelloService.java:

package top.onefine.service;

public class HelloService {

}

spring的配置文件beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloService" class="top.onefine.service.HelloService" />

</beans>
@package top.onefine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@ImportResource(locations = {"classpath:beans.xml"})  // 導入Spring的配置文件讓其生效
@SpringBootApplication
public class SpringbootQuickApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootQuickApplication.class, args);
    }

}

測試類:

package top.onefine;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

/**
 * springboot單元測試
 *
 */
@SpringBootTest
class SpringbootQuickApplicationTests {
    @Autowired
    ApplicationContext ioc;  // IOC容器

    @Test
    void contextLoads() {
        boolean b = ioc.containsBean("helloService");
        System.out.println(b ? "存在" : "不存在");
    }
}

SpringBoot推薦給容器中添加組件的方式:使用全註解的方式,不要編寫這裏的配置文件beans.xml和啓動器上的@ImportResource註解
1、配置類@Configuration—替代—>Spring配置文件 bean.xml

2、使用@Bean給容器中添加組件

package top.onefine.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.onefine.service.HelloService;

@Configuration  // 標明這是一個配置類,替代spring的配置文件如剛纔的beans.xml
public class MyAppConfig {
    // 剛纔的配置文件使用<bean></bean>標籤來添加組件

    //
    // 將方法的返回值添加到容器中,容器中組件默認的id就是方法名
    @Bean
    public HelloService helloService() {
        return new HelloService();
    }
}

5、配置文件佔位符

佔位符獲取之前配置的值,如果沒有可以是用:指定默認值

${random.value}
${random.int}
${random.long}
${random.int(10)}
${random.int[1024,65536]}
// ...

舉例:

server.port=8091
# 隨機數
person.last-name=one中文${random.uuid}
#person.last-name=onefine@qq.com
person.age=${random.int}
person.birth=1996/3/1
person.boss=false
# 獲取不存在的值
person.map.k1=${one}
# 不存在則設置默認值v2
person.map.k2=${one:v2}
person.list=a,b,c
# 引用值
person.dog.name=${person.last-name}的小狗
person.dog.age=15

執行測試文件:

package top.onefine;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import top.onefine.bean.Person;

@SpringBootTest
class SpringbootQuickApplicationTests {

    @Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }
}

結果:
在這裏插入圖片描述

6、Profile

Profile是Spring對不同環境提供不同配置功能的支持,可以通過激活、指定參數等方式快速切換環境。

6.1、多Profile文件

在編寫主配置文件的時候,文件名可以是 application-{profile}.properties/yml

  • application-dev.properties
  • application-prod.properties

默認使用application.properties的配置

6.2、激活指定profile方式

1、在配置文件(src\main\resources\application.properties)中指定

server.port=8081

# 激活開發環境
#spring.profiles.active=dev

# 激活生產環境
spring.profiles.active=prod

2、命令行:

java -jar springboot_quick-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

在這裏插入圖片描述

可以直接在測試的時候,配置傳入命令行參數

在這裏插入圖片描述

3、虛擬機參數:

# jvm參數
-Dspring.profiles.active=dev

在這裏插入圖片描述

6.3、yml支持多文檔塊方式

使用---分隔文檔塊

# 文檔塊1
server:
  port: 8081
  servlet:
    context-path: /boot

## 配置項目的訪問路徑


## 激活開發環境
#spring:
#  profiles:
#    active: dev

## 激活生產環境
spring:
  profiles:
    active: prod
---

# 文檔塊2
server:
  port: 8082

## 指定環境:開發環境
spring:
  profiles: dev
---

# 文檔塊3
server:
  port: 8083

## 指定環境:生產環境
spring:
  profiles: prod

7、配置文件加載位置

springboot 啓動會掃描以下位置的application.properties或者application.yml文件作爲Spring boot的默認配置文件:

  • file:./config/ :file表示當前項目文件路徑,在項目名/
  • file:./
  • classpath:/config/ :classpath表示當前項目類路徑,在項目名/src/main/resources/
  • classpath:/

以上是優先級由高到低的順序排列,所有位置的文件都會被加載,高優先級的配置會覆蓋低優先級的配置;

SpringBoot會從這四個位置全部加載主配置文件,互補配置

我們還可以通過spring.config.location來改變默認的配置文件位置:項目打包好以後,我們可以使用命令行參數的形式,啓動項目的時候來指定配置文件的新位置;指定配置文件和默認加載的這些配置文件共同起作用形成互補配置

java -jar springboot_quick-0.0.1-SNAPSHOT.jar --spring.config.location=G:/application.properties

8、外部配置加載順序

SpringBoot也可以從以下位置加載配置,優先級從高到低,且高優先級的配置覆蓋低優先級的配置,所有的配置會形成互補配置:

  • 1.命令行參數
    • 所有的配置都可以在命令行上進行指定
    • java -jar springboot_quick-0.0.1-SNAPSHOT.jar --server.port=8087 --server.context-path=/abc
    • 多個配置用空格分開,格式爲:--配置項=值
  • 2.來自java:comp/env的JNDI屬性
  • 3.Java系統屬性(System.getProperties())
  • 4.操作系統環境變量
  • 5.RandomValuePropertySource配置的random.*屬性值

由jar包外向jar包內進行尋找,優先加載帶profile:

  • 6.jar包外部的application-{profile}.properties或application.yml(帶spring.profile)配置文件
  • 7.jar包內部的application-{profile}.properties或application.yml(帶spring.profile)配置文件

注:
jar包外部是指和jar同一級目錄
jar包內部是指項目打包前項目內的配置文件

再來加載不帶profile:

  • 8.jar包外部的application.properties或application.yml(不帶spring.profile)配置文件
  • 9.jar包內部的application.properties或application.yml(不帶spring.profile)配置文件

  • 10.@Configuration註解類上的@PropertySource
  • 11.通過SpringApplication.setDefaultProperties指定的默認屬性

以上僅列出常用的配置加載來源,所有支持的配置加載來源,詳參考官方文檔

9、自動配置原理

配置文件到底能寫什麼?怎麼寫?自動配置原理;

配置文件能配置的屬性參照:官方文檔

9.1、自動配置原理:

1)、SpringBoot啓動的時候加載主配置類,開啓了自動配置功能 @EnableAutoConfiguration
2)、@EnableAutoConfiguration 作用:

  • 利用EnableAutoConfigurationImportSelector給spring容器中導入一些組件
  • 可以查看selectImports()方法的內容:List configurations = getCandidateConfigurations(annotationMetadata, attributes);獲取候選的配置。
SpringFactoriesLoader.loadFactoryNames()
// 掃描所有jar包類路徑下 META‐INF/spring.factories 文件
// 把掃描到的這些文件的內容包裝成properties對象
// 從properties中獲取到EnableAutoConfiguration.class類(類名)對應的值,然後把他們添加在容器中

// 將類路徑下 `META-INF/spring.factories` 裏面配置的所有`EnableAutoConfiguration`的值加入到了容器中:
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\ 
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceResolverAutoConfiguration,\
org.springframework.boot.autoconfigure.mobile.DeviceDelegatingViewResolverAutoConfiguration,\ 
org.springframework.boot.autoconfigure.mobile.SitePreferenceAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.reactor.ReactorAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration,\
org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration,\
org.springframework.boot.autoconfigure.social.LinkedInAutoConfiguration,\
org.springframework.boot.autoconfigure.social.TwitterAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration

// 每一個這樣的 xxxAutoConfiguration類都是容器中的一個組件,都加入到容器中;用他們來做自動配置;

3)、每一個自動配置類進行自動配置功能;

4)、以HttpEncodingAutoConfiguration(Http編碼自動配置)爲例解釋自動配置原理:

@Configuration //表示這是一個配置類,和以前編寫的配置文件一樣,也可以給容器中添加組件
//啓動指定類的ConfigurationProperties功能;將配置文件中對應的值和HttpEncodingProperties綁定起來;並把HttpEncodingProperties加入到ioc容器中
@EnableConfigurationProperties(HttpEncodingProperties.class) 

//Spring底層@Conditional註解,根據不同的條件,如果滿足指定的條件,整個配置類裏面的配置就會生效; 判斷當前應用是否是web應用,如果是,當前配置類生效
@ConditionalOnWebApplication 

//判斷當前項目有沒有CharacterEncodingFilter這個類,這是SpringMVC中進行亂碼解決的過濾器;
@ConditionalOnClass(CharacterEncodingFilter.class) 

//判斷配置文件中是否存在某個配置 spring.http.encoding.enabled;matchIfMissing 表示如果不存在,判斷也是成立的,即默認配置。
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true) 
//即使我們配置文件中不配置pring.http.encoding.enabled=true,也是默認生效的;
public class HttpEncodingAutoConfiguration {
	//他已經和SpringBoot的配置文件映射了
	private final HttpEncodingProperties properties;
	
	//只有一個有參構造器的情況下,參數的值就會從容器中拿
	public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {
		this.properties = properties;
	} 
	
	@Bean //給容器中添加一個組件,這個組件的某些值需要從properties中獲取
	@ConditionalOnMissingBean(CharacterEncodingFilter.class) //判斷容器沒有這個組件?
	public CharacterEncodingFilter characterEncodingFilter() {
		CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
		filter.setEncoding(this.properties.getCharset().name());
		filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
		filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
		return filter;
	}
}

根據當前不同的條件判斷,決定這個配置類是否生效。一但這個配置類生效,這個配置類就會給容器中添加各種組件;這些組件的屬性是從對應的properties類中獲取的,這些類裏面的每一個屬性又是和配置文件綁定的;

5)、所有在配置文件中能配置的屬性都是在xxxxProperties類中封裝者‘,配置文件能配置什麼就可以參照某個功能對應的這個屬性類:

@ConfigurationProperties(prefix = "spring.http.encoding") //從配置文件中獲取指定的值和bean的屬性進行綁定
public class HttpEncodingProperties {
	public static final Charset DEFAULT_CHARSET = Charset.forName("UTF‐8");
}

springboot精髓

1)、SpringBoot啓動會加載大量的自動配置類
2)、我們看我們需要的功能有沒有SpringBoot默認寫好的自動配置類;
3)、我們再來看這個自動配置類中到底配置了哪些組件;(只要要用的組件有,就不需要再來配置了)
4)、給容器中自動配置類添加組件的時候,會從properties類中獲取某些屬性。我們就可以在配置文件中指定這些屬性的值;

總結:

  • xxxxAutoConfigurartion:自動配置類,會給容器中添加組件;

  • xxxxProperties:封裝配置文件中相關屬性;

9.2、細節

1、@Conditional派生註解

作用:必須是@Conditional指定的條件成立,纔給容器中添加組件,配置配裏面的所有內容才生效;

自動配置類必須在一定的條件下才能生效:

@Conditional擴展註解 作用(判斷是否滿足當前指定條件)
@ConditionalOnJava 系統的java版本是否符合要求
@ConditionalOnBean 容器中存在指定Bean;
@ConditionalOnMissingBean 容器中不存在指定Bean;
@ConditionalOnExpression 滿足SpEL表達式指定
@ConditionalOnClass 系統中有指定的類
@ConditionalOnMissingClass 系統中沒有指定的類
@ConditionalOnSingleCandidate 容器中只有一個指定的Bean,或者這個Bean是首選Bean
@ConditionalOnProperty 系統中指定的屬性是否有指定的值
@ConditionalOnResource 類路徑下是否存在指定資源文件
@ConditionalOnWebApplication 當前是web環境
@ConditionalOnNotWebApplication 當前不是web環境
@ConditionalOnJndi JNDI 存在指定項

我們怎麼知道哪些自動配置類生效?可以通過在配置文件中啓用 debug=true屬性;來讓控制檯打印自動配置報告,這樣我們就可以很方便的知道哪些自動配置類生效:

# 開啓springboot的debug模式,這裏是在application.yml中配置的
debug: true

項目運行時控制檯打印結果

============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:(自動配置類啓用的)
-----------------

   AopAutoConfiguration matched:
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

   AopAutoConfiguration.ClassProxyingConfiguration matched:
      - @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
      - @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)

   # ...省略


Negative matches:(沒有啓動,沒有匹配成功的自動配置類)

-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

   AopAutoConfiguration.AspectJAutoProxyingConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.aspectj.weaver.Advice' (OnClassCondition)
	# ...省略
  
Exclusions:
-----------

    None


Unconditional classes:
----------------------

    org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration

    org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration

    org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration

    org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration

    org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration


一. 啓動

1. idea啓動

2. 命令行啓動

luckymoney> mvn spring-boot:run

3. 服務器端打包方式

luckymoney> mvn clean package
# ...
luckymoney> java -jar target/luckymoney-0.0.1-SNAPSHOP.jar

二、配置

0. 使用0

src/main/resources/application.properties

server.port = 8081
server.servlet.context-path = /luckymoney

1. 使用1

  1. src/main/resources/application.yml
server:
  port: 8082
  servlet:
    context-path: /luckymoney

minMoney: 1
description: 最少要發${minMoney}
  1. src/main/java/top/onefine/luckymoney/HelloController.java:@Value註解
package top.onefine.luckymoney;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
public class HelloController {

    @Value("${minMoney}")
    private BigDecimal minMoney;

    @Value("${description}")
    private String description;

    @GetMapping("/hello")
    public String say() {
        return "開啓SpringBoot! 金額:" + minMoney + ", 說明:" + description;
    }
}

2. 使用2

src/main/resources/application.yml

spring:
  profiles:
    active: prod

src/main/resources/application-dev.yml

# 開發環境
server:
  port: 8082
  servlet:
    context-path: /luckymoney

# 使用對象配置
limit:
  minMoney: 0.01
  maxMoney: 9999
  description: 最少要發${limit.minMoney}元,最多${limit.maxMoney}

src/main/resources/application-prod.yml

# 生產環境
server:
  port: 8082
  servlet:
    context-path: /luckymoney

# 使用對象配置
limit:
  minMoney: 1
  maxMoney: 9999
  description: 最少要發${limit.minMoney}元,最多${limit.maxMoney}

src/main/java/top/onefine/luckymoney/LimitConfig.java@Component@ConfigurationProperties註解

package top.onefine.luckymoney;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
@ConfigurationProperties(prefix = "limit")
public class LimitConfig {
    private BigDecimal minMoney;
    private BigDecimal maxMoney;
    private String description;

    public BigDecimal getMinMoney() {
        return minMoney;
    }

    public void setMinMoney(BigDecimal minMoney) {
        this.minMoney = minMoney;
    }

    public BigDecimal getMaxMoney() {
        return maxMoney;
    }

    public void setMaxMoney(BigDecimal maxMoney) {
        this.maxMoney = maxMoney;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

src/main/java/top/onefine/luckymoney/HelloController.java

package top.onefine.luckymoney;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private LimitConfig limitConfig;

    @GetMapping("/hello")
    public String say() {
        return "開啓SpringBoot! 金額:" + limitConfig.getMinMoney() + ", 說明:" + limitConfig.getDescription();
    }
}

默認啓用的是生產環境的配置,服務器端打包方式運行時啓用開發環境的配置:java -jar -Dspring.profiles.active=dev target/luckymoney-0.0.1-SNAPSHOP.jar

在這裏插入圖片描述

三、Controller的使用

在這裏插入圖片描述

1. Controller

maven添加依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2. src/main/resources/templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>SpringBoot入門</h1>

</body>
</html>

3. src/main/java/top/onefine/luckymoney/HelloController.java

返回html頁面(不常用)

package top.onefine.luckymoney;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.math.BigDecimal;

@Controller
public class HelloController {

    @Autowired
    private LimitConfig limitConfig;

    @GetMapping("/hello")
    public String say() {
        return "index";
    }
}

或者返回String:

package top.onefine.luckymoney;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class HelloController {


    @Autowired
    private LimitConfig limitConfig;

    @GetMapping("/hello")
    public String say() {
        return "開啓SpringBoot! 金額:" + limitConfig.getMinMoney() + ", 說明:" + limitConfig.getDescription();
    }
}

注意:

@RestController  // @RestController = @Controller + @ResponseBody
//@Controller
//@ResponseBody

4.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章