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);
    }
}
时间: 2024-08-10 02:18:06

spring-boot配置外部静态资源的方法的相关文章

tomcat配置外部静态资源映射路径

一.背景 1.有一个录音软件每天生成很多新的录音文件. 2.现在想通过一个WEB项目页面下载这些录音文件. 3.很显然这些录音文件放在WEB项目下不是很合适(WEB项目更新是个大麻烦,海量的录音文件要处理). 二.外部静态资源映射 首先想到的就是tomcat能否直接配置静态资源路径?哈哈,答案是yes! 只需要修改tomcat的配置文件server.xml就能满足我的需求. 1. 文件路径 你的tomcat的安装路径/apache-tomcat-你的版本/conf 例如: /usr/local/

spring mvc 配置对静态资源的访问

在spring mvc的配置文件中做如下配置: 1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.s

spring boot系列(五)spring boot 配置spring data jpa (查询方法)

接着上面spring boot系列(四)spring boot 配置spring data jpa 保存修改方法继续做查询的测试: 1 创建UserInfo实体类,代码和https://www.cnblogs.com/kxm87/p/9273555.html中的一样. 2 创建数据库操作类相当于dao层,主要创建一个接口UserRepository,继承JpaRepository接口即可.本代码中主要都是自定义方法. 使用findXX 或者countXX(这两个不用编写sql,jpa会自动生成)

spring boot配置信息详解

# REDIS (Redis 配置)# 连接工厂使用的数据库索引spring.redis.database= 0# Redis服务器主机spring.redis.host= localhost# 登录redis服务器的密码spring.redis.password= # 给定时间池可以分配的最大连接数 使用负值为无限制spring.redis.pool.max-active= 8# 池中“空闲”连接的最大数量 使用负值来表示无限数量的空闲连接spring.redis.pool.max-idle=

spring mvc 加载静态资源

由于我们在web.xml进行了如下配置: <servlet> <servlet-name>spring_mvc_demo</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> &l

Spring Boot 配置优先级顺序

http://www.cnblogs.com/softidea/p/5759180.html 一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一点不同,配置读取可是一个让人有点伤脑筋的问题. Spring Boot提供了一种优先级配置读取的机制来帮助我们从这种困境中走出来. 常规情况下,我们都知道Spring Boot的配置会从application.pro

spring boot配置mybatis和事务管理

spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spring Boot 启动父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId>

springboot项目图片上传,回显;使用外部静态资源路径回显图片

//前端图片是Base64字符串形式传递图片参数:需要用Base解密,写入到本地磁盘中 public String upload(String string){ 解析图片(Base64): response.setHeader("Access-Control-Allow-Origin","*"); // 第二个参数填写允许跨域的域名称,不建议直接写 "*" response.setHeader("Access-Control-Allow

Spring Boot 配置元数据指南

1. 概览 在编写 Spring Boot 应用程序时,将配置属性映射到 Java bean 上是非常有用的.但是,记录这些属性的最好方法是什么呢? 在本教程中,我们将探讨 Spring Boot Configuration Processor 和 关联的 JSON 元数据文件,该 JSON 文档记录每个属性的含义.约束等. 2. 配置元数据 作为开发人员,我们开发的大多数应用程序在某种程度上必须是可配置的.但是在通常情况下,我们并不能够真正的理解配置参数的作用,比如它有默认值,又或者是过时的,