SpringBoot入門(簡單詳細教程)

Spring Boot 簡介

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

微服務

  martin fowler;微服務:架構風格(服務微化);一個應用應該是一組小型服務;能夠經過HTTP的方式進行互通;單體應用:ALL IN ONE;微服務:每個功能元素最終都是一個可獨立替換和獨立升級的軟件單元;詳細參照微服務文檔html

環境準備

環境約束
–jdk1.8:Spring Boot 推薦jdk1.7及以上;java version "1.8.0_112"
–maven3.x:maven 3.3以上版本;Apache Maven 3.3.9
–IntelliJIDEA2017:IntelliJ IDEA 2017.2.2 x6四、STS
–SpringBoot 1.5.9.RELEASE:1.5.9;
統一環境;java

1.MAVEN設置;
給maven 的settings.xml配置文件的profiles標籤添加web

<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>
二、IDEA設置
整合maven進來;
spring

 

 

 

 

 

 使用SpringBoot建立一個HellWorld應用

建立項目:springboot

 

 

 

 

 

 

 

 

 導入springboot相關依賴:架構

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

建立一個HelloWorldMainApplication類:app

package com.wwy;

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

/**
 * @SpringBootApplication 來標註一個主程序類,說明這是一個Spring Boot應用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        // Spring應用啓動起來
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

建立controller類:框架

 

 

 

package com.wwy.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 World!";
    }
}

經過main方法運行項目:maven

 

 

訪問http://localhost:8080/hello

 

 

 簡化部署

<!-- 這個插件,能夠將應用打包成一個可執行的jar包  -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

生成jar包:

 

 

 

將jar包複製到桌面(或者別的地方,看我的習慣)

cmd進入jar包所在的目錄並執行jar包:

 

 

訪問http://localhost:8080/hello

 

 

 原理解析

1.pom文件

父項目:

<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應用裏面的全部依賴版本;

啓動器:

<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.主程序類,主入口類

package com.wwy;

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

/**
 * @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;

使用Spring Initializer快速建立Spring Boot項目

一、IDEA:使用 Spring Initializer快速建立項目IDE都支持使用Spring的項目建立嚮導快速建立一個Spring Boot項目;選擇咱們須要的模塊;嚮導會聯網建立Spring Boot項目;默認生成的Spring Boot項目;主程序已經生成好了,咱們只須要咱們本身的邏輯resources文件夾中目錄結構static:保存全部的靜態資源; js css images;templates:保存全部的模板頁面;(Spring Boot默認jar包使用嵌入式的Tomcat,默認不支持JSP頁面);可使用模板引擎(freemarker、thymeleaf);application.properties:Spring Boot應用的配置文件;能夠修改一些默認設置;二、STS使用 Spring Starter Project快速建立項目

相關文章
相關標籤/搜索