spring boot

一  Spring Boot的一些概念java

      1.  Spring Boot是什麼?mysql

                    它是一種以更簡單的方式來使用Spring生態下全部技術的框架,用於簡化Spring項目的web

                 構建、開發、配置、部署與jar報引用。簡而言之,咱們能夠說Spring Boot只是Spring自己ajax

                 的擴展,使開發,測試和部署更加方便。這樣也就明白了SpringBoot和Spring Cloud的區別。spring

                 也有的地方是這樣講的:Spring boot 是 Spring 的一套快速配置腳手架(Spring Boot,看名sql

                 字就知道是Spring的引導)數據庫

         2.  Spring Boot與Spring、Spring Boot與Spring Cloud的區別?json

                 (1)Spriing Boot和Spring   tomcat

                                 SpringBoot是Spring的擴展,它解決了Spring的三大痛點問題。  架構

                                 Spring的三大痛點問題(以啓動一個帶Hibernate的Spring MVC爲例):

                                 --  依賴太多了,並且要注意版本兼容。這個應用,要添加10-20個依賴,Spring相

                                     關的包10多個,而後是Hibernate包,Spring與Hibernate整合包,日誌包,json

                                     包一堆,並且要注意版本兼容性。

                                 --  配置太多了,要配置註解驅動,要配置數據庫鏈接池,要配置Hibernate,要配

                                     置事務管理器,要配置Spring MVC的資源映射,要在web.xml中配置啓動Spring

                                     和pring MVC等

                                 --  部署和運行麻煩。要部署到tomcat裏面。不能直接用java命令運行。太多重複和

                                     你們都同樣的配置了。

                                 Spring Boot正好解決了上述問題,其哲學思想是約定大於配置。既然不少東西都是

                                 同樣的,爲何還要去配置。

                                 --  經過starter(starter是啓動依賴機制,start是指在pom中導入少許依賴缺能夠在

                                      工程中自動引入大量依賴)和依賴管理解決依賴問題。

                                 --  經過自動配置,解決配置複雜問題。

                                 --  經過內嵌web容器,由應用啓動tomcat,而不是tomcat啓動應用,來解決部署運

                                     行問題

                                 Spring Boot和Spring的之間的詳細關係能夠查看文章:

                                  https://www.jianshu.com/p/ffe5ebe17c3a                 

 

                                 Spring Boot和Spring Cloud的區別:

                                 --  Spring Cloud是微服務框架,微服務體系開發中的架構問題,提供了一整套的解

                                     決方案;

                                 --  Spring Boot是Spring框架的擴展能夠基於spring boot 快速開發單個微服務。它是

                                      爲了解決Spring中的一些痛點而產生的

         3.  Spring Boot工程目錄介紹

                   

 

                    別的不說,都會,這裏只介紹static和templates

                    static:存放靜態資源文件

                    templates:存放動態資源文件

                           

          2.Spring Boot的四大核心機制

            (1)起步依賴機制

                      經過起步依賴機制(starter),簡化jar包的引用,解決jar版本衝突問

                             SpringBoot起步依賴機制主要用來簡化jar包的依賴,並經過大量的測試,解決了不一樣

                      jar之間的版本衝突問題。使得開發人員只須要關注代碼的實現,而沒必要在jar包依賴上花費

                      過多心思。

                             Spring Boot經過啓動依賴機制爲咱們引入了大量的依賴,若是咱們不須要某個jar包,

                      咱們能夠經過<exclusion></exclusion>進行屏蔽

在Maven中,可使用<exclusions>:
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>*</artifactId>
    </exclusion>
  </exclusions>
