springBoot--01--快速入門

1.1 原有Spring優缺點分析

1.1.1 Spring的優點分析

Spring是Java企業版(Java Enterprise Edition,JEE,也稱J2EE)的輕量級代替品。無需開發重量級的Enterprise JavaBean(EJB),Spring爲企業級Java開發提供了一種相對簡單的方法,通過依賴注入和麪向切面編程,用簡單的Java對象(Plain Old Java Object,POJO)實現了EJB的功能。

1.1.2 Spring的缺點分析

雖然Spring的組件代碼是輕量級的,但它的配置卻是重量級的。一開始,Spring用XML配置,而且是很多XML配置。Spring 2.5引入了基於註解的組件掃描,這消除了大量針對應用程序自身組件的顯式XML配置。Spring 3.0引入了基於Java的配置,這是一種類型安全的可重構配置方式,可以代替XML。
所有這些配置都代表了開發時的損耗。因爲在思考Spring特性配置和解決業務問題之間需要進行思維切換,所以編寫配置擠佔了編寫應用程序邏輯的時間。和所有框架一樣,Spring實用,但與此同時它要求的回報也不少。
除此之外,項目的依賴管理也是一件耗時耗力的事情。在環境搭建時,需要分析要導入哪些庫的座標,而且還需要分析導入與之有依賴關係的其他庫的座標,一旦選錯了依賴的版本,隨之而來的不兼容問題就會嚴重阻礙項目的開發進度。

1.2 SpringBoot的概述

1.2.1 SpringBoot解決上述Spring的缺點

SpringBoot對上述Spring的缺點進行的改善和優化,基於約定優於配置的思想(自動配置),可以讓開發人員不必在配置與邏輯業務之間進行思維的切換,全身心的投入到邏輯業務的代碼編寫中,從而大大提高了開發的效率,一定程度上縮短了項目週期。

1.2.2 SpringBoot的特點

爲基於Spring的開發提供更快的入門體驗(SpringBoot環境搭建相比Spring更爲簡單)
開箱即用,沒有代碼生成,也無需XML配置。同時也可以修改默認值來滿足特定的需求
提供了一些大型項目中常見的非功能性特性,如嵌入式服務器、安全、指標,健康檢測、外部配置等
SpringBoot不是對Spring功能上的增強,而是提供了一種快速使用Spring的方式

1.2.3 SpringBoot的核心功能

起步依賴(start座標)
起步依賴本質上是一個Maven項目對象模型(Project Object Model,POM),定義了對其他庫的傳遞依賴,這些東西加在一起即支持某項功能。
簡單的說,起步依賴就是將具備某種功能的座標打包到一起,並提供一些默認的功能。
自動配置
Spring Boot的自動配置是一個運行時(更準確地說,是應用程序啓動時)的過程,考慮了衆多因素,才決定Spring配置應該用哪個,不該用哪個。該過程是Spring自動完成的。

二、SpringBoot快速入門

2.1 代碼實現

2.1.1 創建Maven工程

使用idea工具創建一個maven工程,該工程爲普通的java工程即可

 

 

 

 

 

2.1.2 添加SpringBoot的起步依賴

SpringBoot要求,項目要繼承SpringBoot的起步依賴spring-boot-starter-parent

 

<!-- 所有的SpringBoot工程都必須繼承  spring-boot-starter-parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>   

SpringBoot要集成SpringMVC進行Controller的開發,所以項目要導入web的啓動依賴

 

 <dependencies>
        <!--  web功能起步依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--熱部署配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>   

2.1.3 編寫SpringBoot引導類

要通過SpringBoot提供的引導類起步SpringBoot纔可以進行訪問

 

package xyz.ytfs;

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

/**
 * @author by 雨聽風說
 * @Classname MySpringBootApplication
 * @Description TODO(SpringBoot的引導類)
 * @Date 2020/5/12 1:27
 */

//申明該類是一個springboot的啓動類

@SpringBootApplication
public class MySpringBootApplication {

    /**
     * main 是java程序的入口
     * @param args
     */
    public static void main(String[] args) {
        //run方法  表示運行springBoot的引導類   run參數就是springBoot引導類的字節碼對象

        SpringApplication.run(MySpringBootApplication.class);

    }

}   

2.1.4 編寫Controller

在引導類MySpringBootApplication同級包或者子級包中創建QuickStartController

/**
 * @author by 雨聽風說
 * @Classname QuickController
 * @Description TODO(快速入門的controller)
 * @Date 2020/5/12 1:32
 */

@Controller
public class QuickController {


    @RequestMapping("quick")
    @ResponseBody
    public String quickStart(){

        return "hellow world";

    }


}
  

2.1.5 測試

執行SpringBoot起步類的主方法,控制檯打印日誌如下

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

2020-05-12 21:00:02.770  INFO 10340 --- [  restartedMain] xyz.ytfs.MySpringBootApplication         : Starting MySpringBootApplication on 一一 with PID 10340 (started by DTX in F:\project folder\winter vacation\springBoot)
2020-05-12 21:00:02.773  INFO 10340 --- [  restartedMain] xyz.ytfs.MySpringBootApplication         : No active profile set, falling back to default profiles: default
2020-05-12 21:00:02.918  INFO 10340 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-05-12 21:00:02.918  INFO 10340 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-05-12 21:00:04.144  INFO 10340 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-05-12 21:00:04.164  INFO 10340 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-05-12 21:00:04.164  INFO 10340 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-05-12 21:00:04.242  INFO 10340 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-05-12 21:00:04.242  INFO 10340 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1323 ms
2020-05-12 21:00:04.421  INFO 10340 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-12 21:00:04.587  INFO 10340 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-05-12 21:00:04.624  INFO 10340 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-12 21:00:04.627  INFO 10340 --- [  restartedMain] xyz.ytfs.MySpringBootApplication         : Started MySpringBootApplication in 2.304 seconds (JVM running for 4.797)
  

通過日誌發現,Tomcat started on port(s): 8080 (http) with context path ''

tomcat已經起步,端口監聽8080,web應用的虛擬工程名稱爲空

打開瀏覽器訪問url地址爲:http://localhost:8080/quick

 

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