1. 程式人生 > >SpringBoot 初體驗

SpringBoot 初體驗

1、Spring Boot 簡介

簡化Spring應用開發的一個框架

整個Spring技術棧的一個大整合

J2EE開發的一站式解決方案

2、微服務

2014, martin fowler

微服務:架構服務 (服務微化)

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

單體應用:ALL IN ONE

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

詳細參照微服務文件

3、環境準備

環境約束

–jdk1.8:Spring Boot 推薦jdk1.7及以上;java version "1.8.0_112"

–maven3.x:maven 3.3以上版本;Apache Maven 3.3.9

-Eclipse:Kepler Service Release 1

–SpringBoot 1.5.9.RELEASE:1.5.9;

統一環境;

1.Maven設定

(對於eclipse開普勒版本設定了1.8 可能生效不了 此處沒多大影響 就是用不了1.8新特性而已)

給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>

2.Eclipse設定

4、Spring Boot HelloWorld

一個功能:

瀏覽器傳送hello請求,伺服器接受請求並處理,響應Hello World字串;

1、建立一個Maven工程;(jar)

2、匯入spring boot相關的依賴

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

3、編寫一個主程式;啟動Spring Boot應用aa


/**
 *  @SpringBootApplication 來標註一個主程式類,說明這是一個Spring Boot應用
 */
@SpringBootApplication
public class HelloWorldMainApplication {

    public static void main(String[] args) {

        // Spring應用啟動起來
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

4、編寫相關的Controller、Service

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!";
    }
}

5、執行主程式測試

6、簡化部署

 <!-- 這個外掛,可以將應用打包成一個可執行的jar包;-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

將這個應用打成jar包,直接使用java -jar的命令進行執行;

5、Hello World探究

1、POM檔案

1、父專案

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

他的父專案是
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>1.5.9.RELEASE</version>
  <relativePath>../../spring-boot-dependencies</relativePath>
</parent>
他來真正管理Spring Boot應用裡面的所有依賴版本;

Spring Boot的版本仲裁中心;

以後我們匯入依賴預設是不需要寫版本;(沒有在dependencies裡面管理的依賴自然需要宣告版本號)

2、啟動器

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

spring-boot-starter-==web==:

​ spring-boot-starter:spring-boot場景啟動器;幫我們匯入了web模組正常執行所依賴的元件;

Spring Boot將所有的功能場景都抽取出來,做成一個個的starters(啟動器),只需要在專案裡面引入這些starter相關場景的所有依賴都會匯入進來。要用什麼功能就匯入什麼場景的啟動器

2、主程式類,主入口類

/**
 *  @SpringBootApplication 來標註一個主程式類,說明這是一個Spring Boot應用
 */
@SpringBootApplication
public class HelloWorldMainApplication {

    public static void main(String[] args) {

        // Spring應用啟動起來
        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開啟自動配置功能;這樣自動配置才能生效;

@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

​ @AutoConfigurationPackage:自動配置包

​ @Import(AutoConfigurationPackages.Registrar.class):

​ Spring的底層註解@Import,給容器中匯入一個元件;匯入的元件由AutoConfigurationPackages.Registrar.class;

​ 將主配置類(@SpringBootApplication標註的類)的所在包及下面所有子包裡面的所有元件掃描到Spring容器;

​ @Import(EnableAutoConfigurationImportSelector.class);

​ 給容器中匯入元件?

EnableAutoConfigurationImportSelector:匯入哪些元件的選擇器;

​ 將所有需要匯入的元件以全類名的方式返回;這些元件就會被新增到容器中;

​ 會給容器中匯入非常多的自動配置類(xxxAutoConfiguration);就是給容器中匯入這個場景需要的所有元件,並配置好這些元件;

有了自動配置類,免去了我們手動編寫配置注入功能元件等的工作;

​ SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);

Spring Boot在啟動的時候從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值,將這些值作為自動配置類匯入到容器中,自動配置類就生效,幫我們進行自動配置工作;==以前我們需要自己配置的東西,自動配置類都幫我們;

J2EE的整體整合解決方案和自動配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar;