SpringBoot扩展SpringMVC原理并实现全面接管

网友投稿 407 2023-03-15

SpringBoot扩展SpringMVC原理并实现全面接管

SpringBoot扩展SpringMVC原理并实现全面接管

如果想在SpringBoot中扩展一些SpringMVC的配置,例如需要配置自定义的视图解析器或-等,需要怎么实现呢?

例如,自定义一个视图解析器:

@Configuration

public class MyConfig implements WebMvcConfigurer {

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addViewController("/index").setViewName("index");

}

}

我们只需要编写一个配置类去实现WebMvcConfigurer接口,并选择实现接口中的方法,不能标注@EnableWebMvc,这些WebMvcConfigurer接口中的方法就是SpringMVC所可以扩展的配置

注意:在SpringBoot1.0版本中扩展SpringMVC配置是继承WebMvcConfigurerAdapter类,但在2.0以上的版本中已经过时,官方推荐使用以上实现WebMvcConfigurer接口的方式进行扩展,因为在2.0版本中WebMvcConfigurer接口有了默认实现。

WebMvcConfigurer方法介绍:这里只列举几个比较关键的方法

public interface WebMvcConfigurer {

//定制URL匹配规则

default void configurePathMatch(PathMatchConfigurer configurer) {

}

//内容协商机制

default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

}

//异步任务执行器。

default void configureAsyncSupport(AsyncSupportConfigurer configurer) {

}

//使用默认servlet处理静态资源

default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

}

//添加格式转换器

default void addFormatters(FormatterRegistry registry) {

}

//添加-

default void addInterceptors(InterceptorRegistry registry) {

}

//添加视图解析器

default void addViewControllers(ViewControllerRegistry registry) {

}

}

扩展MVC的实现原理:

我们都知道WebMvcAutoConfiguration是SpringMVC的自动配置类,当在做其他配置导入时,导入了@Import(EnableWebMvcConfiguration.class)这样一个注解,这个注解有什么用?

@Configuration(proxyBeanMethods = false)

@Import(EnableWebMvcConfiguration.class)

@EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })

@Order(0)

public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {

}

点进这个注解,发现他还是WebMvcAutoConfiguration里的一个静态内部类,但他继承了DelegatingWebMvcConfiguration

@Configuration(proxyBeanMethods = false)

public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {

}

再点进这个DelegatingWebMvcConfiguration类里,开头有这样一段代码,有一个configurers属性,类型是WebMvcConfigurerComposite ,这个WebMvcConfigurerComposite类也实现了WebMvcConfigurer,当@Autowired标注在一个方法上说明,这个方法的参数都从容器中获取,这里是从容器中获取所有的WebMvcConfigurer,并赋值给了configurers属性

@Configuration(proxyBeanMethods = false)

public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

@Autowired(required = false)

public void setConfigurers(List configurers) {

if (!CollectionUtils.isEmpty(configurers)) {

this.configurers.addWebMvcConfigurers(configurers);

}

}

}

在这个类往下看,发现这个类的方法跟WebMvcConfigurer接口里的方法一样,以这个视图解析器举例,方法里调用了这个方法this.configurers.addViewControllers(registry)

@Configuration(proxyBeanMethods = false)

public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

@Autowired(required = false)

public void setConfigurers(List configurers) {

if (!CollectionUtils.isEmpty(configurers)) {

this.configurers.addWebMvcConfigurers(configurers);

}

}

...

@Override

protected void addViewControllers(ViewControllerRegistry registry) {

this.configurers.addViewControllers(registry);

}

}

点进configurers.addViewControllers(registry),这个方法是把容器中所有的addViewControllers()都执行一遍。因为我们自己写的配置类也注入到了容器里,所以我们的配置也会被调用,并且也被SpringBoot自动配置上,所以SpringMVC的自动配置和我们的扩展配置都会起作用

class WebMvcConfigurerComposite implements WebMvcConfigurer {

...

@Override

public void addViewControllers(ViewControllerRegistry registry) {

for (WebMvcConfigurer delegate : this.delegates) {

delegate.addViewControllers(registry);

}

}

}

还有上面在写自定义配置类时为什么不能标注@EnableWebMvc

因为一但标注了@EnableWebMvc,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了。原理又是怎么样的?</mark>

给自己的配置类加上@EnableWebMvc

@Configuration

@EnableWebMvc

public class myConfig implements WebMvcConfigurer {

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addViewController("/index").setViewName("index");

}

}

这个注解导入了@Import(DelegatingWebMvcConfiguration.class)

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

@Documented

@Import(DelegatingWebMvcConfiguration.class)

public @interface EnableWebMvc {

}

这个类继承了WebMvcConfigurationSupport

public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

}

我们再回头看一下WebMvcAutoConfiguration,@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)这个注解的意思就是容器中没有这个组件的时候,这个自动配置类才生效

小结:大概了解到SpringBoot扩展SpringMVC的原理和全面接管SpringMVC,但SpringBoot中还有其他很多配置,只要了解其中的原理,其他配置也就一通百通了。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot使用Maven插件进行项目打包的方法
下一篇:小程序中视频编辑插件(微信小程序好用的视频插件)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~