14.springboot對springmvc的自動配置
阿新 • • 發佈:2022-05-10
參考地址;https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications
Spring MVC auto-configuration
5.如何修改springboot的預設配置呢: 首先了解springboot的預設配置模式: 1).springboot在自動配置很多元件的時候,先看容器中有沒有使用者自己配置的(@Bean,@Component)如果有就用使用者自己配置的,如果沒有,才自動配置;如果有些元件可以有多個(ViewResolver)將使用者的配置和自己預設的組合起來! 2).如何擴充套件springmvcspringboot自動配置好了springmvc 以下是springboot對springmvc的預設: 1.Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans. 。自動配置了ViewResolver(檢視解析器:根據方法的返回值得到檢視物件(view)),檢視物件決定如何渲染(轉發/重定向) 。ContentNegotiatingViewResolver:組合所有的檢視解析器的 。如何定製自己的檢視解析器:我們可以自己給容器中新增一個檢視解析器;ContentNegotiatingViewResolver會自動將其組合進來 2.Support for serving static resources, including support for WebJars (see below).:靜態資原始檔夾,webjars 3.Automatic registration of Converter, GenericConverter, Formatter beans. 。Converter:轉換器;頁面引數和後臺的實體類進行格式換轉化:例如頁面引數的18是text要轉換為後臺的int 。Formatter 格式花器:例如時間:2020-09-20=====Date 原始碼如下: @Bean @Override//需要在application.properties配置檔案中配置格式化的規格:spring.mvc.format.date=yy-MM-dd public FormattingConversionService mvcConversionService() { Format format = this.mvcProperties.getFormat(); WebConversionService conversionService = new WebConversionService(new DateTimeFormatters() .dateFormat(format.getDate()).timeFormat(format.getTime()).dateTimeFormat(format.getDateTime())); addFormatters(conversionService); return conversionService; } 自己新增的格式花轉換器,我們只需要放到容器中即可 使用參考上述手冊:https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications 4.Support for HttpMessageConverters (see below). 。HttpMessageConverters :springmvc用來轉換http請求和響應的:例如user物件轉json字串 。HttpMessageConverters 是從容器中獲取;獲取所有的HttpMessageConverters ; 自己給容器中太你家HttpMessageConverters ,只需要將自己的元件註冊到容器中(@Bean,@Component) 使用參考上述手冊:https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications 5. Automatic registration of MessageCodesResolver (see below).定義錯誤程式碼生成規則 6. Static index.html support.:靜態首頁 Custom Favicon support (see below). 7.Automatic use of a ConfigurableWebBindingInitializer bean (see below). 初始化webDataBinder:web資料繫結器 請求資料和javaBean的繫結 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.
3.全面接管springmvc springboot對springmvc的自動配置不需要了,所有的都是我們自己配置;所有的springmvc都失效了 如何做呢:我們只需要在配置類上加上:@EnableWebMvc如下(儘量不要用)如spring配置的,springboot如何實現呢 如果傳送的請求不想通過controller,只想直接地跳轉到目標頁面 <mvc:view-controller path="/hello" view-name="success"/> 攔截器 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/hello"/> <bean></bean> </mvc:interceptor> </mvc:interceptors> springboot可以這麼做: 1.使用@Configuration註解告訴springboot這是一個配置類 2.類上不能標註@EnableWebMvc標籤() 2.繼承於WebMvcConfigurerAdapter 3.重寫WebMvcConfigurerAdapter裡的方法 示例如下: //使用WebMvcConfigurerAdapter來擴充套件springmvc功能 @Configuration public class Myconfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { //瀏覽器傳送hello1請求,直接來到success頁面:不用通過控制層@requ registry.addViewController("/hello1").setViewName("success"); } } 結論:這樣既保留了springboot所有對spring的自動配置,也能用我們的擴充套件
@EnableWebMvc(儘量不要加) @Configuration public class Myconfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { //瀏覽器傳送hello請求,直接來到success頁面:不用通過控制層@requ registry.addViewController("/hello1").setViewName("success"); } }