1. 程式人生 > >springboot-mvc+擴充套件[email&#

springboot-mvc+擴充套件[email&#

1. Spring MVC auto-configuration

27.1.1 Spring MVC auto-configuration

Spring Boot provides auto-configuration for Spring MVC that works well with most applications.

The auto-configuration adds the following features on top of Spring’s defaults:

Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
Support for serving static resources, including support for WebJars (see below). 靜態資原始檔夾 WebJars
Automatic registration of Converter, GenericConverter, Formatter beans.
Support for HttpMessageConverters (see below).   
Automatic registration of MessageCodesResolver (see below).
Static index.html support.  靜態首頁訪問
Custom Favicon support (see below).
Automatic use of a ConfigurableWebBindingInitializer bean (see below).
If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Configuration class of type WebMvcConfigurerAdapter, but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you can declare a WebMvcRegistrationsAdapter instance providing such components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

解析:

Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans

  • 自動配置了檢視解析器,根據方法的返回值得到檢視對,檢視物件決定如何渲染
  • ContentNegotiatingViewResolver:組合所有的檢視解析器的;
  • 如何定製:我們可以自己給容器中新增一個檢視解析器,自動的將其組合進來
載入所有檢視,並獲取最優的那個.
@Bean
@ConditionalOnBean(ViewResolver.class)
@ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class)
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
   ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
   resolver.setContentNegotiationManager(
         beanFactory.getBean(ContentNegotiationManager.class));
   // ContentNegotiatingViewResolver uses all the other view resolvers to locate
   // a view so it should have a high precedence
   resolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
   return resolver;
}
初始化的時候載入了所有檢視解析器
@Override
protected void initServletContext(ServletContext servletContext) {
   Collection<ViewResolver> matchingBeans =
         BeanFactoryUtils.beansOfTypeIncludingAncestors(obtainApplicationContext(), ViewResolver.class).values();
   if (this.viewResolvers == null) {
      this.viewResolvers = new ArrayList<>(matchingBeans.size());
      for (ViewResolver viewResolver : matchingBeans) {
         if (this != viewResolver) {
            this.viewResolvers.add(viewResolver);
         }
      }
   }

  定製一個檢視解析器

@SpringBootApplication
public class WebApplication {

   public static void main(String[] args) {
      SpringApplication.run(WebApplication.class, args);
   }




   //定製一個檢視解析器
   @Bean
   public ViewResolver myViewReolver(){
      return new MyViewResolver();
   }

   public static class MyViewResolver implements ViewResolver{

      @Override
      public View resolveViewName(String viewName, Locale locale) throws Exception {
         return null;
      }
   }
}

驗證

Automatic registration of Converter, GenericConverter, Formatter beans

轉換器、通用轉換器、格式化bean的自動註冊。

Converter:將瀏覽器資料封裝到controller對應的方法引數中,比如:public void (User user),這裡使用了反射。

Formatter 格式化器;比如日期格式化

Support for HttpMessageConverters (see below).   可以自己定義。

HttpMessageConverters :SpringMVC用來轉換Http請求和響應的,比如響應時候將User轉換為Json;

他從容器中載入來,是從容器中確定。所以可以自己定義。

public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>> {

  ........
構造器新增物件
   public HttpMessageConverters(HttpMessageConverter<?>... additionalConverters) {
      this(Arrays.asList(additionalConverters));
   }

Automatic use of a ConfigurableWebBindingInitializer bean (see below).

自動配置web繫結的初始化

2擴充套件SpringMVC(WebMvcConfigurerAdapter)

01實現檢視解析器和攔截器的功能

<mvc:view‐controller path="/hello" view‐name="success"/>
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/hello"/>
        <bean></bean>
    </mvc:interceptor>
</mvc:interceptors>
 

做法

//使用WebMvcConfigurerAdapter可以來擴充套件SpringMVC的功能
//@EnableWebMvc   不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {



    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //瀏覽器傳送 /success2 請求來到 success
        registry.addViewController("/success2").setViewName("success");
    }

}

[email protected]

讓所有的SpringMVC的自動配置都失效了,使用使用者自己的配置

//使用WebMvcConfigurerAdapter可以來擴充套件SpringMVC的功能,
 @EnableWebMvc 去掉mvc的自動配置
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {



    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //瀏覽器傳送 /success2 請求來到 success
        registry.addViewController("/success2").setViewName("success");
    }

}

4 修改自動化配置

  1. 先看容器中有沒有使用者自己配置的(@Bean、@Component)如
    1.     有:就用使用者配置的    
    2.     沒有: 自動配置;
    3.     元件有多個(如ViewResolver):將使用者配置的和容器預設的組合起來;
  2. 在SpringBoot中會有非常多的xxxConfigurer幫助我們進行擴充套件配置
  3. 在SpringBoot中會有很多的xxxCustomizer幫助我們進行定製配置

相關推薦

springboot-mvc+擴充套件<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c7f5c5e45424b617a6f076c69424d4e40497b494e615a4f">[email&#

1. Spring MVC auto-configuration 27.1.1 Spring MVC auto-configuration Spring Boot provides auto-configuration for Spring MVC that wor

Springboot註解<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="260b0b666549485254494a4a4354">[email protected]a>和@RestCon

