Skip to content

Instantly share code, notes, and snippets.

@bclozel
Last active September 24, 2022 13:36
Show Gist options
  • Save bclozel/10172413 to your computer and use it in GitHub Desktop.
Save bclozel/10172413 to your computer and use it in GitHub Desktop.
Resource Handling configuration drafts
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- First draft at XML configuration: must refine! -->
<mvc:resources mapping="/resources/**" location="classpath:/public/, classpath:/static/" cache-period="3600">
<mvc:resolvers>
<list>
<ref bean="pathResourceResolver" />
</list>
</mvc:resolvers>
</mvc:resources>
<mvc:resources mapping="/resources/css/**" location="classpath:/static/css/" cache-period="3600">
<mvc:resolvers>
<list>
<ref bean="fingerprintResourceResolver" />
<ref bean="pathResourceResolver" />
</list>
</mvc:resolvers>
</mvc:resources>
<mvc:resources mapping="/resources/js/**" location="classpath:/static/js/" cache-period="3600">
<mvc:resolvers>
<list>
<ref bean="prefixResourceResolver" />
<ref bean="pathResourceResolver" />
</list>
</mvc:resolvers>
</mvc:resources>
<bean id="pathResourceResolver" class="org.springframework.web.servlet.resource.PathResourceResolver" />
<bean id="fingerprintResourceResolver" class="org.springframework.web.servlet.resource.FingerprintResourceResolver" />
<bean id="prefixResourceResolver" class="org.springframework.web.servlet.resource.PrefixResourceResolver" />
</beans>
@Configuration
public class MultipleResolverResourcesConfig extends WebMvcConfigurerAdapter {
// Multiple ResourceResolver configuration
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
List<ResourceResolver> jsResolvers = new ArrayList<>();
jsResolvers.add(new PrefixResourceResolver("hexprefix"));
jsResolvers.add(new PathResourceResolver());
List<ResourceResolver> cssResolvers = new ArrayList<>();
cssResolvers.add(new FingerprintResourceResolver());
cssResolvers.add(new PathResourceResolver());
List<ResourceResolver> defaultResolvers = new ArrayList<>();
defaultResolvers.add(new PathResourceResolver());
registry.addResourceHandler("/resources/**")
.addResourceLocations("classpath:/public/", "classpath:/static/")
.setResourceResolvers(defaultResolvers)
.setCachePeriod(3600);
registry.addResourceHandler("/resources/css/**")
.addResourceLocations("classpath:/static/css/")
.setResourceResolvers(cssResolvers)
.setCachePeriod(3600);
registry.addResourceHandler("/resources/js/**")
.addResourceLocations("classpath:/static/js/")
.setResourceResolvers(jsResolvers)
.setCachePeriod(3600);
}
}
@Configuration
public class MultipleVariantResolverResourcesConfig extends WebMvcConfigurerAdapter {
// Multiple ResourceResolver configuration
// Variant for {@link MultipleResolverResourcesConfig}, with varargs
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// do we want to share ResourceResolver instances between ResourceHandlers?
ResourceResolver pathResourceResolver = new PathResourceResolver();
registry.addResourceHandler("/resources/**")
.addResourceLocations("classpath:/public/", "classpath:/static/")
.setResourceResolvers(pathResourceResolver)
.setCachePeriod(3600);
registry.addResourceHandler("/resources/css/**")
.addResourceLocations("classpath:/static/css/")
.setResourceResolvers(new FingerprintResourceResolver(), pathResourceResolver)
.setCachePeriod(3600);
registry.addResourceHandler("/resources/js/**")
.addResourceLocations("classpath:/static/js/")
.setResourceResolvers(new PrefixResourceResolver("hexprefix"), pathResourceResolver)
.setCachePeriod(3600);
}
}
@Configuration
public class ResolverResourcesConfig extends WebMvcConfigurerAdapter {
// ResourceResolver configuration
// serve resources from two classpath locations and use a list of resolvers for both
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
List<ResourceResolver> resourceResolvers = new ArrayList<>();
resourceResolvers.add(new FooResourceResolver());
resourceResolvers.add(new PathResourceResolver());
registry.addResourceHandler("/resources/**")
.addResourceLocations("classpath:/public/", "classpath:/static/")
.setResourceResolvers(resourceResolvers)
.setCachePeriod(3600);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Simple configuration, serve resources from two classpath locations -->
<mvc:resources mapping="/resources/**" location="classpath:/public/, classpath:/static/" cache-period="3600"/>
</beans>
@Configuration
public class SimpleResourcesConfig extends WebMvcConfigurerAdapter {
// Simple configuration
// serve resources from two classpath locations
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("classpath:/public/", "classpath:/static/")
.setCachePeriod(3600);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment