Last active
September 24, 2022 13:36
-
-
Save bclozel/10172413 to your computer and use it in GitHub Desktop.
Resource Handling configuration drafts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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