spring boot 核心

基本配置

@SpringBootApplication包含這三個註解:@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScanhtml

@EnableAutoConfiguration:讓spring boot根據當前項目的jar包依賴爲當前項目自動配置java

例如添加了spring-boot-starter-web的依賴,那麼就會自動添加tomcat和spring mvc的依賴web

springboot會自動掃描同級以及同級包下面的bean對象,建議入口類的放置位置在groupid和artifcatid組合包下面。spring

若是不須要掃描的包能夠經過exclude排除掉。apache

定製banner

在src/main/resource 下面放置一個spanner.txt,而後去http://patorjk.com/software/taag/ 把生成banner,將生成的文字copy到banner.txt就能夠修改spring boot啓動的banner了tomcat

若是不喜歡啓動的時候顯示banner,也能夠將啓動的banner關閉,經過相似下面形式啓動安全

SpringApplication app  = new SpringApplication(DemoApplication.class);
		app.setBannerMode(Banner.Mode.OFF);
		app.run(args);

starter pom

spring boot 爲咱們提供了絕大多數的starter pom,只要使用了starter pom 相關的技術配置就會消除,就能夠獲得spring boot爲咱們提供的自動配置bean
spring boot 官方薦的starter pom 以及第三方的 pom配置參考
https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#using-boot-starterspringboot

使用xml

若是項目中實在要用到xml,則能夠經過相似下面的方式引入session

@ImportResource("classpath:context.xml")

常規屬性配置

若是要用到properties裏面的配置則能夠經過配置註解@ImportRource 應用properties文件,而後經過@Value註解直接在代碼裏面使用properties裏面的值
若是是application.properties裏面的值的話,那麼能夠不須要配置@ImportRource註解,直接使用@Value註解引用properties裏面的值就能夠了mvc

類型安全的配置

咱們能夠經過@ConfigurationProperties 註解和一個bean 對象綁定,直接將properties裏面的對象綁定到bean裏面
例如 application.properties裏買定義內容

author.age=12
author.name=wahaha

而後在類裏面直接定義bean生成對象,對象裏面的值就包含properties裏面的值

@ConfigurationProperties(prefix="author")
@Data
@Component
public class Author {
    private String name;
    private Integer age;

}

這樣,獲取到的bean author對象的屬性就有值了。

spring boot的自動裝備功能是經過spring-boot-autoconfigure實現的。

經過在application.properties裏面設置下面的屬性,就能夠看到哪些組建被啓動了,哪些沒有被啓動

debug=true

spring boot運行原理

若是咱們在pom文件加入了對應的starter,那麼spring就有了對應的功能,這個是怎麼實現的呢?例如,咱們在pom文件中加入了spring-boot-starter-web ,系統就自動識別除了,這個是一個web項目,這個是怎麼實現的,下面咱們以spring-boot-starter-web 來看看原理

當咱們啓用@SpringBootApplication註解,那麼就會啓用@EnableAutoConfiguration註解,@EnableAutoConfiguration註解裏面倒入了這個類:AutoConfigurationImportSelector

這個類裏面會經過SpringFactoriesLoader.loadFactoryNames方法去掃描META-INF/spring.factories的jar包(這個文件裏面聲明瞭有哪些自動配置)

那麼如何判斷項目須要啓動web組建做爲web項目呢

  • 確認org.springframework.web.context.support.GenericWebApplicationContext是否在類路徑裏面

  • 容器裏面是否有session的scope

  • 當前容器的environment是否有StandardServletEnvironment

  • 當前的ResourceLoader是否爲WebApplicationContext(ResourceLoader是 - ApplicationContext的頂級接口之一)

  • 咱們須要構造ConditionOutcome類的對象來幫助咱們,最終經過ConditionOutcome.isMatch方法返回的布爾值來肯定條件。

編寫本身的starter pom

項目結構

在這裏插入圖片描述

pom文件配置

<?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>com.alipay</groupId>
  <artifactId>spring-boot-starter-hello</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>spring-boot-starter-hello</name>

  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <!-- 須要spring boot 自身自動配置做爲依賴 -->
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-test-autoconfigure</artifactId>
      <version>2.1.5.RELEASE</version>
    </dependency>
  </dependencies>

</project>

屬性配置

package com.alipay;

/** * 此類做爲判斷依據類,若是這個類存在那麼就能夠啓動這個starter * @author bijia * @version $Id: HelloService.java, v 0.1 2019年06月01日 11:47 PM bijia Exp $ */
public class HelloService {

    private String msg;

    public String sayHello() {
        return "Hello " + msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

自動配置類

package com.alipay;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/** * 自動配置類 * @author bijia * @version $Id: HelloAutoConfiguration.java, v 0.1 2019年06月01日 11:47 PM bijia Exp $ */
@Configuration //標識此類爲一個spring配置類
@EnableConfigurationProperties(value = HelloServiceProperteis.class) //啓動配置文件,value用來指定咱們要啓用的配置類,能夠有多個,多個時咱們能夠這麼寫value={xxProperties1.class,xxProperteis2.class....}
@ConditionalOnClass(HelloService.class) //表示當classPath下存在HelloService.class文件時改配置文件類纔有效
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true) //表示只有咱們的配置文件是否配置了以hello爲前綴的資源項值,而且在該資源項值爲enable,若是沒有配置咱們默認設置爲enable
public class HelloAutoConfiguration {


    @Autowired
    private HelloServiceProperteis helloServiceProperteis;


    // 下面的意思是當容器沒有這個bean 的時候,會新建這個bean
    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperteis.getMsg());
        return helloService;
    }
}

註冊配置

在resources 的META-INF下面新增spring.factories文件,而後配置下面內容

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.alipay.HelloAutoConfiguration

以上spring-boot-starter-hello下載地址:https://download.csdn.net/download/fighterandknight/11221600

新加maven測試starter

新建一個spring boot web project,而後引入上面創建的jar,並在application.properties裏面設置hello.msg的值

debug=true
hello.msg=bijia.whx

而後編寫下面代碼

import com.alipay.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication {

	@Autowired
	private HelloService helloService;

	@RequestMapping("/")
	public String index() {
		return helloService.sayHello();
	}

	public static void main(String[] args) {
		ConfigurableApplicationContext context=SpringApplication.run(DemoApplication.class, args);
	}

}

效果展現

在這裏插入圖片描述