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

概述

  • 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下面資源的名稱。

/**:訪問當前項目的任何資源(靜態資源的文件夾)html

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());
        }

配置歡迎頁的映射:java

  • 歡迎頁:靜態資源文件夾下的全部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;
            }

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

  • 標籤圖標:全部的 **/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語法:spring

  • 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:express

//
  @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:api

@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,進行擴展配置
相關文章
相關標籤/搜索