davioooh davioooh - 1 month ago 40
Java Question

Cannot extend WebMvcAutoConfigurationAdapter in Spring Boot 1.4

I'd like to update a web project to Spring Boot 1.4 but I'm getting and error.

I'm currently using Spring Boot 1.3.3 and I'm extending

WebMvcAutoConfigurationAdapter
to map some custom resource hanlders:

@Configuration
public class MvcConfiguration extends WebMvcAutoConfigurationAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
registry.addResourceHandler("/images/users/**").addResourceLocations("http://file-storage/images/users/");
// ...

}


I noticed that in the new version of SB a constructor was added to
WebMvcAutoConfigurationAdapter
:

public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties,
WebMvcProperties mvcProperties, ListableBeanFactory beanFactory,
HttpMessageConverters messageConverters,
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider) {}


and this constructor uses
ResourceHandlerRegistrationCustomizer
that is a package-protected interface, so, basically, from version 1.4 is not possible to extend autoconfiguration.

What is an alternative way to extend autoconfiguration?

Answer

There's no need to extend WebMvcAutoConfigurationAdapter to add some custom resource handlers (and never has been). You should extend Spring MVC's WebMvcConfigurerAdapter instead.