Spring Boot笔记

文章目录

一、Spring Boot入门

1、Spring Boot简介

简化Spring应用开发的一个框架;
整个Spring技术栈的个大整合;
J2EE开发的一站式解决方案;

2、微服务

2014,martin fowler
微服务:架构风格(服务微化)
一个应用应该是一组小型服务,可以通过HTTP的方式进行互通;

每一个功能元素最终都是一个可独立替换和独立升级的软件单元;

详细参照微服务文档:[链接](https://martinfowler.com/articles/microservices.html#MicroservicesAndSoa)

3、环境准备

MAVEN设置:

3.1、MAVEN设置

给maven的setting.xml配置文件添加

<profiles>
   <id>jdk-1.8</id>  
                <activation>  
                    <activeByDefault>true</activeByDefault>  
                    <jdk>1.8</jdk>  
                </activation>  
                <properties>    
                    <maven.compiler.source>1.8</maven.compiler.source>    
                    <maven.compiler.target>1.8</maven.compiler.target>    
                    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>    
                </properties>  
   </profiles>
3.2、IDEA设置

在这里插入图片描述

4、Spring Boot HelloWorld

一个功能:
	浏览器发送hello请示,服务器接受请求并处理,响应Hello World字符串;
4.1、创建一个maven工程(jar)
4.2、导入spring boot相关依赖
在pom.xml文件中
 <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.0.4.RELEASE</version>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
4.3、编写一个主程序(用来启动spirng boot程序)
/**
 *@SpringBootApplication 标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        
        //启动Spring应用
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}
4.4、编写相关的Controller、Service
@Controller
public class HelloController {
    
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!";
    }
}
4.5、运行主程序测试
4.6、简化部署
 <!--这个插件,可以将应用打包成一个可执行的jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </plugin>
        </plugins>
    </build>  
    <!--导入这个插件后,可以直接使用 java -jar 的dos命令将这个应用打成jar包。-->

5、Hello World探究

5.1、PM文件
5.1.1、父项目
 <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.0.4.RELEASE</version>
  </parent>
//他的父项目是:
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>
    </dependencies>
    //他来真正管理Sprig Boot应用里所有的依赖版本

Spring Boot的版本仲裁中心;以后我们导入依赖默认是不需要写版本的;
(没有在dependencies里面管理的依赖自然需要声明版本号)

5.1.2、导入的依赖
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

spring-boot-start-web
spring-boot:spring-boot 场景启动器;帮我们导入了web模块正常运行所依赖的组件;

Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器。

5.2、主程序类,主入口类
/**
 *@SpringBootApplication 标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {

        //启动Spring应用
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

@SpringBootApplication 标注的类,说明该类是Spring Boot的主配置类,Spring Boot就应该运行该类的main()方法来运行Spring Boot应用;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
		@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

@SpringBootConfiguration:Spring Boot的配置类;
标注在某个类上,表示这是一个Spring Boot的配置类;
@Configuration:配置类上来标注这个注解

@EnableAutoConfiguration:开启自动配置功能;

6、使用Spring Initializer快速创建Spring Boot项目

IDEA都支持使用Spring Boot的项目创建向导快速创建一个Spring Boot项目;

选择我们需要的模块;向导会联网创建Sping Boot项目

默认生成的Spinrg Boot项目:

  • 主程序已经生成好了,我们只需要写我们自己的逻辑
  • resources文件夹中目录结构:
    ▲static:保存所有的静态资源;(js、css、images…)
    ▲templates:保存所有模板页面;(Sping Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP);可以使用模拟引擎(freemarker、thymeleaf)
    ▲application.properties:Spring Boot应用的配置文件;可以修改一些默认设置

二、配置文件

1、配置文件

--Spring Boot使用一个全局的配置文件,配置文件名称固定
  • application.propertions
  • application.yml

配置文件的作用:修改Spring Boot自动配置的默认值;

YAML(YAML Ain’t Markup language)
····YAML A Markup Language:是一个标记语言
····YAML isn’t Markup Language:不是一个标记语言

标记语言:
····以前的配置文件,大多都使用 xxxx.xml文件;
····YAML:以数据为中心,比 JSON、xml等更适合做配置文件
····YAML:配置实例

server:
  port: 8090

····XML:

<server>
	<port>8090</port>
</server>

2、YAML语法:

2.1、基本语法

k(空格)v;//表示一对键值(空格必须有);

空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级

server:
    port:8090
    path:/hello

属性和值都是大小写敏感的;

2.2、值的写法

字面量:普通的值(数字、字符串、布尔值)
  k:v:字面直接来写;
    字符串默认不用加上单引号或双引号
    “”:双引号,不会转义字符串里面的特殊字符;特殊字符会作为本身想表达的意思
eg:
  name:“小明 \n list” 输出:小明
                list

    ‘’:单引号,特殊字符最终转义为一个普通的字符串输出
eg:
  name:“小明 \n list” 输出:小明 \n list

对象 、Map(属性和值)(键值对)
  k:v:在下一行写对象的属性和值的关系;注意缩进

friends:
	lastName:张三
	age:20

  行内写法:

friends:{lastName:张三,age:20}
数组(List、Set):

  用 - 值表示数组中一个元素

pets:
 - cat
 - dog
 - pig

  行内写法

pets:[cat,dog,pig]
2.3、配置文件值注入

  配置文件

person:
  lastName: 小明
  age: 18
  boos: 老张
  birthday: 2000/1/1
  maps: {k1: v1,k2: v2}
  lists:
     - 李四
     - 小王
  dog:
    name: 花花
    age: 1

  JavaBean:

/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties : 告诉Spring Boot将本类中的所有属性和配置文件中相关的配置进行绑定
 *      prefix = "person":配置文件中哪个下面的的所有属性进行一一映射
 *      只有这个组件是窗口中的组件,才能使用容器提供的 @ConfigurationProperties 功能
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birthday;

    private Map<String,Object> maps;
    private List<String> lists;
    private Dog dog;

  我们可以导入配置文件处理器,方便以后编写配置文件有代码提示

	<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

3、配置文件值注入

3.1、properties配置文件在idea中默认utf-8,可能会乱码,在IDEA中进行设置解决

在这里插入图片描述

3.2、@Value获取值和@ConfigurationProperties获取值比较
@Value@ConfigurationProperties
功能一个个属性注入批量注入配置文件中的属性
松散绑定(松散语法)不支持支持
SpEL支持不支持
JSR303数据校验不支持支持
复杂类型封装不支持支持

配置文件yml和properties都能获取到值;

如果说,在某个业务逻辑中只需要获取一下配置文件中的某项值,使用@Value;

如果说,专门编写了一个javaBean来和配置文件进行映射,则直接使用@ConfigurationProperties

3.3、配置文件注入值数据检验
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {

    /**
     * <bean class="Person">
     *      <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
     * <bean/>
     */

   //lastName必须是邮箱格式,不是 则报错
    @Email
    //@Value("${person.last-name}")
    private String lastName;
    //@Value("#{11*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;

    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
3.4、@PropertySource、@ImportResource

@PropertySource:加载指定的配置文件;

/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties : 告诉Spring Boot将本类中的所有属性和配置文件中相关的配置进行绑定
 *      prefix = "person":配置文件中哪个下面的的所有属性进行一一映射
 *      只有这个组件是窗口中的组件,才能使用容器提供的 @ConfigurationProperties 功能
 *
 * @Value 一样可以从配置文件中获取值
 */
@PropertySource("classpath:person.properties")
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {
   // @Value("${person.lastName}")
    private String lastName;
    //@Value("#{10*2-1}")
    private Integer age;
    //@Value("true")
    private Boolean boss;
    //@Value("2000/10/20")
    private Date birthday;
    private Map<String,Object> maps;
    private List<String> lists;
    private Dog dog;

@ImportResource:导入Spring的配置文件,让配置文件的内容生效;

Spring Boot里没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;

若要Spring的配置文件生效,就需要@ImportResource注解,标注在一个配置类上;

@ImportResource(locations = "classpath:beans.xml")//导入Spring的配置文件,让其生效

不来编写Sprin的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="helloService" class="com.example.springboot02config.service.HelloService"></bean>
</beans>

Spring Boot推荐的给容器中添加组件的方式:使用全注解的方式
1、配置类====Spring 配置文件

2、使用 @Bean 给容器中添加组件

 * @Configuration 指明当前类是配置类,替代之前的Spring配置文件
 *
 * 在配置文件中用 <bean></bean> 标签添加组件
 *
 * 在配置类中用 @Bean 添加组件
 */
@Configuration
public class MyAppConfig {

    //将方法的返回值添加到容器中,容器中这个组件默认的ID就是方法名
    @Bean
    public HelloService helloService(){
        System.out.println("配置类 @Bean 给容器中添加组件了!");
        return new HelloService();
    }
}

4、配置文件占位符

4.1、随机数
random.value、{random.int}、${random.long}
random.int(10){random.int[1024,2048]}
4.2、占位符获取之前配置的值,如果没有可以使用 : 指定默认值
#person.lastName=张三
#person.age=${random.int}
#person.boss=false
#person.birthday=2000/12/20
#person.maps.k1=v1
#person.maps.k2=v2
#person.lists=a,b,c
#person.dog.name=${person.hello:hello_dog}
#person.dog.age=2

5、Profile

5.1、多Profile文件

在主配置文件编写的时候,文件名可以是:application-{profile}.properties/.yml
默认使用application.properties的配置

5.2、yml支持多文档块方式
server:
  port: 8090

#激活指定端口配置
spring:
  profile:
    active: prod
---
server:
  port: 8093
spring:
  profile: dev
---
server:
  port: 8094
spring:
  profile: prod #指定属于哪个环境
---
5.3、激活指定profile
5.3.1、在配置文件中指定要激活的配置文件
server.port=8090
spring.profiles.active=dev #指定要激活的配置文件
5.3.2、命令行:
--spring.profiles.active=dev
5.3.3、JVM(虚拟机)参数
-Dspring.profiles.active=dev

6、配置文件加载位置

SpringBoot启动会扫描以下位置的application.properties或者application.yml文件作为Spring Boot的默认配置文件(注:优先级从上到下,由高到低,高优先级的会覆盖低优先级的配置)

—file:…/config/
—file:…/
—classpath:/config/
—classpath:/

SpringBoot会从这四个位置全部加载配置文件,互补配置;

还可以通过spring.config.location来改变默认的配置文件位置:

项目打包好以后,可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置,指定配置文件和默认加载的这些配置文件共同互补作用于项目

7、外部配置加载顺序

SpringBoot也可以从以下位置加载配置:优先级从高到低,高优先级配置覆盖低优先级配置,所有的配置会形成互补配置

1、命令行参数

多个配置用空格分开;–配置项=值

2、来自java:comp/env的JNDI属性

3、Java系统属性(System.getProperties())

4、操作系统环境变量

5、RandomValuePropertySource配置的random.*属性值

由jar包外向jar包内进行寻找,优先加载带profile的,再来加载不带profile的

6、jar包外部的application-{profile}.properties 或 application.yml(带spring.profile)的配置文件

7、jar包内部的application-{profile}.properties 或 application.yml(带spring.profile)的配置文件

8、jar包外部的application.properties或application.yml(不带spring.profile)的配置文件

9、jar包内部的application.properties或application.yml(不带spring.profile)的配置文件

10、@Configuration注解类上的@PropertySource

11、通过SpringApplication.setDefaultProperties指定的默认属性

8、自动配置原理

8.1、自动配置原理:

1)、Spring Boot启动的时候加载主配置类,开启了自动配置功能 @EnableAutoConfiguration

2)、@EnableAutoConfiguration作用:
——利用EnableAutoConfigurationImportSelect给容器中导入一些组件
——可以查看selectImports()方法的内容