</dependency>

                      自定義要使用jar包的版本,而不使用啓動依賴機制中的jar包

 

 

            (2)自動配置

                            自動配置是指能夠實現簡單配置,甚至是零配置(好比我搭建一個Spring Boot項目,

                     什麼都不配,就能夠啓動,這就是零配置),就能搭建整套框架,它儘可能減小開發人員對

                     配置的編寫。

                            注:並不表明Spring Boot能夠零配置哈。

                    這裏補充一些知識:

                  (a)Spring 獲取bean的幾種方式

                           --  讀取xml文件的方式,這種在初學入門的時候比較適用

             ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:tt.xml");

          ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:tt.xml");

                           --  繼承spring的Aware類,覆蓋方法實現獲取上下文,從上下文中獲取。

                          *  繼承自抽象類ApplicationObjectSupport

                                  抽象類ApplicationObjectSupport提供getApplicationContext()方法,能夠方便的獲

                                  取到ApplicationContext。Spring初始化時,會經過該抽象類的

                                  setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象

                                  注入。

                               *  繼承自抽象類WebApplicationObjectSupport

                                   相似上面方法,調用getWebApplicationContext()獲取WebApplicationContext

                               *  實現接口ApplicationContextAware

                     當一個類實現了這個接口以後,這個類就能夠方便地得到 ApplicationContext 中的

                                  全部bean。換句話說,就是這個類能夠直接獲取Spring配置文件中,全部有引用到

                                  的bean對象。

                                  使用例子:

                                         第一步,定義一個工具類,實現 ApplicationContextAware,實現

                                                       setApplicationContext方法

 
 

                   public class SpringContextUtils implements ApplicationContextAware {

 
 

                   private static ApplicationContext context;

 
 


                   @Override

 
 

                   public void setApplicationContext(ApplicationContext context)

 
 

                   throws BeansException {

 
 

                          SpringContextUtils.context = context;

 
 

                   }

 
 

                   public static ApplicationContext getContext(){

 
 

                          return context;

 
 

                  }
              }

 

 

                                         

                                                       如此一來,咱們就能夠經過該工具類,來得到 ApplicationContext,進

                                                       而使用其getBean方法來獲取咱們須要的bean。

                                         第二步,在Spring配置文件中註冊該工具類

                                                        之因此咱們能如此方便地使用該工具類來獲取,正是由於Spring可以

                                                        爲咱們自動地執行 setApplicationContext 方法,顯然,這也是由於

                                                        IOC的緣故,因此必然這個工具類也是須要在Spring的配置文件中進行

                                                        配置的。

                         <!--Spring中bean獲取的工具類-->                         <bean id="springContextUtils" class="com.zker.common.util.SpringContextUtils" />

 

                                         第三步,編寫方法進行使用

                                                       一切就緒,咱們就能夠在須要使用的地方調用該方法來獲取bean了。

   /**

     * 利用Ajax實現註冊的用戶名重複性校驗

     * @return

     */

    public String ajaxRegister() throws IOException {

        UserDao userDao = (UserDao)SpringContextUtils.getContext().getBean("userDao");

        if (userDao.findAdminByLoginName(loginName) != null

                || userDao.findUserByLoginName(loginName) != null) {

            message.setMsg("用戶名已存在");

            message.setStatus(false);

        } else {

            message.setMsg("用戶名能夠註冊");

            message.setStatus(true);

        }


        return "register";

    }

 

                           --  藉助於spring提供的工具類   

                               這種方式適合於採用Spring框架的B/S系統,經過ServletContext對象獲取

                               ApplicationContext對象,而後在經過它獲取須要的類實例。 下面兩個工具方式的區別

                               是,前者在獲取失敗時拋出異常,後者返回null。

import org.springframework.web.context.support.WebApplicationContextUtils; 
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc) 
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc) 
ac1.getBean("beanId"); 
ac2.getBean("beanId"); 

 

                  (b)一些註解                    

                           --  @Configuration用於定義配置類,可替換xml配置文件,被註解的類內部包含有一個

                               或多個被@Bean註解的方法,這些方法將會被AnnotationConfigApplicationContext或

                               AnnotationConfigWebApplicationContext類進行掃描,並用於構建bean定義,初始化

                               Spring容器。@Configuration可理解爲用spring的時候xml裏面的<beans>標籤;

                               @Bean可理解爲用spring的時候xml裏面的<bean>標籤

                               AnnotationConfigWebApplicationContext:使用AnnotationConfigApplicationContext

                              能夠實現基於Java的配置類加載Spring的應用上下文。避免使用application.xml進行配

                              置。位於spring-context.jar;AnnotationConfigEmbeddedWebApplicationContext是

                              spring boot中的WebApplicationContext。

                              WebApplicationContext

                                            WebApplicationContext是專門爲web應用準備的,他容許從相對於web根目錄

                                     的路勁中裝載配置文件完成初始化工做,從WebApplicationContext中能夠得到

                                     ServletContext的引用,整個Web應用上下文對象將做爲屬性放置在ServletContext

                                     中,以便web應用能夠訪問spring上下文,spring中提供WebApplicationContextUtils

                                     的。

                                            在之前的spring項目中,會有兩個WebApplicationContext,一個是parent,從

                                     applicationContext.xml里加載的,一個是child,從servlet-context.xml里加載的。兩

                                     者是繼承關係,child WebApplicationContext 能夠經過getParent()函數獲取到

                                     root WebApplicationContext。也就是說,咱們經常經過listener初始化parent,並

                                     component-scan掃描非controller類。而經過servlet初始化child,只掃描controller類。

                                     這樣便於在child可注入parent中的bean,反之就不行(本身感受這就是父子容器)。

                           --  @ConfigurationProperties("xxx") 註解

                                      加上該註解後,就會注入在application.properties中xxx開頭的屬性。

                                      示例:

                                       Person.java

@Component
@ConfigurationProperties(prefix="person")
public class Person {

    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}

 

                                       application.properties

