从零开始学Spring Boot(4)常用配置

  • Spring boot默认是/ ,这样直接通过http://ip:port/就可以访问到index页面,如果要修改为http://ip:port/path/ 访问的话,那么需要在Application.properties文件中加入server.context-path = /你的path,比如:spring-boot,那么访问地址就是http://ip:port/spring-boot 路径
server.context-path=/spring-boot
  • 修改端口
server.port=9090
  • 定时任务
    /**
     *
     *
     */
     @Configuration
     @EndableScheduling
     class ScheduleConfig{
         @Scheduled(corn="0/10****?")//每20秒执行一次
         public void scheduler(){
             System.out.println("******定时任务******");
         }

     }
  • Spring Boot在编译的时候,是有默认JDK版本的,如果我们期望使用我们要的JDK版本的话,那么要怎么配置呢?这个只需要修改pom.xml文件的 – 加入一个plugin即可
<plugin>
   <artifactId>maven-compiler-plugin</artifactId>
   <configuration>
      <source>1.8</source>
      <target>1.8</target>
   </configuration>
</plugin>

添加了plugin之后,需要右键Maven à Update Projects,这时候你可以看到工程根目录下的JRE System Library 版本更改了。

  • 处理静态资源
    Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。

默认资源映射

我们在启动应用的时候,可以在控制台中看到如下信息:

handler.SimpleUrlHandlerMapping  : Mapped URL path [/test/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

其中默认配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources)
其中默认配置的 /webjars/** 映射到 classpath:/META-INF/resources/webjars/
PS:上面的 static、public、resources 等目录都在 classpath: 下面(如 src/main/resources/static)。

如果我按如下结构存放相同名称的图片,那么Spring Boot 读取图片的优先级是怎样的呢?

如下图:

这里写图片描述

优先级顺序为:META/resources > resources > static > public

自定义资源映射

上面介绍了Spring Boot 的默认资源映射
这些资源都是打包在jar包中的,然后实际应用中,我们还有很多资源是在管理系统中动态维护的,并不可能在程序包中,对于这种随意指定目录的资源,如何访问?

以增加 /test/* 映射到 classpath:/test/* 为例的代码处理为:
实现类继承 WebMvcConfigurerAdapter 并重写方法 addResourceHandlers

import org.springboot.sample.interceptor.MyInterceptor1;
import org.springboot.sample.interceptor.MyInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MyWebAppConfigurer
        extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/test/**").addResourceLocations("classpath:/test/");
        super.addResourceHandlers(registry);
    }
   }

访问test 文件夹中的test.jpg 图片的地址为 http://localhost:8080/test/test.jpg

这样使用代码的方式自定义目录映射,并不影响Spring Boot的默认映射,可以同时使用。
如果我们将/myres/* 修改为 /* 与默认的相同时,则会覆盖系统的配置,可以多次使用 addResourceLocations 添加目录,优先级先添加的高于后添加的。

其中addResourceLocations的参数是动参,可以这样写

registry.addResourceHandler("/test/**")
        .addResourceLocations(“classpath:/test1/”, “classpath:/test2/");

访问访问test1、test2文件夹中的资源 http://localhost:8080/test/{资源文件名}

如果我们要指定一个绝对路径的文件夹(如 D:/data/api_files ),则只需要使用 addResourceLocations 指定即可。

// 可以直接使用addResourceLocations 指定磁盘绝对路径,同样可以配置多个位置,注意路径写法需要加上file:

registry.addResourceHandler("/test/**").addResourceLocations("file:D:/data/test);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值