3)、每一个自动配置类进行自动配置功能

4)、以HttpEncodingAutoConfiguration(Http编码自动配置)为例解释自动配置原理

@Configuration(
    proxyBeanMethods = false
)  //表示这是一个配置类,可以给容器中添加组件
@EnableConfigurationProperties({ServerProperties.class}) //启用ConfigurationProperties功能,将配置文件中对应的值和ServerProperties绑定起来
@ConditionalOnWebApplication(
    type = Type.SERVLET
)  //Spring底层的注解,根据不同的条件可以进行判断,如果满足,整个配置类里面的配置就会生效
@ConditionalOnClass({CharacterEncodingFilter.class})  //判断当前项目有没有这个类
@ConditionalOnProperty(
    prefix = "server.servlet.encoding",
    value = {"enabled"},
    matchIfMissing = true
)  //判断配置文件中是否存在某个配置
public class HttpEncodingAutoConfiguration {
 private final Encoding properties;

    public HttpEncodingAutoConfiguration(ServerProperties properties) {
        this.properties = properties.getServlet().getEncoding();
    }

    @Bean	//给容器中添加一个组件,这个组件的某些值需要从properties中获取
    @ConditionalOnMissingBean
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
        filter.setEncoding(this.properties.getCharset().name());
        filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.web.servlet.server.Encoding.Type.REQUEST));
        filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.web.servlet.server.Encoding.Type.RESPONSE));
        return filter;
    }

根据当前不同的条件判断,决定这个类是否生效
一旦该配置类生效,该配置类就会给容器中添加各种组件,这些组件的属性是从对应的properties类中获取的,这些类里的每个属性又和配置文件对应绑定

5)、所有在配置文件中能配置的属性都是在 xxxx.properties类中封装着,配置文件能配置什么就可以参数该功能对应的属性类

@ConfigurationProperties(
    prefix = "server",
    ignoreUnknownFields = true
)  //从配置文件中获取指定的值进行绑定
public class ServerProperties {

6)、Spring Boot精髓
——(1)、Spring Boot启动会加载大量自动配置类
——(2)、看所需要的功能Spring Boot中有没有默认写好
——(3)、看自动配置类中具体配置了哪些组件(只要我们需要的组件都有,就不需要配置了)
——(4)、给容器中自动配置类添加组件时,会从properties类中获取某些属性。可以在配置中进行指定这些属性的值

8.2、细节:
8.2.1、@Conditional派生注解

作用:必须是@Conditional指定的条件成立,才给容器中添加组件,配置类里面的所有内容才生效在这里插入图片描述
自动配置类必须在一定的条件下才能生效
可以通过 debug=true 来打印自动配置报告,方便了解哪些自动配置类生效

#开启Spring Boot的debug模式
debug=true

server.port=8080
============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:(自动配置类启用的)
-----------------

   AopAutoConfiguration matched:
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)
      
Negative matches:(自动配置类没启用的)
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

三、Spring Boot与日志

1、日志框架

市面上的日志框架:
JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j……
在这里插入图片描述
左边选一个日志门面(抽象层)、右边选一个实现
日志门面:SLF4j
日志实现:Logback

SpringBoot:底层是Spring框架,默认日志框架是JCL
SpringBoot选用SLF4jLogback

2、SLF4j使用

2.1、如何在系统中使用SLF4j

日志记录方法的调用:先调用日志抽象层里的方法
给系统里面导入slf4j和logback的实现jar包

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

图示
在这里插入图片描述
第一个日志的实现框架都有自己的配置文件,使用slf4j以后,配置文件还是做成日志实现框架本身的配置文件

2.2、遗留问题

统一日志记录
在这里插入图片描述
将系统中所有的日志都统一到slf4j:
1、将系统中其它日志框架先排除出去
2、用中间包替换原有的日志框架
3、导入slf4j其它的实现

2.3、SpringBoot日志关系
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

SpringBoot使用它来做日志功用

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-logging</artifactId>
      <version>2.4.2</version>
      <scope>compile</scope>
</dependency>

底层依赖关系:
在这里插入图片描述
1)、SpringBoot底层也是使用slf4j+logback的方式进行日志记录
2)、SpringBoot也把其它的日志替换为slf4j
3)、中间转换包
在这里插入图片描述
4)、SpringBoot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志。如果要引入其它框架 ,一定要把这个框架的默认日志依赖移除掉。

2.4、日志使用
2.4.1、默认配置

SpringBoot默认配置好了日志

  //日志记录器
    Logger logger = LoggerFactory.getLogger(getClass());
    @Test
    void contextLoads() {
        //日志的级别,由低到高 trace<debug<info<warn<error
        //可以调整需要输出的日志级别,日志就从该级别开始到高级别生效
       logger.trace("这是trace日志……");
       logger.debug("这是debug日志……");
       //SpringBoot的日志默认从info级别开始
       logger.info("这是info日志……");
       logger.warn("这是warn日志……");
       logger.error("这是error日志……");
    }

SpringBoot修改日志的默认配置

#标识日志从trace级别开始生效
logging.level.com.example=trace  

#指定日志文件名
#logging.file.name=springboot.log

#指定日志文件路径
#在当前磁盘的根路径下创建springLog文件夹和里面的log文件夹;使用spring.log作为默认日志文件
logging.file.path=/springLog/log

#在控制台输出日志
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n

#指定文件中日志输出的格式
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} === %msg%n
2.4.2、指定配置

在类路径下放上每个日志框架自己的配置文件,SpringBoot就不使用默认的日志配置文件
在这里插入图片描述
logback.xml:直接就被日志框架加载
logback-spring.xml:日志框架不直接加载,由SpringBoot解析日志配置,可以使用SpringBoot的高级Profile功能

2.5、切换日志

可以按照slf4j的日志适配图,进行相关的切换

四、Spring Boot与Web开发

4.1、简介

自动配置:
这个场景SpringBoot帮我们配置了什么?能不能修改?能修改哪些?xxx

xxxAutoConfiguration:给容器中自动配置组件
xxxProperties:配置来自封装配置文件的内容

4.2、SpringBoot对静态资源的映射规则

@Deprecated
@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties extends Resources {	
//可以设置和静态资源有关的参数
        @Bean
        @Primary
        public RequestMappingHandlerMapping requestMappingHandlerMapping(@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager, @Qualifier("mvcConversionService") FormattingConversionService conversionService, @Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {
            return super.requestMappingHandlerMapping(contentNegotiationManager, conversionService, resourceUrlProvider);
        }

        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            super.addResourceHandlers(registry);
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
            } else {
                ServletContext servletContext = this.getServletContext();
                this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
                this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
                    registration.addResourceLocations(this.resourceProperties.getStaticLocations());
                    if (servletContext != null) {
                        registration.addResourceLocations(new Resource[]{new ServletContextResource(servletContext, "/")});
                    }

                });
            }
        }
        @Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
            WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
            welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
            welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
            return welcomePageHandlerMapping;
        }

1)、所有 /webjars/**,都去 classpath:/META-INF/resources/webjars/ 找资源
webjars都以jar包的方式导入静态资源,https://www.webjars.org/

"http://127.0.0.1:8080/webjars/jquery/3.5.1/jquery.js

 <!--引入jQuery的webjar--><!--访问时只需要写webjars里资源的路径、名称就行-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.5.1</version>
        </dependency>

2)、“ /** ”访问当前项目的任意资源(静态资源的文件夹)

"classpath":/META-INF/resource/",
"classpath:/resource/",
"classpath:/static/",
"/":当前项目的根路径

3)、欢迎页:静态资源文件夹下的所有 index.html 页面,被“ /** ” 映射
http://127.0.0.1:8080/

4)、所有的 “ **/favicon.ico ” 都在静态资源文件夹找

……待后接实战示例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值