SpringBoot的基础配置

目录

SpringBoot快速搭建web程序

第一步:导包

第二步:配置SpringBoot引导类

 第三步:编写controller类

第四步:在SpirngBoot引导类中启动项目

起步依赖

SpringBoot基础配置

配置文件格式

yaml语法规则

读取yml配置文件的方法

 第一种:使用@Value注入

第二种:封装全部数据到Environment对象(SpringBoot自己封装的)

第三种:自定义对象封装指定数据

多环境配置开发 -配置文件

第一种:使用yml文件

第二种:使用properties文件

 本地启动SpringBoot项目

可以发现启动的这个程序是pro环境,那如何在启动的时候切换环境呢?

也可以临时改端口号

​编辑 Maven的pom.xml文件中配置多环境

配置文件分类


SpringBoot快速搭建web程序

第一步:导包

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
<!--    继承-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.10.RELEASE</version>
    </parent>
    <groupId>com.hhh</groupId>
    <artifactId>spring_day8_springBoot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

这里首先要继承SpringBoot

<!--    继承-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.10.RELEASE</version>
    </parent>

 然后导入自己想要的功能的依赖,这里要创建web程序,就导入web功能的依赖包,版本号不用指定

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

第二步:配置SpringBoot引导类

//SpringBoot引导类
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

 第三步:编写controller类

@RestController
@RequestMapping("/books")
public class Book {
    @RequestMapping("/save")
    public String save(){
        System.out.println("user save");
        return "hello";
    }
}

第四步:在SpirngBoot引导类中启动项目

启动成功

起步依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.10.RELEASE</version>
    </parent>

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

parent:所有SpringBoot项目要继承的项目,定义了若干个坐标版本号,以达到减少依赖冲突的目的 

SpringBoot基础配置

配置文件格式

 配置文件有三种格式分别为

  • application.properties
  • application.yml
  • application.yaml 

它们的文件名都是application,这是SpringBoot默认的文件名,这三种文件的优先级依次降低

验证:在这三个配置文件种都配置端口号,看启动的web程序的端口号是哪个

 结果:

可见properties格式的配置文件优先级最高,然后到yml文件(可以把properties注释自己去比较看看),最后是yaml文件 

yaml语法规则

  1. 大小写敏感
  2. 属性层级关系使用多行进行描述,每层结尾使用冒号(:)结束
  3. 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不可以使用tab键)
  4. 属性值前面要要添加一个空格,即属性名和属性值之间间隔一个冒号一个空格
  5. #表示注释 

读取yml配置文件的方法

people:
 name: hhh
 age: 19
 hobby:
  - sing
  - dance
  - rap
#数组的值前面要加一个-

 第一种:使用@Value注入

@RestController
@RequestMapping("/books")
public class Book {
    @Value("${people.name}")
    private String name;
    @Value("${people.age}")
    private Integer age;
    @Value("${people.hoppy[1]}")//取数组索引为1的值
    private String hobby;

    @RequestMapping("/save")
    public String save(){
        //System.out.println("user save");
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        System.out.println("hobby:"+hobby);
        return "hello";
    }

}

 

 这里也许有人会好奇,明明都没有导入外部配置文件,是怎么读取里面的数据的?

其实SpringBoot已经帮我们把名字为appliction的配置文件都导入加载了,所以我们可以直接读配置文件里的属性值

第二种:封装全部数据到Environment对象(SpringBoot自己封装的)

 注意:是这个包下的Environment类对象

org.springframework.core.env.Environment;
@RestController
@RequestMapping("/books")
public class Book {
    @Value("${people.name}")
    private String name;
    @Value("${people.age}")
    private Integer age;
    @Value("${people.hobby[1]}")//取数组索引为1的值
    private String hobby;
    @Autowired
    private Environment environment;

    @RequestMapping("/save")
    public String save(){
        //System.out.println("user save");
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        System.out.println("hobby:"+hobby);
        System.out.println("-----");
        System.out.println("name:"+environment.getProperty("people.name"));
        System.out.println("age:"+environment.getProperty("people.age"));
        System.out.println("hobby:"+environment.getProperty("people.hobby[0]"));
        return "hello";
    }
}

第三种:自定义对象封装指定数据

@Configuration//交给Spring管理
@ConfigurationProperties(prefix = "people")//与配置文件中的people属性进行映射绑定
public class People {
    private String name;
    private Integer age;
    private String[] hobby;

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", hobby=" + Arrays.toString(hobby) +
                '}';
    }

    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[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }
}

 这个类中使用了两个注解

@Configuration-->把这个类交给Spring管理

@ConfigurationProperties(prefix = "people")-->与yml配置文件中的people属性进行映射绑定

@RestController
@RequestMapping("/books")
public class Book {
    @Value("${people.name}")
    private String name;
    @Value("${people.age}")
    private Integer age;
    @Value("${people.hobby[1]}")//取数组索引为1的值
    private String hobby;
    @Autowired
    private Environment environment;
    @Autowired//根据类型自动注入
    private People people;

    @RequestMapping("/save")
    public String save(){
        //System.out.println("user save");
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        System.out.println("hobby:"+hobby);
        System.out.println("-----");
        System.out.println("name:"+environment.getProperty("people.name"));
        System.out.println("age:"+environment.getProperty("people.age"));
        System.out.println("hobby:"+environment.getProperty("people.hobby[0]"));
        System.out.println("----");
        System.out.println(people);
        return "hello";
    }
}

结果:

多环境配置开发 -配置文件

第一种:使用yml文件

spring:
  profiles:
    active: pro

---

spring:
  profiles: dev
server:
  port: 8081

---

spring:
  profiles: test
server:
  port: 8082

---

spring:
  profiles: pro

server:
  port: 8083

先写一个主环境

spring:
  profiles:
    active:

然后不同的开发环境之间使用 ---  三个杠间隔

之后相使用哪个环境直接在active属性赋值即可,如要使用pro环境,结果端口就要为8083

第二种:使用properties文件

先创建一个application.properties文件,定义主环境

spring.profiles.active=

 再写两个properties定义多环境

使用test环境

结果端口号为8082 

 本地启动SpringBoot项目

使用插件进行jar打包

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

 然后先点clean,再点package

之后在target中看见jar包

打开jar包本地路径

启动

可以发现启动的这个程序是pro环境,那如何在启动的时候切换环境呢?

在启动命令后面加上

--spring.profiles.active=环境名

也可以临时改端口号

只要加上

--server.port=端口号

 Maven的pom.xml文件中配置多环境

  <profiles>
<!--        开发环境-->
        <profile>
            <id>env_dev</id>
            <properties>
                <profile.active>dev</profile.active>
            </properties>
        </profile>
<!--        测试环境-->
        <profile>
            <id>env_test</id>
            <properties>
                <profile.active>test</profile.active>
            </properties>
<!--            设置为默认环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
<!--        生产环境-->
        <profile>
            <id>env_pro</id>
            <properties>
                <profile.active>pro</profile.active>
            </properties>
        </profile>
    </profiles>

yml文件使用pom.xml中的默认环境

spring:
  profiles:
    active: @profile.active@
#使用的是pom.xml中profile.active中的值
---

spring:
  profiles: dev
server:
  port: 8081

---

spring:
  profiles: test
server:
  port: 8082

---

spring:
  profiles: pro

server:
  port: 8083

结果:为test环境

但是我们可以发现这里yml使用pom.xml文件的属性值时使用的是@@占位符,我们也可以进行修改,进而使用默认的占位符${}

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>

 插件管理中加上

 <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>

这样一来就可以使用默认的占位符了

配置文件分类

1级:与jar包同级目录的config目录里的yml文件--file:config/application.yml(优先级最高)

2级:与jar包同级目录的yml文件--file:application.yml

3级:resource里config包下的yml文件--classpath:config/application.yml

4级:resource里的yml文件--classpath:application.yml

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落落落sss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值