问题
This is from WebMvcConfigurerAdapter, the official Spring documentation.
Deprecated. as of 5.0 WebMvcConfigurer has default methods (made possible by a Java 8 baseline) and can be implemented directly without the need for this adapter
I looked at the source code of WebMvcConfigurer but I couldn't find a single method with the keyword 'default'.
How exactly does WebMvcConfigurer have default methods defined?
I must confess that I don't understand
(made possible by a Java 8 baseline)
I tried googling but nothing came up for 'Java 8 baseline'. (Some kind of new feature?)
Currently, my local setup uses WebMvcConfigurer interface, with all the methods auto-generated and they have nothing in the method body.
I'm roughly following the Spring REST guide and so far everything seems to be working.
How does this work exactly?
EDIT
I had my suspicions on WebMvcConfigurer. That is, it only adds or customises the configuration. I tested my local setup with a config class with @EnableWebMvc without extending or implementing any class or interface, and without any configuration in the class body. Everything(very minimalistic json response) works fine.
So my guess is, it doesn't matter if I implement WebMvcConfigurer and leave the method bodies empty.
Still, my curiosity is, why does the document say WebMvcConfigurer has default methods?
回答1:
The difference between Spring 5 and it's earlier releases within the context of this configuration class is, earlier Developers used WebMvcConfigurerAdapter
which was an abstract
class which implemented WebMvcConfigurer
If we closely look at why this was done, everything will be crystal clear.
The first line of WebMvcConfigurerAdapter
class says, An implementation of WebMvcConfigurer with empty methods allowing subclasses to override only the methods they're interested in
Thus, if someone wanted to write the configuration him/herself, They had to give the implementation of every method
in WebMvcConfigurer
.
So, Spring created an abstract
class so that the developers can choose the required features they wanted to use.
Since with JAVA 8, interfaces
came with default
implementations, Spring developers found a way out to get rid of the abstract
class by providing the default implementation in interface
itself instead of the abstract
class.
Hence, default
keyword was added as a prefix to every method in WebMvcConfigurer
回答2:
in version 5.0.4 they are pretty much there:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.web.servlet.config.annotation;
import java.util.List;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
public interface WebMvcConfigurer {
default void configurePathMatch(PathMatchConfigurer configurer) {
}
default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
}
default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
}
default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
}
default void addFormatters(FormatterRegistry registry) {
}
default void addInterceptors(InterceptorRegistry registry) {
}
default void addResourceHandlers(ResourceHandlerRegistry registry) {
}
default void addCorsMappings(CorsRegistry registry) {
}
default void addViewControllers(ViewControllerRegistry registry) {
}
default void configureViewResolvers(ViewResolverRegistry registry) {
}
default void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
}
default void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
}
default void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
}
default void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
}
default void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
}
default void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
}
@Nullable
default Validator getValidator() {
return null;
}
@Nullable
default MessageCodesResolver getMessageCodesResolver() {
return null;
}
}
来源:https://stackoverflow.com/questions/48575191/where-are-the-default-methods-of-webmvcconfigurer