Exclude particular file (static resource)

2019-08-29 20:13发布

I have a spring boot application and below is the code for registrations of resource handlers:

@Configuration    
public class MyAppConfig extends WebMvcConfigurerAdapter {
       ...
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/js/**")         
            .addResourceLocations("/resources/js/").setCachePeriod(31536000);
          //in same directory where all the js files placed,
          //I want to cache all the files in that directory
          //except for one file i.e. prop.js
          /*registry.addResourceHandler("/resources/js/prop.js")
           .addResourceLocations("/resources/js/").setCachePeriod(0);*/
       }
       ...
    }

There are certain things I need to understand, viz.
1. What does the parameter of addResourceHandler mean?
2. What does the parameter of addResourceLocations mean?
3. Is there any way to explicitly prevent prop.js from being cached?

There are may files at the same level where prop.js is, I want all other files to be cached.

1条回答
混吃等死
2楼-- · 2019-08-29 20:44
  1. In the addResourceHandler method, you specify URL, or URL pattern.
  2. In the addResourceLocations, you specify a valid directory actually containing (or possibly many directories separated by commas) the static resources.

So in the end, you made a simple mapping between URL ans directories on your web-server.

  1. if you want to disable caching only for the file prop.js, try this :

    registry
        .addResourceHandler("/resources/js/**/prop.js")
        .addResourceLocations("/resources/js/")
        .setCachePeriod(0);
    registry
        .addResourceHandler("/resources/js/**")
        .addResourceLocations("/resources/js/")
        .setCachePeriod(31536000);
    
查看更多
登录 后发表回答