SpringBoot運行原理

1吨枉、查看當前項目已啟用和未啟用自動配置

在application.properties中設置屬性

debug=true
2、自動配置滿足兩個條件

①配置參數(shù)
②配置Bean

3咸这、自己寫一個自動配置

①新建一個Maven項目


image

②pom.xml

<?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.example.springboot</groupId>
  <artifactId>09-springboot-autoconfig</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>09-springboot-autoconfig</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
      <version>2.1.3.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

③屬性配置

package com.example.springboot;

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

@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties1 {
    private static final String MSG = "world";
    private String msg = MSG;

    public String getMsg() {
        return msg;
    }

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

④判斷依據(jù)類

package com.example.springboot;

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.example.springboot;

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;

@Configuration
@EnableConfigurationProperties(HelloServiceProperties1.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello",value = "enabled",matchIfMissing = true)
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloServiceProperties1 helloServiceProperties;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        HelloService helloService = new HelloService() ;
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }
}

⑥自動配置
在src/main/resources新建META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.springboot.HelloServiceAutoConfiguration

若有多個配置,用“,”隔開
⑦新建springboot項目忆矛,在pom.xml添加依賴

<dependency>
            <groupId>com.example.springboot</groupId>
            <artifactId>09-springboot-autoconfig</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

運行代碼

@Autowired
    HelloService helloService;

    @RequestMapping("/index1")
    public  String index1() {
        return helloService.sayHello();
    }
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子催训,更是在濱河造成了極大的恐慌洽议,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件漫拭,死亡現(xiàn)場離奇詭異亚兄,居然都是意外死亡,警方通過查閱死者的電腦和手機采驻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門审胚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人礼旅,你說我怎么就攤上這事膳叨。” “怎么了痘系?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵菲嘴,是天一觀的道長。 經(jīng)常有香客問我碎浇,道長临谱,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任奴璃,我火速辦了婚禮悉默,結果婚禮上,老公的妹妹穿的比我還像新娘苟穆。我一直安慰自己抄课,他們只是感情好,可當我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布雳旅。 她就那樣靜靜地躺著跟磨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪攒盈。 梳的紋絲不亂的頭發(fā)上抵拘,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天,我揣著相機與錄音型豁,去河邊找鬼僵蛛。 笑死,一個胖子當著我的面吹牛迎变,可吹牛的內容都是我干的充尉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼衣形,長吁一口氣:“原來是場噩夢啊……” “哼驼侠!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤倒源,失蹤者是張志新(化名)和其女友劉穎苛预,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體相速,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡碟渺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了突诬。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片苫拍。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖旺隙,靈堂內的尸體忽然破棺而出绒极,到底是詐尸還是另有隱情,我是刑警寧澤蔬捷,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布垄提,位于F島的核電站,受9級特大地震影響周拐,放射性物質發(fā)生泄漏铡俐。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一妥粟、第九天 我趴在偏房一處隱蔽的房頂上張望审丘。 院中可真熱鬧,春花似錦勾给、人聲如沸滩报。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽脓钾。三九已至,卻和暖如春桩警,著一層夾襖步出監(jiān)牢的瞬間可训,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工捶枢, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留握截,地道東北人。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓柱蟀,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蚜厉。 傳聞我的和親對象是個殘疾皇子长已,可洞房花燭夜當晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內容