SpringBoot的基础配置

问题导入
  • 入门案例中没有引入spring-webmvc等依赖包,没有配置Tomcat服务器,为什么能正常启动?
  • 我们没有配置端口号,为什么端口是8080?
起步依赖
  • starter

    • SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
  • parent

    • 所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
    • spring-boot-starter-parent(2.5.0)与 spring-boot-starter-parent(2.4.6)共计57处坐标版本不同
  • 实际开发

    • 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
    • 如发生坐标错误,再指定version(要小心版本冲突)
<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>${junit.version}version>
dependency>
<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>javax.servlet-apiartifactId>
    <version>${servlet-api.version}version>
dependency>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.5.0version>
    parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
project>

默认配置

tomcat默认配置了端口号为8080

SpringBoot的基础配置_第1张图片

基础配置

1. 配置文件格式

问题导入

框架常见的配置文件有哪几种形式?

1.1 修改服务器端口

http://localhost:8080/books/1 >>> http://localhost/books/1

SpringBoot提供了多种属性配置方式

  • application.properties
server.port=80
  • application.yml
server:
  port: 81
  • application.yaml
server:
  port: 82
1.2 自动提示功能消失解决方案

操作步骤:

SpringBoot的基础配置_第2张图片

SpringBoot的基础配置_第3张图片

SpringBoot的基础配置_第4张图片

SpringBoot的基础配置_第5张图片

SpringBoot的基础配置_第6张图片

SpringBoot的基础配置_第7张图片

1.3 SpringBoot配置文件加载顺序(了解)
  • application.properties > application.yml > application.yaml

注意事项:

  1. SpringBoot核心配置文件名为application
  2. SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性

2. yaml

问题导入

什么是yaml,和properties有什么区别?

  • YAML(YAML Ain’t Markup Language),一种数据序列化格式
  • 优点:
    • 容易阅读
    • 容易与脚本语言交互
    • 以数据为核心,重数据轻格式
  • YAML文件扩展名
    • .yml(主流)
    • .yaml
2.1 yaml语法规则
  • 大小写敏感
  • 属性层级关系使用多行描述,每行结尾使用冒号结束
  • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
  • #表示注释
  • 核心规则:数据前面要加空格与冒号隔开
2.2 yaml读取数据三种格式

1、@Value(直接读取)

实例:

yml文件:

enterprise:
  name: itcast
  age: 18
#  subject表示一个数组
  subject:
    - Java
    - Python
    - 大数据
@Value("${enterprise.name}")
private String name;
@Value("${enterprise.age}")
private Integer age;
@Value("${enterprise.subject[0]}")
private String subject;

2、Environment(封装后读取)

    @Autowired
    private Environment environment;

    @GetMapping("/select")
    public String getUser(){
        System.out.println(environment.getProperty("enterprise.name"));
        System.out.println(environment.getProperty("enterprise.age"));
        System.out.println(environment.getProperty("enterprise.subject[0]"));
        return "查询用户成功";
    }

3、实体类封装属性(封装后读取)

定义一个实体类,写上@ConfigurationProperties注解

@Component//交给Spring管理
@ConfigurationProperties(prefix = "enterprise")

public class Enterprise {

    private String name;
    private Integer age;

    private String[] subject;




    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }



    public String[] getSubject() {
        return subject;
    }

    public void setSubject(String[] subject) {
        this.subject = subject;
    }
}

将该实体类注入后通过get方法即可输出

@Autowired
private Enterprise enterprise;

@GetMapping("/select")
public String getUser(){
    System.out.println(enterprise.getName());
    System.out.println(enterprise.getAge());
    System.out.println(enterprise.getSubject()[0]);
    return "查询用户成功";
}

注:自定义对象封装数据警告解决方案

在这里插入图片描述

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-configuration-processorartifactId>
    <optional>trueoptional>
dependency>

你可能感兴趣的:(SpringBoot,spring,boot,后端,java)