person.name=Five
person.age=20

 

 

                                       此時Person.java中的name和age的值分別被注入Five和age

                                       測試Controller     

package ai.yunxi.springBootDemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private JdbcTemplate template;
    
    @Autowired
    private Person person;
    
    @RequestMapping("/sayHello")
    public String sayHello(){
        System.out.println("template:"+template);
        return "Hello "+person.getName()+"!";
    }
}

 


                           --  @ConditionalOnClass:聽說後面會講到

@ConditionalOnBean(僅僅在當前上下文中存在某個對象時,纔會實例化一個Bean)
@ConditionalOnClass(某個class位於類路徑上,纔會實例化一個Bean)
@ConditionalOnExpression(當表達式爲true的時候,纔會實例化一個Bean)
@ConditionalOnMissingBean(僅僅在當前上下文中不存在某個對象時,纔會實例化一個Bean)
@ConditionalOnMissingClass(某個class類路徑上不存在的時候,纔會實例化一個Bean)
@ConditionalOnNotWebApplication(不是web應用)

@RestController 定義一個Controller爲RestController,RestController響應的是字符串而不是視圖

 

            (3)Spring Boot CLI

                     一種命令行工具。

                     --  能夠構建SpringBoot項目,其實仍是經過https://start.spring.io/去構建工程的

                         例:spring init --dependencies=web,mysql,jdbc bootProject

                     --  能夠執行Groovy腳本(spring run命令)

                         例:spring run hello.groovy

                     --  例:spring jar bootProject.jar *.groovy

            (4)Actuator

                            Spring Boot Actuator的關鍵特性是在應用程序裏提供衆多Web端點,經過

                     它們瞭解應用程序運行時的內部情況。能夠知道Bean在Spring應用程序上下文

                     裏是如何組裝在一塊兒的,掌握應用程序能夠獲取的環境屬性信息等。其實說白

                     了就是一些端點。

                  (a)開啓Actuator須要的一些配置

                           依賴:

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

 

                           端點相關的配置

      #設置端點路徑
      management.endpoints.web.base-path=/
       #暴露端點
      management.endpoints.web.exposure.include=*
      #隱藏端點
      management.endpoints.web.exposure.exclude=env,metrics

 

 

                  (b)要熟悉的一些端點(標紅的)

                           GET /configprops 描述配置屬性(包含默認值)如何注入Bean

                           GET /beans 描述應用程序上下文裏所有的Bean,以及它們的關係

                           GET /env 獲取所有環境屬性

                           GET /env/{name} 根據名稱獲取特定的環境屬性值

                           GET /health 報告應用程序的健康指標,這些值由HealthIndicator的實現類提供

                           GET /info 獲取應用程序指定發佈的信息,這些信息由info打頭的屬性提供

                           GET /mappings 描述所有的URI路徑,以及它們和控制器(包含Actuator端點)的映射關係

                           GET /metrics 報告各類應用程序度量信息,好比內存用量和HTTP請求計數

                           GET /httptrace 提供基本的HTTP請求跟蹤信息(時間戳、 HTTP頭等)

                           GET /threaddump 獲取線程活動的快照

                           GET /conditions 提供了一份自動配置報告,記錄哪些自動配置條件經過了,哪些沒經過

                           POST /shutdown 關閉應用程序,要求management.endpoint.shutdown.enabled設置爲true

二  Spring Boot框架構建的四種方式

          1.  Spring Tool Suite

          2.  SpringBoot CLI

          3.  Spring Initializr

          4.  手動引用jar包

三  Spring Boot熱部署

                 熱部署就是咱們對一些Java文件、Jsp文件、配置文件等進行了一些修改以後,項目來幫咱們自動

          重啓、從新加載的過程,不須要每次修改本身都須要手動進行重啓。

          1.  實現熱部署只須要添加以下依賴便可

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>  <!--取消傳遞依賴-->
</dependency>

 

          2.  其它一些重要配置       

       #熱部署生效
    spring.devtools.restart.enabled: true
    #設置重啓的目錄,能夠是任意位置的文件夾(工程內和工程外,其中的文件或文件內容發生改變則重啓服務)
     spring.devtools.restart.additional-paths: src/main/java
    #修改默認不重啓目錄(/META-INF/maven、/META-INF/resources、/resources、/static、/templates、/public)
    spring.devtools.restart.exclude: WEB-INF/**
    #添加額外不重啓目錄
    spring.devtools.restart.additional-paths

四  Spring Boot與MyBatis集成

          不作筆記了看視頻「第07節-MyBatis集成.vep」吧 總共才9分鐘

五  Spring Boot與Security集成

          看視頻

四  Spring Boot與MyBatis集成

          看視頻

相關文章
相關標籤/搜索