SpringBoot中對SpringMVC的自動配置詳解
一. SpringMVC自動配置
spring boot自動配置好了springmvc,以下是springboot對springmvc的自動配置(WebMvcAutoConfiguration.class)
1. Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
1) 自動配置了ViewResolver(視圖解析器:根據(jù)方法的返回值得到視圖對象(View),視圖對象決定如何渲染(轉(zhuǎn)發(fā)?重定向?))
2) ContentNegotiatingViewResolver:組合所有的視圖解析器的;
public View resolveViewName(String viewName, Locale locale) throws Exception {
RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");
List<MediaType> requestedMediaTypes = getMediaTypes(((ServletRequestAttributes) attrs).getRequest());
if (requestedMediaTypes != null) {
List<View> candidateViews = getCandidateViews(viewName, locale, requestedMediaTypes); //獲取所有View
View bestView = getBestView(candidateViews, requestedMediaTypes, attrs); //獲得正確的View
if (bestView != null) {
return bestView;
}
}
3) 如何定制:我們可以自己給容器中添加一個(gè)視圖解析器;自動的將其組合進(jìn)來;
2. Support for serving static resources, including support for WebJars (see below):靜態(tài)資源文件夾路徑,webjars
3. Static index.html support.:靜態(tài)首頁訪問
4. Custom Favicon support (see below).:favicon.ico
5. 自動注冊了 of Converter , GenericConverter , Formatter beans.
1)Converter:轉(zhuǎn)換器;例如: public String hello(User user):頁面上類型轉(zhuǎn)換使用Converter(表單屬性能對應(yīng)上User屬性)
2)Formatter:格式化器;例如:日期格式化
@Override
public void addFormatters(FormatterRegistry registry) {
for (Converter<?, ?> converter : getBeansOfType(Converter.class)) {
registry.addConverter(converter);
}
for (GenericConverter converter : getBeansOfType(GenericConverter.class)) {
registry.addConverter(converter);
}
for (Formatter<?> formatter : getBeansOfType(Formatter.class)) {
registry.addFormatter(formatter);
}
}
3)自己添加的格式化類型轉(zhuǎn)化器,我們只需要放在容器中即可
6. Support for HttpMessageConverters (see below).
1)HttpMessageConverter:SpringMVC用來轉(zhuǎn)換Http請求和響應(yīng)的;User---Json;
2)HttpMessageConverters 是從容器中確定;獲取所有的HttpMessageConverter;自己給容器中添加HttpMessageConverter,只需要將自己的組件注冊容器中(@Bean,@Component)
7. Automatic registration of MessageCodesResolver (see below).定義錯誤代碼生成規(guī)則
8. Automatic use of a ConfigurableWebBindingInitializer bean (see below).
我們可以配置一個(gè)ConfigurableWebBindingInitializer來替換默認(rèn)的;(添加到容器)
@Override
protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer() {
try {
return this.beanFactory.getBean(ConfigurableWebBindingInitializer.class);
}
catch (NoSuchBeanDefinitionException ex) {
return super.getConfigurableWebBindingInitializer();
}
}
二. 擴(kuò)展SpringMVC
例如原先SpringMVC的配置文件中我們可以配置攔截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>
在SpringBoot中我們是全注解式開發(fā),因此我們?nèi)绻?;想?shí)現(xiàn)攔截器的功能,則需要編寫一個(gè)配置類@Configuration,是WebMvcConfigurerAdapter類型;不能標(biāo)注@EnableWebMvc;
既保留了所有的自動配置,也能用我們擴(kuò)展的配置;
//使用WebMvcConfigurerAdapter可以來擴(kuò)展SpringMVC的功能@Configurationpublic class MyMvcConfig extends WebMvcConfigurationSupport{
//使用WebMvcConfigurerAdapter可以來擴(kuò)展SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport{
@Autowired
private UserTokenInterceptor userTokenInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多個(gè)攔截器組成一個(gè)攔截器鏈
// addPathPatterns 用于添加攔截規(guī)則
// excludePathPatterns 用戶排除攔截
registry.addInterceptor(userTokenInterceptor).addPathPatterns("/**")
.excludePathPatterns("/account/login","/account/register");
super.addInterceptors(registry);
}
}
原理:
1)WebMvcAutoConfiguration是SpringMVC的自動配置類
2)在做其他自動配置時(shí)會導(dǎo)入;@Import(EnableWebMvcConfiguration.class)
@Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.configurers.addWebMvcConfigurers(configurers);
}
}
3)容器中所有的WebMvcConfigurer都會一起起作用;
4)我們的配置類也會被調(diào)用;效果:SpringMVC的自動配置和我們的擴(kuò)展配置都會起作用;
三. 全面接管SpringMVC
SpringBoot對SpringMVC的自動配置不需要了,所有都是我們自己配置;所有的SpringMVC的自動配置都失效了
我們需要在配置類中添加@EnableWebMvc即可;
//使用WebMvcConfigurerAdapter可以來擴(kuò)展SpringMVC的功能
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
//瀏覽器發(fā)送 /hello 請求來到 success
registry.addViewController("/hello").setViewName("success");
}
}
原理:
1)@EnableWebMvc的核心
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
2)
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
}3)
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
//容器中沒有這個(gè)組件的時(shí)候,這個(gè)自動配置類才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
4)@EnableWebMvc將WebMvcConfigurationSupport組件導(dǎo)入進(jìn)來;
5)導(dǎo)入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;
四. 如何修改SpringBoot的默認(rèn)配置
1)、SpringBoot在自動配置很多組件的時(shí)候,先看容器中有沒有用戶自己配置的(@Bean、@Component)如果有就用用戶配置的,如果沒有,才自動配置;
如果有些組件可以有多個(gè)(ViewResolver)將用戶配置的和自己默認(rèn)的組合起來;
2)、在SpringBoot中會有非常多的xxxConfigurer幫助我們進(jìn)行擴(kuò)展配置3)、在SpringBoot中會有很多的xxxCustomizer幫助我們進(jìn)行定制配置
到此這篇關(guān)于SpringBoot中對SpringMVC的自動配置詳解的文章就介紹到這了,更多相關(guān)SpringMVC自動配置詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java并發(fā)編程之進(jìn)程和線程調(diào)度基礎(chǔ)詳解
這篇文章主要介紹了java并發(fā)編程之進(jìn)程和線程調(diào)度基礎(chǔ),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
如何解決Mybatis-plus中@TableLogic注解失效問題
這篇文章主要介紹了如何解決Mybatis-plus中@TableLogic注解失效問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Springboot項(xiàng)目平滑關(guān)閉及自動化關(guān)閉腳本
這篇文章主要為大家詳細(xì)介紹了Springboot項(xiàng)目平滑關(guān)閉及自動化關(guān)閉腳本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
簡單了解JAVA內(nèi)存泄漏和溢出區(qū)別及聯(lián)系
這篇文章主要介紹了簡單了解JAVA內(nèi)存泄漏和溢出區(qū)別及聯(lián)系,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java中將String類型轉(zhuǎn)換為int類型的幾種常見方法
在java中經(jīng)常會遇到需要對數(shù)據(jù)進(jìn)行類型轉(zhuǎn)換的場景,這篇文章主要給大家介紹了關(guān)于Java中將String類型轉(zhuǎn)換為int類型的幾種常見方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07

