springBoot配置文件

目录

一、SpringBoot使用一个全局的配置文件,配置文件名是固定的

二、配置文件的作用

三、YAML

四、配置文件占位符

1.随机数

${random.int}

Person类

TestController

​编辑${random.int(10)}

2.占位符获取之前配置的值,如果没有可以是用:指定默认值

配置文件

PersonDog类

PersonDogController

运行

修改配置文件再运行

五、@PropertySource&@ImportResource&@Bean

1.@PropertySource

配置文件

PersonDog类

PersonDogController

运行:

2.@ImportResource

3.@Bean

六、profile

application.yml

Person类

PersonTest测试类

七、配置文件加载位置

验证


一、SpringBoot使用一个全局的配置文件,配置文件名是固定的

•application.properties

•application.yml

这两个是互补的,只是后缀不一样,格式不一样,用得比较多的是application.yml(YAML也行)

二、配置文件的作用

修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好

三、YAML

以前的配置文件;大多都使用的是 xxxx.xml文件;

YAML:以数据为中心,比json、xml等更适合做配置文件;

例子:
YAML:

server:
  port: 8081
# (port:和8081之间有空格)

XML

<server>
    <port>8081</port>
</server>

四、配置文件占位符

1.随机数

${random.value}、${random.int}、${random.long}

${random.int(10)}、${random.int[1024,65536]}

${random.int}

Person类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;

@Component
@ConfigurationProperties("person")
public class Person {
    private String name;
    private Integer age;
    private Boolean boss;
    private String email;

    public Person() {
    }

    public Person(String name, Integer age, Boolean boss, String email) {
        this.name = name;
        this.age = age;
        this.boss = boss;
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    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 Boolean getBoss() {
        return boss;
    }

    public void setBoss(Boolean boss) {
        this.boss = boss;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", email=" + email +
                '}';
    }
}
TestController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/test")
public class TestController {
    @Autowired
    private Person person;

    @RequestMapping("/hello")
    @ResponseBody
    public String sayHello(){
        System.out.println(person);
        return "say Hello";
    }
}

${random.int(10)}

Person类和TestController不变

2.占位符获取之前配置的值,如果没有可以是用:指定默认值

person.last‐name=张三${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.hello:hello}_dog
person.dog.age=15

配置文件

PersonDog类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "person1")
public class PersonDog {
    private String name;
    private int age;
    private Dog dog;

    public PersonDog() {
    }

    public PersonDog(String name, int age, Dog dog) {
        this.name = name;
        this.age = age;
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public Dog getDog() {
        return dog;
    }

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

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

PersonDogController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/person")
public class PersonDogController {

    @Autowired
    private PersonDog personDog;

    @RequestMapping("/dog")
    @ResponseBody
    public String sayHello(){
        System.out.println(personDog);
        return "对象里面含对象";
    }
}

运行

修改配置文件再运行

五、@PropertySource&@ImportResource&@Bean

1.@PropertySource

导入外部的properties

配置文件

多加一个配置文件:

person1.name=李四
person1.age=20
person1.dog.name=狗
person1.dog.age=5

我不想走默认的,我想走我写的这个person.properties,我就得加个注解,在PersonDog类里

PersonDog类

@Component
@ConfigurationProperties(prefix = "person1")
@PropertySource(value = {"classpath:person.properties"})
public class PersonDog {
    private String name;
    private int age;
    private Dog dog;

    public PersonDog() {
    }

    public PersonDog(String name, int age, Dog dog) {
        this.name = name;
        this.age = age;
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public Dog getDog() {
        return dog;
    }

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

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

但是这样不行,因为他是先加载application.yml,可以运行看一下输出,此时的application.yml

很明显跟person.properties的内容是不一样,所以很好区分输出的是哪一个

PersonDogController

@Controller
@RequestMapping("/person")
public class PersonDogController {

    @Autowired
    private PersonDog personDog;

    @RequestMapping("/dog")
    @ResponseBody
    public String sayHello(){
        System.out.println(personDog);
        return "复杂数据绑定";
    }
}

运行:

可以看出输出的是application.yml里的,现在把这个里面的注释掉,只留person.properties里面的再运行:

这次输出的是person.properties里的内容

2.@ImportResource

导入外部的配置文件

①导入Spring的配置文件,让配置文件里面的内容生效;

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

③想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上

④这个注解是加在启动类Springboot05Application.java上的

3.@Bean

跟spring里的bean使用方式一样

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

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

六、profile

多配置文件

springBoot里面弄没有那么多的配置文件,只有一个总的配置文件,要配置多个环境(环境有测试环境、开发环境、部署环境等),参数是不一样的,怎么来控制多种环境,用spring.profiles.active

yml支持多文档块方式

application.yml

server:
  port: 8081

spring:
  profiles:
    active: dev  #通过这里决定走的是哪个配置文件
---
person: #测试环境,相当于这是第一个配置文件的
  name: 张三
  age: 21
  boss: true
  email: 123@qq.com
spring:
  profiles: dev
---
person: #生产环境,这是第二个配置文件的
  name: 李四
  age: 10
  boss: true
  email: 234@qq.com
spring:
  profiles: prod

Person类

@Component
@ConfigurationProperties("person")
public class Person {
    private String name;
    private Integer age;
    private Boolean boss;
    private String email;

    public Person() {
    }

    public Person(String name, Integer age, Boolean boss, String email) {
        this.name = name;
        this.age = age;
        this.boss = boss;
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    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 Boolean getBoss() {
        return boss;
    }

    public void setBoss(Boolean boss) {
        this.boss = boss;
    }

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

PersonTest测试类

import com.qcby.springboot05.controller.Person;
import org.junit.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
public class PersonTest {
    @Autowired
    private Person person;

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

运行:

七、配置文件加载位置

springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件

(1)根目录config文件夹下----1

(2)根目录下---4

(3)resources里的config文件夹下---2

(4)resources下---3

优先级从高到低,高优先级的配置会覆盖低优先级的配置;

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

验证

运行可以看到端口号是8082

现在注释掉8082再运行

可以看到端口号是8083

再把8083注释掉运行可以看到端口号是8084

再把8084注释掉就是8081了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值