Annotation Configuration Replacement for mvc:resources - Spring

Dan W picture Dan W · Feb 13, 2013 · Viewed 48.9k times · Source

I'm trying to upgrade my spring mvc project to utilize the new annotations and get rid of my xml. Previously I was loading my static resources in my web.xml with the line:

<mvc:resources mapping="/resources/**" location="/resources/" /> 

Now, I'm utilizing the WebApplicationInitializer class and @EnableWebMvc annotation to startup my service without any xml files, but can't seem to figure out how to load my resources.

Is there an annotation or new configuration to pull these resources back in without having to use xml?

Answer

AHungerArtist picture AHungerArtist · Feb 13, 2013

For Spring 3 & 4:

One way to do this is to have your configuration class extend WebMvcConfigurerAdapter, then override the following method as such:

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}