微服務架構Day03-SpringBoot之web開發配置

攻城獅Chova發表於2021-02-13

概述

  • SpringBoot開發:
    1.建立SpringBoot應用,選中需要的場景模組。
    2.SpringBoot已經預設將場景模組配置好,只需要在配置檔案中指定少量的配置(資料庫地址,使用者名稱,密碼)就可以執行起來。
    3.只需要編寫業務邏輯程式碼。
  • 需要掌握自動配置原理:這個場景中SpringBoot預設配置好了什麼,能不能修改,能修改哪些配置,能不能擴充套件。
XxxAutoConfiguration:幫我們給容器中自動配置元件
XxxProperties:配置類,封裝配置檔案中的內容

SpringBoot對靜態資源(static-location)的對映規則


@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
  • ResourceProperties可以設定和資源有關的引數,快取時間等。

 /* 
  * ResourceHandlerRegistry儲存用於通過Spring MVC服務靜態資源的資源處理程式的註冊
  * 允許設定為在Web瀏覽器中高效載入而優化的快取頭
  * 可以在Web應用的目錄下,類路徑等位置之外的位置提供資源
  */
 public void addResourceHandlers(ResourceHandlerRegistry registry) { 
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                if (!registry.hasMappingForPattern("/webjars/**")) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

                String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                if (!registry.hasMappingForPattern(staticPathPattern)) {
                    this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                }

            }
        }
  • 所有/web.jars/**中的資源都在classpath:/META-INF/resources/webjars/中尋找。
  • web.jars:以jar包的方式引入靜態資源:https://www.webjars.org/
  • 訪問時,只需要寫web.jars下面資源的名稱。

/**:訪問當前專案的任何資源(靜態資源的資料夾)

classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/	# 當前專案的根路徑
 @Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
            return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
        }

配置歡迎頁的對映:

  • 歡迎頁:靜態資原始檔夾下的所有index.xml頁面,被 /** 對映。
        @Configuration
        @ConditionalOnProperty(
            value = {"spring.mvc.favicon.enabled"},
            matchIfMissing = true
        )

		/* 
		 * ResourceLoaderAware是一個標記介面
		 * 用於通過ApplicationContext上下文注入ResourceLoader
		 * 有setResourceLoader()方法
		 */
        public static class FaviconConfiguration implements ResourceLoaderAware {
            private final ResourceProperties resourceProperties;
            /* 
             * ResourceLoader用於返回Resource物件和ClassLoader物件
             * 		- getResource(String location)方法根據提供的location引數返回相應的Resource物件
             * 		- getClassLoader()方法則返回載入這些Resource的ClassLoader
             */
            private ResourceLoader resourceLoader;

            public FaviconConfiguration(ResourceProperties resourceProperties) {
                this.resourceProperties = resourceProperties;
            }

            public void setResourceLoader(ResourceLoader resourceLoader) {
                this.resourceLoader = resourceLoader;
            }

			/* 
			 * SimpleUrlHandlerMapping是SpringMVC中適應性最強的Handler Mapping類
			 * 允許明確指定URL模式和Handler的對映關係.有兩種宣告方式:
			 * 		- prop:
			 * 		  - key: URL模式
			 * 		  — value: Handler的ID或者名字
			 * 		- value:
			 * 		  - 等號左邊是URL模式
			 * 		  - 等號右邊是HandlerID或者名字  
			 */
            @Bean
            public SimpleUrlHandlerMapping faviconHandlerMapping() {
                SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
                mapping.setOrder(-2147483647);
                mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
                return mapping;
            }

配置喜歡的圖示(標籤的圖示):

  • 標籤圖示:所有的 **/favicon.ico 都是在靜態資料夾資源下。

模板引擎

  • jsp,velocity,freemarker,thymeleaf
優點 缺點
jsp 1. 功能強大,可以寫Java程式碼
2. 支援jsp標籤 - jsp tag
3. 支援表示式語言 - EL表示式
4. 官方標準,使用廣泛,豐富的第三方jsp標籤庫
5. 效能良好 ,jsp編譯成class檔案執行,有很好的效能表現
1. jsp沒有明顯的缺點
2. 由於可以編寫Java程式碼,使用不當容易破壞MVC結構
velocity 1. 不編寫Java程式碼,實現嚴格的MVC分離
2. 效能良好,比jsp優越
3. 使用表示式語言 - EL表示式
1. 不是官方標準
2. 使用範圍小,第三方標籤庫較少
3. 對jsp標籤的支援不夠友好
freemarker 1. 不編寫Java程式碼,實現嚴格的MVC分離
2. 效能非常好
3. 對jsp標籤支援良好
4. 內建大量常用功能,使用非常方便
5. 巨集定義(類似jsp標籤)非常方便
6. 使用表示式語言 - EL表示式
1.不是官方標準
2. 使用範圍小,第三方標籤庫較少
thymeleaf 1. 靜態html嵌入標籤屬性,瀏覽器可以直接開啟模板檔案,便於後端聯調
2. SpringBoot框架推薦模板
1.模板必須符合xml規範
2. 需要加入js指令碼
  • freemarker:
    • freemarker是一個用Java語言編寫的模板引擎,基於模板生成文字來輸出
    • freemarkerWeb容器無關,也就是說,在Web執行時,並不知道是Servlet還是HTTP
    • 不僅可以用作表現層的實現技術,而且還可以用於生成XML,JSP,Java
    • 目前企業中主要使用freemarker做靜態頁面或頁面展示
    • 選擇freemarker的原因:
      • 效能: 就效能而言 ,velocity是最好的,其次是jsp, 普通的頁面freemarker效能最差.但是在複雜頁面,比如包含大量判斷,日期金額格式化的頁面上 ,freemarker的效能比使用tageljsp
      • 巨集定義比jsp tag方便
      • 內建大量常用功能. 比如html過濾,日期金額格式化等等,使用方便
      • 支援jsp標籤
      • 可以實現嚴格等mvc分離
  • freemarker與velocity比較:
    • velocity:
      • velocity優於freemarker在於有廣泛的第三方支援以及龐大的使用者社群
      • velocity的效能是最好的
    • freemarker:
      • freemarker比velocity簡單,因為velocity必須編寫一些自定義的toolbox以及一遍一遍重複的編寫一些比較通用的模版程式碼
      • velocity的做法使得在velocity的模版中大量的與Java物件互動,違背了簡單的原則,儘管也可以將程式碼轉入控制器中實現
      • freemarker能做到,而velocity無法做到的:
        • 日期和數字的支援:
          • 比較和格式化顯示日期或者時間值
          • 執行運算和比較,對任意數量的型別,包括精度型別,而不僅僅是整數
        • 國際化:
          • 格式數字區域,各種各樣的內建和自定義數字格式模式
          • 格式日期地區和時區,各種各樣的內建和定製的日期格式模式
          • 識別符號,即變數名可以包含非英語字母一樣的重音字母,阿拉伯字母,漢字等
        • 迴圈處理:
          • 退出迴圈
          • 訪問控制變數外迴圈機構的內部迴圈
          • 得知當前是否到了迴圈的結束位置
        • 模版級別的陣列處理:
          • 使用 [i] 的語法來訪問陣列元素,包括原始的和非原始的指數
          • 獲取陣列的長度
        • 巨集定義:
          • 巨集呼叫可以通過位置或名稱進行引數傳遞
          • 巨集的引數可以設定預設值,在呼叫巨集時如果沒有指定該引數,則使用預設值代替
          • 通過 < @myMacro >body< / @myMacro > 可以支援巨集的巢狀
          • 通過文字表達的 “巨集的名稱” 來直接呼叫某個巨集
          • 巨集允許先使用再定義
          • 巨集可以定義區域性變數
        • 名稱空間:
          • 使用多個名稱空間的變數. 這個在建立 “巨集庫”
        • 內建與Java語言無關的字串,列表,Map的操作方法
        • 能提示模版中的拼寫錯誤以及其他錯誤
          • 當訪問一個不存在的變數時,freemarker在執行模版時會報錯
          • 通過配置,可以指定freemarker在發生此類錯誤時停止執行,還是忽略該錯誤,同時freemarker會在日誌中記錄此問題
          • 輸入錯誤的指令名稱,freemarker將丟擲一個異常
        • 更高階的文字輸出工具:
          • 將模版塊封裝在一組標記中,這樣可以應用HTML轉義或者XML轉義(或者freemarker表示式的其他轉換)到 ${foo} 塊中
          • freemarker具有模版塊的轉換器,會在渲染時經過轉換過濾器.內建的轉換器包括空格壓縮器,HTML和XML溢位器. 也可以實現自定義的轉換器,即如果生成Java原始碼,則可以編寫Java程式碼pretty-printer轉換並插入到模版中.同時轉換也可以巢狀
          • 使用內建的flush-directive顯式重新整理輸出寫入器
          • 使用內建的stop-directive停止渲染
        • 文字處理:
          • 支援Java的特殊字元處理,比如 \b, \t, \n, \f, \r, \ ", \ ', \ , 以及unicode的 \xXXXX
          • 除了通常的字串,數字,布林常量,也可以自定義列表和地圖文字以及內部模版
        • 高階的空格刪除:
          • freemarker將刪除一些多餘的空格,跳格,換行等字元
          • 提供相關指令來刪除多餘的空格
        • 與其他技術整合:
          • 提供JSP標籤庫以便在JSP中嵌入freemarker模版
          • 直接和Python物件一起使用
        • 更強大的XML轉換功能
        • 模版元程式:
          • 捕捉到輸出任意部分範本背景變數
          • 任意解釋的範圍變數,類似一個模版定義

thymeleaf

thymeleaf基本概念

  • thymeleaf是一個XML,XHTML,HTML5模板引擎,可用於Web與非Web應用
  • thymeleaf主要目標: 提供一個可被瀏覽器正確顯示的,格式良好的模板建立方式,可以用於靜態建模
  • 可以使用thymeleaf建立經過驗證的XML與HTML模板:
    • 相對於編寫邏輯程式碼,開發者只需將標籤屬性新增到模板中即可
    • 這些標籤就會在文件物件模型DOM上執行預先制定好的邏輯
  • thymeleaf具有良好的擴充套件性:
    • 可以使用thymeleaf自定義模板屬性集合,用來計算自定義表示式並使用自定義邏輯
    • 這樣thymeleaf可以作為模板引擎框架

引入thymeleaf依賴

  • 引入SpringBoot中的thymeleaf依賴:
		<properties>	
			<!-- 切換thymeleaf版本 -->
			<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
			<!-- 佈局功能支援程式-thymeleaf3==layout2  thymeleaf2==layout1 -->
			<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>	
		</properties>
		<dependency>	
			<!-- 引入thymeleaf依賴 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

thymeleaf使用和語法

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";	
    private String mode = "HTML";
    private Charset encoding;
  • 將html頁面放在classpath:/templates/中,thymeleaf就能自動渲染了。
  • Thymeleaf的使用
    1.匯入thymeleaf的名稱空間
<html xmlns:th="http://www.thymeleaf.org">

2.使用thymeleaf語法:

  • th:text - 改變當前元素裡面的文字內容
  • th:任意html屬性 - 改變原生屬性的值
thymeleaf jsp
片段包含 th:insert
th:replace
include
遍歷 th:each c:forEach
條件判斷 th:if
th:unless
th:switch
th:case
c:if
宣告變數 th:object
th:with
c:set
任意屬性修改 th:attr
th:attrprepend(前面)
th:attrappend(後面)
修改指定屬性預設值 th:value
th:href
th:src
修改標籤體文字內容 th:text(轉義)
th:utext(不轉義)
宣告片段 th:fragment
移除宣告片段 th:remove
  • 表示式
Simple expressions:						(表示式語法)
Variable Expressions: ${...}			(獲取變數值-OGNL)
					1.獲取物件的屬性,呼叫方法
					2.使用內建的基本物件:
						#ctx : the context object.
						#vars: the context variables.
						#locale : the context locale.
						#request : (only in Web Contexts) the HttpServletRequest object.
						#response : (only in Web Contexts) the HttpServletResponse object.
						#session : (only in Web Contexts) the HttpSession object.
						#servletContext : (only in Web Contexts) the ServletContext object.
					3.內建的工具物件:
						#execInfo : information about the template being processed.
						#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.
						#uris : methods for escaping parts of URLs/URIs
						#conversions : methods for executing the configured conversion service (if any).
						#dates : methods for java.util.Date objects: formatting, component extraction, etc.
						#calendars : analogous to #dates , but for java.util.Calendar objects.
						#numbers : methods for formatting numeric objects.
						#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
						#objects : methods for objects in general.
						#bools : methods for boolean evaluation.
						#arrays : methods for arrays.
						#lists : methods for lists.
						#sets : methods for sets.
						#maps : methods for maps.
						#aggregates : methods for creating aggregates on arrays or collections.
						#ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).
Selection Variable Expressions: *{...}		(選擇表示式,和${}在用法上大體一致)
								補充:配合th:object="${session.user}"
								<div th:object="${session.user}">
									<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
									<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
									<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
								</div>
Which is exactly equivalent to:
								<div>
									<p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
									<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
									<p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
								</div>
Message Expressions: #{...}					(獲取國際化內容)
Link URL Expressions: @{...}				(定義url)
<a href="details.html"
th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
Fragment Expressions: ~{...}				(片段引用表示式)	

Literals(字面量)
	Text literals: 'one text' , 'Another one!' ,…
	Number literals: 0 , 34 , 3.0 , 12.3 ,…
	Boolean literals: true , false
	Null literal: null
	Literal tokens: one , sometext , main ,…
Text operations		(文字操作)
	String concatenation: +
	Literal substitutions: |The name is ${name}|
	Arithmetic operations		(數學運算)
	Binary operators: + , - , * , / , %
	Minus sign (unary operator): -
Boolean operations		(布林運算)
	Binary operators: and , or
	Boolean negation (unary operator): ! , not
Comparisons and equality		(比較運算)
	Comparators: > , < , >= , <= ( gt , lt , ge , le )
	Equality operators: == , != ( eq , ne )
Conditional operators(條件運算)
	If-then: (if) ? (then)
	If-then-else: (if) ? (then) : (else)
	Default: (value) ?: (defaultvalue)
Special tokens(特殊操作)
	No-Operation: _		

SpringBoot對SpringMVC的web主要的自動配置

  • SpringBoot預設自動配置了SpringMVC
  • 自動配置了ViewResolver-ContentNegotiatingViewResolver,BeanNameViewResolver(檢視解析器:根據方法的返回值得到檢視物件,檢視物件決定轉發、重定向)
    1.ContentNegotiatingViewResolver: 組合所有的檢視解析器
    1.1:如何定製配置-在容器中新增一個定製的檢視解析器,ContentNegotiatingViewResolver會自動將定製的檢視解析器組合進來
  • 靜態資原始檔夾路徑和web.jars
  • 靜態首頁訪問
  • favicon.ico
  • 自動註冊Converter,GenericConverter,Formatter
    1.Converter:轉換器,型別轉換使用
    2.GenericConverter:通用轉換器,多個源型別和目標型別之間進行轉換。
    3.Formatter:格式化器-可以自己定製格式化轉換器放在容器中即可以配置
  • HttpMessageConverter: SpringMVC用來轉換Http請求和響應的。從容器中確定HttpMessageConverters值。可以自己將定製配置的HttpMessageConverter放在容器中即可配置。
  • MessageCodeResolver: 定義錯誤程式碼生成規則
  • ConfigurableWebBindingInitializer: 初始化web資料繫結器,繫結器把請求資料繫結.可以配置ConfigurableWebBindingInitializer新增到容器中替換預設的

如何修改SpringBoot的預設配置

  • SpringBoot在自動配置很多元件時,先看容器中有沒有已經配置 (@Bean,@Component) 好的元件,如果有,就用已經配置好的,如果沒有,才自動配置;如果元件可以有多個,將已經配置的和預設配置的組合起來。

擴充套件MVC(不能標註@EnableWebMvc)

  • 編寫一個配置類(@Configuration),繼承WebMvcConfigurationSupport,不能標註@EnableWebMvc。既保留了所有的自動配置,也可以使用擴充套件的配置。在做配置時,會匯入
@Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})

EnableWebMvcConfiguration:

 @Configuration
    public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {

DelegatingWebMvcConfiguration:

  //
  @Autowired(
        required = false
    )
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
        }

    }

參考實現

 //將所有的WebMvcConfigurer相關的配置都呼叫一遍
 public void addViewControllers(ViewControllerRegistry registry) {
        Iterator var2 = this.delegates.iterator();

        while(var2.hasNext()) {
            WebMvcConfigurer delegate = (WebMvcConfigurer)var2.next();
            delegate.addViewControllers(registry);
        }

    }
  • 容器中所有的WebMvcConfigurer都會起作用。配置的配置類也會被呼叫。這樣Spring的自動配置和擴充套件配置都會起作用。

全面接管SpringMVC(@EnableWebMvc)—不推薦使用

  • 禁用SpringBoot對SpringMVC的自動配置,全面對SpringMVC進行配置。在配置類中標註@EnableWebMvc。所有的SpringMVC的預設配置都被禁用了。
  • @EnableWebMvc
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}

DelegatingWebMvcConfiguration:

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

WebMvcAutoConfiguration:

@Configuration
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})	//當容器中沒有此元件時,此自動配置類才生效
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
  • @EnableWebMvc將WebMvcAutoConfigurationSupport匯入進來,不包括SpringMVC的功能。
    總結:
  • 多多學習SpringBoot中的XxxConfigurer,進行擴充套件配置

相關文章