1.使用@Controller 註解,在對應的方法上,檢視解析器可以解析return 的jsp,html頁面,並且跳轉到相應頁面;若返回json等內容到頁面,則需要加@ResponseBody註解 [email protected]註解,相當於@[email protected

SpringBoot學習<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7e8f7e7c5d8c7d2c5c3cee4d8c2c5d4d2">[email protected]a>&am

文章目錄 @PropertySource:載入指定的配置檔案 @ImportResource:匯入Spring的配置檔案,讓配置檔案裡面的內容生效; @Bean @PropertySource:載入指定的配置檔案

springboot-註解<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e137e6c5b4e514d574a514c47">[email protected]a>、@Service、

Spring 2.5 中除了提供 @Component 註釋外,還定義了幾個擁有特殊語義的註釋,它們分別是:@Repository、@Service 和 @Controller。 在目前的 Spring 版本中,這 3 個註釋和 @Component 是等效的,但是從註釋類的

Spring MVC系列(四)之session處理<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8858585e8fbcddbdbc1c7c6e9dcdcdac1cadddccddb">[email

介紹        在web開發中,session的重要性不言而喻,與cookie相比,session更加安全,處於伺服器端,開發者經常把一些重要的資訊放在session,方便在多次請求中方便的獲取資訊,Spring MVC 對session的支援也依舊很強大很靈活 Sp

Spring MVC之@<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c597a0b4b0a0b6b187aaa1bc8597a0b6b5aaabb6a087aaa1bc">[email p

引言: 接上一篇文章講述處理@RequestMapping的方法引數繫結之後,詳細介紹下@RequestBody、@ResponseBody的具體用法和使用時機; 簡介: @RequestBody 作用:        i) 該註解用於讀取Request請求的bod

shell腳本中的$# $0 <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f8dcb8">[email protected]a> $* $$ $! $?的意義

腳本 $* width 上一個 pre shell int .cn height 轉載自:http://www.cnblogs.com/davygeek/p/5670212.html 今天學寫腳本遇到一些變量不認識,在此做下記錄。 變量 含義 $0 當前腳本的文件

shell中$*與<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b296f2">[email protected]a>的區別

劃分 位置 一個 這也 差異 獨立 [email protected] 情況 雙引號 $*所有的位置參數,被作為一個單詞 註意:"$*"必須被""引用 [email protected] 與$*同義,但是每個參數都是一個獨立的""引用字串,這就意味著參數

Spring4.0系列<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aa9f87eae9c5c4cec3dec3c5c4cbc6">[email protected]a>

one window 標識 cto ace ted ada bsp 布爾 這篇文章介紹Spring 4的@Conditional註解。在Spring的早期版本你可以通過以下方法來處理條件問題: 3.1之前的版本,使用Spring Expression Langua

Spring高級話題<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b29ff2f7dcd3d0ded7">[email protected]a>***註解的工作原理

sso metadata bool logs tcl task ota -c ann 出自:http://blog.csdn.net/qq_26525215 @EnableAspectJAutoProxy @EnableAspectJAutoProxy註解 激活Aspe

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="297a595b40474e69685c5d465e405b4c4d">[email protected]a>註解與自動裝配(轉發)

配置 調用方法 support autowired 信息 ann over 反射機制 test 1 配置文件的方法我們編寫spring 框架的代碼時候。一直遵循是這樣一個規則:所有在spring中註入的bean 都建議定義成私有的域變量。並且要配套寫上 get 和 se

linux bash Shell特殊變數:Shell $0, $#, $*, <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8aaeca">[email protected]a>, $?

在linux下配置shell引數說明 前面已經講到,變數名只能包含數字、字母和下劃線,因為某些包含其他字元的變數有特殊含義,這樣的變數被稱為特殊變數。  例如,$ 表示當前Shell程序的ID,即pid,看下面的程式碼: [[email protected] /]$ ec

spring <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="62000d0d16222103010a0703000e07">[email protected]a>中value的理解

先看原始碼 /** * Names of the caches in which method invocation results are stored. * <p>Names may be used to determine the target cache (or cac

{<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="733e3c3f3f2a342136363d203323213c273c3d3e323a3f5d303c3e">[email protecte

近日,復旦解密安全團隊發現GandCrab4.0活躍度提升,跟蹤到多起GandCrab4.0變種勒索事件,現釋出安全預警,提醒廣大使用者預防GandCrab4.0勒索。 目前復旦解密已經可以成功解密GandCrab4.0變種採用RSA+AES加密演算法 mg中毒檔案可以在一個小時解決.電話151691214

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b2c3e391b33">[email protected]a>,c小總結

問題0:元素內聯元素,行內元素,行內塊元素.         內聯: 寬高M,P都有效         行內元素:無寬高,內容撐開,M,P左右有效  

SQL Server資料庫mdf檔案中了勒索病毒<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc9f8e858c889998a39d8f9d9293bc9f939f97">[email p

SQL,資料庫,勒索病毒,mdf檔案中毒,[email protected]_email *SQL Server資料庫mdf檔案中了勒索病毒[email protected]_email。副檔名變為[email protected]_email SQL Serv

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5400313a273b2632383b2379142032">[email protected]a>_export詳解

Tensorflow經常看到定義的函式前面加了“@tf_export”。例如,tensorflow/python/platform/app.py中有: @tf_export('app.run') def run(main=None, argv=None): """Runs the progr

手把手教你搭建React Native 開發環境 - ios篇 (React <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eda38c99849b88adddc3d8d8c3d9">[email&#

由於之前我是h5的,沒接觸過ios和安卓, 也不瞭解xcode配置,所以 建議學reace-native之前還是先去了解一下ios和安卓開發環境搭建等問題。 環境下載及配置 nodejs:https://nodejs.org/en/download/ 設定淘寶映象 $ npm con

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4978382833093e3a3179797878">[email protected]a>

function changeSpan(){         var f = document.getElementById("file1").files;         &nb

eclipse支援@<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95d2f0e1e1f0e7d5c6f0e1e1f0e7">[email protected]a>註解使用 -轉載

1. 下載lombok.jar 2.將下載的lombok.jar放在你的eclipse安裝目錄下,如圖: 3.修改eclipse.ini檔案,新增如下兩行配置:   -Xbootclasspath/a:lombok.jar -javaage