您的位置:首页 > 编程语言 > Java开发

SpringBoot(5):静态资源管理,服务器启动加载数据

2018-01-25 14:44 555 查看
静态资源管理

SpringBoot有默认的静态资源处理,我们可以通过WebMvcAutoConfiguration来配置各种属性。

默认的配置已经足够让我们平常使用了,如果有特殊要求则可以通过配置进行修改。

如果想要自己完全控制WebMVC,就需要在@Configuration注解的配置类上增加@EnableWebMvc(@SpringBootApplication 注解的程序入口类已经包含@Configuration),增加该注解以后WebMvcAutoConfiguration中配置就不会生效,你需要自己来配置需要的每一项。这种情况下的配置还是要多看一下WebMvcAutoConfiguration类。

我们既然是快速使用Spring Boot,并不想过多的自己再重新配置。本文还是主要针对Spring Boot的默认处理方式,部分配置在application 配置文件中(.properties 或 .yml)
其中默认配置的 /** 映射到 /static (或/public、/resources、/META-INF/resources)

也就是说,如果你有一个资源,放在这几个文件夹中的几个或一个都是可以访问到的。

如果还有别的需求的话就可以自定义,

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

package org.springboot.sample.config;

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("/mkdlpres/**").addResourceLocations("classpath:/
mkdlpres/");
super.addResourceHandlers(registry); }}


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

// 可以直接使用addResourceLocations 指定磁盘绝对路径,同样可以配置多个位置,注意路径写法需要加上file:
registry.addResourceHandler("/myimgs/**").addResourceLocations("file:H:/myimgs/");


通过配置文件配置

上面是使用代码来定义静态资源的映射,其实Spring Boot也为我们提供了可以直接在 application.properties(或.yml)中配置的方法。 

配置方法如下:
# 默认值为 /**
spring.mvc.static-path-pattern=
# 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
spring.resources.static-locations=这里设置要指向的路径,多个使用英文逗号隔开,
使用 spring.mvc.static-path-pattern 可以重新定义pattern,如修改为 /mkdlpres/** ,则访问static 等目录下的资源文件应该为 http://localhost:8080/myres/xxx ,修改之前为 http://localhost:8080/xx
使用 spring.resources.static-locations 可以重新定义 pattern 所指向的路径,支持 classpath: 和 file: (上面已经做过说明) 
注意 spring.mvc.static-path-pattern 只可以定义一个,目前不支持多个逗号分割的方式。


页面中使用

上面几个例子中也已经说明了怎么访问静态资源,其实在页面中使用不管是jsp还是freemarker,并没有什么特殊之处,也我们平时开发web项目一样即可。 

下面是我的index.jsp:
<body>
<img alt="读取默认配置中的图片" src="${pageContext.request.contextPath }/pic.jpg">
<br/>
<img alt="读取自定义配置myres中的图片" src="${pageContext.request.contextPath }/myres/fengjing.jpg">
</body>

启动加载数据
当启动服务器的时候我们要加载数据的话,在SpringBoot中通过实现接口CommandLineRunner即可。无需别的配置。

Spring Boot应用程序在启动后,会遍历CommandLineRunner接口的实例并运行它们的run方法。也可以利用@Order注解(或者实现Order接口)来规定所有CommandLineRunner实例的运行顺序。

下面写两个类实现CommandLineRunner.

MyStartUpRunner1.java

package com.springboot.study.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value=1)
public class MyStartUpRunner1 implements CommandLineRunner{

@Override
public void run(String... arg0) throws Exception {
System.out.println("================>服务器启动,加载数据!(1)");
}

}
MyStartUpRunner2.java
package com.springboot.study.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(value=2)
public class MyStartUpRunner2 implements CommandLineRunner{

@Override
public void run(String... arg0) throws Exception {
System.out.println("================>服务器启动,加载数据!(2)");
}

}
然后运行程序,就可以看到运行的顺序了。
================>服务器启动,加载数据!(1)
================>服务器启动,加载数据!(2)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: