Spring Boot

  1. <!-- -->
    sprint-boot-starter-parent
    <!-- 场景启动器;帮助导入web等模块正常运行所依赖的组件-->
    spring-boot-starter
    spring-boot-starter-web
  2. <!-- @SpringBootApplication标注这个类是SpringBoot的主配置类,SpringBoot运行这个类的main方法来启动SpringBoot应用-->

Spring Boot配置文件

application.properties和application.yml都是全局配置文件

yaml语法

#1.字符串
    1.1 双引号""
    1.2 单引号'',表示忽略里面的转义字符,是什么则输出什么
#2.对象
    friends:
        lastName: zhangsan
        age: 18
    friends: {lastName: zhangsan,age: 18}
#3.数组(List,Set)
    pets:
    -cat
    -dog
    -pig
    pets: [cat,dog,pig]

静态资源

优先级 : resources>static(默认)>public

配置文件

  • springboot启动时会加载大量的自动配置类

  • 我们需要的功能有没有springboot默认写好的自动配置类

  • 我们看看这个自动配置类使用类哪些组件

  • 给容器中自动配置类添加组件, 会从properties类中获取某些属性,我们就可以在配置文件中指定这些属性的值

  • xxxAutoConfiguration : 自动配置类

    给容器添加组件

    xxxProperties : 封装配置文件中的相关属性

配置文件

注意:IDEA 中的插件(yaml和properties自动转换)存在一点小bug,涉及到复杂对象的List和Map生成会当做简单对象处理,因此生成的配置文件可能出现不能正常运行的情况。

import lombok.Data;

@Data
public class Cat {
    private String name;
    private Integer age;
}
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.*;

@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    // 基本数据类型
    private int id;
    // 引用类型
    private String name;
    // 时间类型
    private Date birthday;
    // 自定义类型
    private Cat cat;

    // 集合类型
    private List<Person> friends = new ArrayList<>();

    private Map<String, Cat> attributes = new HashMap<>();

}

Properties 表示复杂对象

person.id=1
person.name=root
person.birthday=2023/6/5 12:12:12
person.cat.name=kitty
person.cat.age=2

# List列表中的每个元素都是一个复杂对象
person.friends[0].id=2
person.friends[0].name=admin
person.friends[0].birthday=2021/6/5 12:12:12

person.friends[1].id=3
person.friends[1].name=joker
person.friends[1].birthday=2020/6/5 12:12:12

# Map字典结构, 其中cat01和cat02作为字典的key
person.attributes.cat01.name=c1
person.attributes.cat01.age=2

person.attributes.cat02.name=c2
person.attributes.cat02.age=3

YAML 表示复杂对象

person:
  id: 1
  name: root
  birthday: 2023/6/5 12:12:12

  cat:
    age: 2
    name: kitty

  # 列表中是一个复杂对象
  friends:
    - id: 2
      name: admin
      birthday: 2021/6/5 12:12:12
    - id: 3
      name: joker
      birthday: 2020/6/5 12:12:12

  # 字典的key作为一个自定义的字段, value作为一个复杂对象, List可以认为使用"-"来作为匿名key
  attributes:
    cat01:
      age: 2
      name: c1
    cat02:
      age: 3
      name: c2

日志配置

项目中不使用 System.out.printIn() 进行输出,而是使用日志进行输出。

SpringBoot 中默认使用的日志框架是 Logback,但日志并不是使用XXXAutoConfiguration自动配置的机制实现,而是使用的监听器机制。自动配置原理是在系统启动完成之后,再向 Spring 容器中放入组件,但是日志同时需要记录系统启动过程。

日志门面(接口) 日志实现
JCL
Slf4j Log4j2、Logback

debug模式

在配置文件中debug=true

日志选择

日志门面(抽象层) : SLF4J

日志实现 : Logback

SpringBoot对静态资源的映射规则

WebAutoConfiguration

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
    } else {
        Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
        CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
        if (!registry.hasMappingForPattern("/webjars/**")) {
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

        String staticPathPattern = this.mvcProperties.getStaticPathPattern();
        if (!registry.hasMappingForPattern(staticPathPattern)) {
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

    }
}

所有的/webjars/**, 都去classpath://META_INF/resources/webjars/找资源

http://localhost:8080/webjars/jquery/3.5.0/jquery.js

在访问的时候只需要写webjars下面资源的名称即可

静态资源文件夹

ResourceProperties.java
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/",                                             "classpath:/static/",
"classpath:/public/" };
"/":当前项目的根路径

欢迎页: 静态资源文件夹下所有的index.html

在public文件夹下创建一个index.html文件, 则访问http://localhost:8080的时候默认访问index.html

引入模板引擎thymeleaf

只要我们把HTML页面放在classpath:/templates/ ,thymeleaf就能自动渲染

<html lang="en" xmlns:th="http://www.thymeleaf.org">

整合JDBC

springcloud

entities

主实体类

Json封装体CommonResult


   转载规则


《》 熊水斌 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
优点 控制反转IOC 面向切面编程AOP 轻量级, 非入侵 IOC理论推导 UserDao接口 UserDaoImpl实现类 UserService业务接口 UserServiceImpl业务实现类 原来的业务中,用户的需求可能会影响原
2023-02-04
下一篇 
ServletContext对象 对象在工程部署时创建, 在web工程停止时销毁 一个web工程只有一个ServletContext对象实例 ServletContext对象和不同的Servlet类的关系类似全局变量和函数的感觉 GET
2023-02-04
  目录