• spring-boot配置外部静态资源的方法


    import java.io.File;
    
    import javax.servlet.Servlet;
    
    import lombok.extern.slf4j.Slf4j;
    
    import org.springframework.boot.autoconfigure.AutoConfigureAfter;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
    import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
    import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.Ordered;
    import org.springframework.core.annotation.Order;
    import org.springframework.web.servlet.DispatcherServlet;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    @ConditionalOnWebApplication
    @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class})
    @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
    @Order(Ordered.HIGHEST_PRECEDENCE )
    @AutoConfigureAfter(DispatcherServletAutoConfiguration.class)
    @Slf4j
    public class StaticResourceConfiguration extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            /*
            File directory = new File("");
            String path2=directory.getAbsolutePath();
            StringBuilder builder = new StringBuilder();
            builder.append("file:").append(path2).append("/m/");
    
            //registry.addResourceHandler("/**").addResourceLocations( builder.toString() );
            //String myExternalFilePath = "file:///C:/Temp/whatever/m/";
            log.info( builder.toString() );
            */
            
            registry.addResourceHandler("/m/**").addResourceLocations("file:m/");
            //registry.addResourceHandler("/m/**").addResourceLocations(builder.toString());
    
            super.addResourceHandlers(registry);
        }
    }
  • 相关阅读:
    [BZOJ2431] [HAOI2009]逆序对数列
    [Luogu2323] [HNOI2006]公路修建问题
    [Luogu2455] [SDOI2006]线性方程组
    [BZOJ3550] [Sdoi2014]数数
    [Noip2017] 列队
    [Luogu2824] [HEOI2016/TJOI2016]排序
    [BZOJ1060] [ZJOI2007]时态同步
    P1036 选数 题解
    快速幂取模算法详解
    同余定理及其应用
  • 原文地址:https://www.cnblogs.com/feika/p/4543754.html
Copyright © 2020-2023  润新知