文章目录
  1. 1. SpringBoot
    1. 1.1. SpringBoot官方定义
    2. 1.2. SpringBoot学习
      1. 1.2.1. SpringBoot的启动
      2. 1.2.2. SpringBoot的静态资源访问
      3. 1.2.3. SprinBoot的配置文件

SpringBoot

SpringBoot官方定义

​ Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

​ We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Spring Boot使创建独立的、基于生产级Spring的应用程序变得很容易,您可以“直接运行”这些应用程序。

我们对Spring平台和第三方库有自己的见解,这样您就可以轻松入门了。大多数Spring引导应用程序只需要很少的Spring配置。

SpringBoot学习

SpringBoot的启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* SpringBoot 启动类
* 注意:
* 启动器存放的位置。启动器可以和 controller位于同一个包下,或者位于 controller的上一级 包中,
* 但是不能放到 controller的平级以及子包下。
* @author Administrator
*
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
//启动SpringBoot
SpringApplication.run(App.class, args);
}
}

SpringBoot的静态资源访问

​ 源码分析

​ org.springframework.boot.autoconfigure.web.ResourceProperties

1
2
3
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };

因此,在类路径下的这几个目录都可以访问到静态资源

注意1:

​ 在maven中的src/main下建立webapp目录,可以访问静态资源

分析:

​ eclipse编译会产生一个target/classes目录,有eclipse中.classpath文件指定编译输出目录

​ output=”target/classes”

​ maven中则将所有的资源打成一个jar或者war包,如果是jar包,则jar目录结构和eclipse中

​ 中的target/classes下的目录结构相同,由项目中的包文件以及一个META-INF文件夹组成

​ 用法:

​ 可以使用Maven中的资源打包插件,将src/main/webapp下的静态资源打包至META-INF/resources

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<!-- 开启过滤,用指定的参数替换directory下的文件中的参数 -->
<filtering>true</filtering>
</resource>

<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>

注意2:

​ 在有多个目录下有相同的静态资源,则优先级顺序优先级序

​ /META-INF/resources > resources > static > public

注意3:

​ 静态资源路径也可以通过配置文件指定

1
spring.resources.static-locations=指定路径

#修改默认的静态资源存放目录,多个路径可以用逗号隔开

spring.resources.static-locations=classpath:/

修改后,默认路径无效

注意4: 访问时使用相对路径,

​ 如果使用绝对路径static,public等默认是不计入路径的

参考:https://blog.csdn.net/qq_34797335/article/details/80194137

SprinBoot的配置文件

SpringBoot配置文件支持yml格式和properties格式,默认命名为

​ application.properties

​ application.yml

两种配置文件选择哪一种都可以

配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好;

yml全称YAML, YAML(Yet Another Markup Language)(发音 /ˈjæməl/ ), 一种基于Unicode

容易阅读,容易和脚本语言交互的,用来表达资料序列的编程语言。yaml语言广泛用于书写配置文件。

yml主要特点参考:https://www.cnblogs.com/geeklove01/p/8284236.html

注:

​ 配置文件也可以不使用默认的配置名称,只需要在启动类中手动读取配置文件即可

1
2
3
4
5
6
7
8
9
10
@SpringBootApplication
public class SpringbootConfigApplication {

public static void main(String[] args) {
//变更配置文件读取位置启动
new SpringApplicationBuilder(SpringbootConfigApplication.class). properties("spring.config.location=classpath:/springbootconfig.properties").run(args);
//读取默认配置文件启动
SpringApplication.run(SpringbootConfigApplication.class, args);
}
}
文章目录
  1. 1. SpringBoot
    1. 1.1. SpringBoot官方定义
    2. 1.2. SpringBoot学习
      1. 1.2.1. SpringBoot的启动
      2. 1.2.2. SpringBoot的静态资源访问
      3. 1.2.3. SprinBoot的配置文件