2、SpringBoot配置

添加bean文件夹,保存类信息
在这里插入图片描述

package com.huawei.testdemo.bean;

public class Dog {
    private String name;
    private int  age;

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

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

package com.huawei.testdemo.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@ConfigurationProperties(prefix = "person")  //配置类,批量注入
public class Person {
    private String  name;
    @V
    private int  age;
    private Date birth;
    private  Dog dog;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                ", dog=" + dog +
                '}';
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

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

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public int getAge() {
        return age;
    }

    public Date getBirth() {
        return birth;
    }

}

如果出现报错:在这里插入图片描述

在POM中添加配置文件处理器依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

重新运行运行main函数,添加配置信息
在这里插入图片描述

person.name = jack
person.age = 18
person.birth = 2020/11/22
person.dog.name = dog
person.dog.age = 2

POM导入单元测试依赖

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

运行单元测试
在这里插入图片描述

package com.huawei.testdemo;

import com.huawei.testdemo.bean.Person;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
class TestdemoApplicationTests {
    @Autowired//自动注入
    Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }
}

运行结果:
在这里插入图片描述
在Person.java中,改用@Value写入属性
在这里插入图片描述
单个写入在这里插入图片描述
区别:
在这里插入图片描述

配置文件占位符
使用以下占位符可以生成随机数,还可以指定随机数类型

${random.uuid} :随机生成uuid

${random.value} :随机生成字符串

${random.int} :随机生成int型数字

${random.int(value,[max])} 随机生成数字,指定最大值

${random.long}:随机生成log型数字

${random.long(value,[max])}:随机生成long型数字,可指定最大值

属性占位符
可以在配置文件中引用以前配置过的属性;
使用 : 来指定默认值;
代码示例:

person.last-name=jack${random.value}
person.age=${random.int(100)}
person.birthday=2020/10/11
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.last-name} have a dog
person.dog.age=${initAge:12}

配置文件的加载
配置文件加载位置
Spring Boot在启动时会扫描以下位置的 application.properties 和 application.yml 文件作为Spring Boot的默认配置文件。

file:./config/:项目中config文件夹内
file:./:项目文件内
classpath:/config/:src/resource/config文件夹内
classpath:/:src/resource文件夹内

加载的优先级由高到低,高优先级的配置会覆盖低优先级的配置;

Spring Boot会加载这四个位置的全部配置文件,形成互补配置;

可以通过 spring.config.location 改变默认配置文件位置;

可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;便于运维时调整项目的配置信息,可以通过命令行调整配置信息。

命令行参数: --spring.config.location=G:/application.properties

如在部署时执行此命令:java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=E:/application.properties

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值