SpringBoot的配置

配置文件

SpringBoot使用一个全局的配置文件,配置文件名是固定的(application.propertiesapplication.yml)。配置文件的作用:修改SpringBoot自动配置的默认值,SpringBoot在底层都给我们自动配置好了。

以前的配置文件;大多都使用的是 xxxx.xml文件。.yml是YAML语言的文件,以数据为中心,比json、xml等更适合作为配置文件。

==配置例子:==YAML:

server:
 port: 8081

XML:

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

YAML语法

基本语法

  • 使用缩进表示层级关系
  • 缩进时不允许使用Tab键,只允许使用空格
  • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
  • 大小写敏感

==application.yml:==

server:
 port: 8081
 path: /hello

值的写法

YAML 支持的三种数据结构

  • 字面量:单个的、不可再分的值
  • 对象:键值对的集合
  • 数组:一组按次序排列的值

字面量:普通的值(数字,字符串,布尔)

k: v:字面直接来写 字符串默认不用加上单引号或者双引号 “”:双引号:不会转义字符串里面的特殊字符 特殊字符会作为本身想表示的意思name: "zhangsan n lisi" 输出:zhangsan 换行 lisi单引号:会转义特殊字符,特殊字符最终只是一个普通的字符串数据name: 'zhangsan n lisi' 输出:zhangsan n lisi

对象、Map(属性和值)(键值对)

k: v 在下一行来写对象的属性和值的关系,注意缩进和空格。

对象还是k: v的方式

friends: 
 lastName: Keafmd
 age: 20

行内写法:

friends: {lastName: Keafmd,age: 18}

数组(List、Set)

- 值表示数组中的一个元素。

pets: 
  cat 
  dog 
  pig

行内写法:

pets: [cat,dog,pig]

配置文件值注入

代码演示

==Person:==

package com.keafmd.springboot.bean;

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;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Keafmd
 *
 * @ClassName: Person
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-02-23 12:47
 */

 /**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties :告诉SpringBoot将本类中的所有属性和配置文件相关的配置进行绑定
 * prefix = "person" :配置文件中哪个属性进行一一映射
 *
 * 只有这个组件时容器中的组件吗,才能使用容器提供的功能
 */

@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birthday;

    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    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;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + ''' +
                ", age=" + age +
                ", boss=" + boss +
                ", birthday=" + birthday +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

==Dog:==

package com.keafmd.springboot.bean;

/**
 * Keafmd
 *
 * @ClassName: Dog
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-02-23 12:50
 */

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

    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;
    }

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

==application.yml:==

person:
  lastName: Keafmd
  age: 18
  boss: false
  birthday: 2020/02/02
  maps: {k1: v1,k2: v2}
  lists:
    - lisi
    - zhaoliu
    - Keafmd
    - xiaolan
  dog:
    name: 二狗
    age: 2

==测试代码:==

package com.keafmd.springboot;

import com.keafmd.springboot.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

/**
 * SpringBoot单元测试
 * 可以再测试期间很方便的类似编码一样进行自动注入等容器
 */

@SpringBootTest
class SpringBoot02ConfigApplicationTests {

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

}

==运行结果:==

Person{lastName='Keafmd', age=18, boss=false, birthday=Sun Feb 02 00:00:00 CST 2020, maps={k1=v1, k2=v2}, lists=[lisi, zhaoliu, Keafmd, xiaolan], dog=Dog{name='二狗', age=2}}

导入配置文件处理器,使编写配置有提示

注意:我们可以在pom.xml中导入配置文件处理器,以后编写配置就有提示了。

<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-configuration-processor</artifactId>
     <optional>true</optional>
</dependency>

properties配置文件在idea中默认utf-8可能会乱码

application.yml的person注释掉,在application.properties文件中进行编写。

==application.properties:==

# 配置person的值
person.last-name=张三
person.age=18
person.boss=false
person.birthday=2022/02/02
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=二狗
person.dog.age=3

我们再次运行测试代码会发现,会出现中文乱码的情况。

==解决办法:==SpringBoot的配置设置好再次运行测试代码==运行结果:==

Person{lastName='张三', age=18, boss=false, birthday=Sun Feb 02 00:00:00 CST 2020, maps={k1=v1, k2=v2}, lists=[a, b, c], dog=Dog{name='二狗', age=3}}

@Value获取值和@ConfigurationProperties获取值比较

@ConfigurationProperties @Value
功能 批量注入配置文件中的属性 一个个指定
松散绑定(松散语法) 支持 不支持
SpEL 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装 支持 不支持

配置文件yml还是properties他们都能获取到值 如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value 如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties

==application.properties:==

# 配置person的值
person.last-name=张三
person.age=18
person.boss=false
person.birthday=2022/02/02
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=二狗
person.dog.age=3

==Person:(部分代码,get和set以及toString不展示了)==

package com.keafmd.springboot.bean;

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;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Keafmd
 *
 * @ClassName: Person
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-02-23 12:47
 */


/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties :告诉SpringBoot将本类中的所有属性和配置文件相关的配置进行绑定
 * prefix = "person" :配置文件中哪个属性进行一一映射
 *
 * 只有这个组件时容器中的组件吗,才能使用容器提供的功能
 */

@Component
//@ConfigurationProperties(prefix = "person")
public class Person {

    /**
     * <bean class = "Person">
     *     <property name = "lastName" value = "字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
     * </bean>
     */

    @Value("${person.last-name}")
    private String lastName;
    @Value("#{11*2}")
    private Integer age;
    @Value("true")
    private Boolean boss;
    private Date birthday;

    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

}

==运行测试代码的运行结果:==

Person{lastName='张三', age=22, boss=true, birthday=null, maps=null, lists=null, dog=null}

属性名匹配规则(Relaxed binding) – person.firstName:使用标准方式 – person.first-name:大写用- – person.first_name:大写用_ – PERSON_FIRST_NAME:• 推荐系统属性使用这种写法

配置文件注入值数据校验

@Value 不支持数据校验。

需要先在pom.xml引入校验的依赖:

<!--引入校验-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

==Person:(部分代码,get和set以及toString不展示了)==

package com.keafmd.springboot.bean;

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;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Keafmd
 *
 * @ClassName: Person
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-02-23 12:47
 */


/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties :告诉SpringBoot将本类中的所有属性和配置文件相关的配置进行绑定
 * prefix = "person" :配置文件中哪个属性进行一一映射
 *
 * 只有这个组件时容器中的组件吗,才能使用容器提供的功能
 */

@Component
@ConfigurationProperties(prefix = "person")
@Validated //校验
public class Person {

    /**
     * <bean class = "Person">
     *     <property name = "lastName" value = "字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
     * </bean>
     */

    //@Value("${person.last-name}")
    //lastName必须是邮箱格式
    @Email
    private String lastName;
    //@Value("#{11*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;
    private Date birthday;

    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

}

==运行测试类的结果:==SpringBoot的配置

@PropertySource&@ImportResource&@Bean

@PropertySource:加载指定的配置文件

==person.properties:==

person.last-name=Keafmd-person
person.age=18
person.boss=false
person.birthday=2022/02/02
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=二狗
person.dog.age=3

我们想加载这个配置文件的内容,就必须用 @PropertySource指定配置文件

package com.keafmd.springboot.bean;

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;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Keafmd
 *
 * @ClassName: Person
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-02-23 12:47
 */


/**
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @ConfigurationProperties :告诉SpringBoot将本类中的所有属性和配置文件相关的配置进行绑定
 * prefix = "person" :配置文件中哪个属性进行一一映射
 *
 * 只有这个组件时容器中的组件吗,才能使用容器提供的功能
 */

@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated //校验
public class Person {

    /**
     * <bean class = "Person">
     *     <property name = "lastName" value = "字面量/${key}从环境变量、配置文件中获取值/#{SpEL}"></property>
     * </bean>
     */

    //@Value("${person.last-name}")
    //lastName必须是邮箱格式
    //@Email
    private String lastName;
    //@Value("#{11*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;
    private Date birthday;

    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
}

==运行测试类的测试结果:==

Person{lastName='Keafmd-person', age=18, boss=false, birthday=Wed Feb 02 00:00:00 CST 2022, maps={k1=v1, k2=v2}, lists=[a, b, c], dog=Dog{name='二狗', age=3}}

==注意==:需要把application.propertiesapplication.yml里的person注释掉。

@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效,使用@Bean给容器中添加组件

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别。如果想让Spring的配置文件生效,加载进来,需要把@ImportResource标注在一个配置类上。

@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效

创建一个Spring的配置文件beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="helloService" class="com.keafmd.springboot.service.HelloService">

    </bean>
</beans>

创建一个helloService:

package com.keafmd.springboot.service;

/**
 * Keafmd
 *
 * @ClassName: HelloService
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-02-23 15:59
 */

public class HelloService {
}

==先测试下容器中有没有helloService:==

package com.keafmd.springboot;

import com.keafmd.springboot.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

/**
 * SpringBoot单元测试
 * 可以再测试期间很方便的类似编码一样进行自动注入等容器
 */

@SpringBootTest
class SpringBoot02ConfigApplicationTests {

    @Autowired
    Person person;

    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloService(){
        Boolean b = ioc.containsBean("helloService");
        System.out.println(b);
    }

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

}

运行testHelloService的测试结果:

false

此时说明配置文件并没有生效。

我们把@ImportResource标注在主配置类上。

==SpringBoot02ConfigApplication :==

package com.keafmd.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBoot02ConfigApplication.classargs);
    }

}

==再次运行测试方法:==

true
SpringBoot的配置
在这里插入图片描述

但是在实际开发中我们会采用一种更合适的方法,SpringBoot推荐给容器中添加组件的方式是推荐使用全注解的方式。接下来把@ImportResource(locations = {"classpath:beans.xml"})注释掉,创建一个配置类MyAppConfig 。

1、创建配置类,使用 @Configuration ——>Spring配置文件 2、使用 @Bean给容器中添加组件

==MyAppConfig: ==

package com.keafmd.springboot.config;

import com.keafmd.springboot.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Keafmd
 *
 * @ClassName: MyAppConfig
 * @Description: 配置类
 * @author: 牛哄哄的柯南
 * @date: 2021-02-23 16:06
 */


/**
 * @Configuration: 指明当前类是配置类,替代之前的Spring配置文件
 * 在配置文件中用<bean></bean>标签添加组件
 */

@Configuration
public class MyAppConfig {

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

==再次运行测试方法:==SpringBoot的配置

配置文件占位符

随机数

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

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

# 配置person的值
person.last-name=Keafmd${random.uuid}
#person.last-name=张三
#person.age=${random.int}
person.age=18
person.boss=false
person.birthday=2022/02/02
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
#person.dog.name=${person.last-name}的二狗
person.dog.name=${person.hello:hello}的二狗
#person.dog.name=二狗
person.dog.age=3

Profile

多Profile文件

我们在主配置文件编写的时候,文件名可以是   application-{profile}.properties/yml 默认使用application.properties的配置

SpringBoot的配置
在这里插入图片描述

yml支持多文档块方式

server:
  port: 8081
spring:
  profiles:
    active: dev #指定属于哪个环境

---
server:
  port: 8083

spring:
  config:
    activate:
      on-profile: dev



---
server:
  port: 8084
spring:
  profiles: prod  #不推荐的写法


---


SpringBoot的配置
在这里插入图片描述

激活指定profile

在配置文件中指定  spring.profiles.active=dev

==application.properties:==

server.port=8081
spring.profiles.active=dev

命令行

1、可以在这里配置SpringBoot的配置

SpringBoot的配置
在这里插入图片描述

2、也可以直接在测试的时候,配置传入命令行参数打包后运行jar包时,输入下面的命令:

java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

虚拟机参数

-Dspring.profiles.active=dev
SpringBoot的配置
在这里插入图片描述

配置文件加载位置

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

优先级由高到底,高优先级的配置会覆盖低优先级的配置

SpringBoot的配置
在这里插入图片描述

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

例子:

==我们创建个HelloController:==

package com.keafmd.springboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Keafmd
 *
 * @ClassName: HelloController
 * @Description:
 * @author: 牛哄哄的柯南
 * @date: 2021-02-23 17:07
 */

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

==这是上面优先级第四的配置文件内容:==

server.port=8081

# 配置项目的访问路径
server.servlet.context-path=/boot02

==这是上面优先级第一的配置文件内容:==

server.port=8084

最后我们启动程序。

==运行结果:==SpringBoot的配置SpringBoot的配置

我们还可以通过spring.config.location来改变默认的配置文件位置

==我们先在F盘放一个配置文件,在这个配置文件把端口改为8085:==

server.port=8085

SpringBoot的配置测试一下:SpringBoot的配置说明这样是不起作用的。


==我们先将项目打包,生成jar包。==SpringBoot的配置

项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置。

java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.additional-location=F:/application.properties

==打开便捷的控制台:==SpringBoot的配置

输入:java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.additional-location=F:/application.properties,然后回车:

SpringBoot的配置==我们就可以发现,是启动的8085端口,证明启用的F盘的配置文件。==SpringBoot的配置

外部配置加载顺序

==SpringBoot也可以从以下位置加载配置,优先级从高到低,高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置==

==1.== 命令行参数

所有的配置都可以在命令行上进行指定

java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --server.port=8087  --server.servlet.context-path=/abc

多个配置用空格分开, –配置项=值

==运行结果:==SpringBoot的配置SpringBoot的配置

==2.== 来自java:comp/env的JNDI属性

==3.== Java系统属性(System.getProperties())

==4.== 操作系统环境变量

==5.== RandomValuePropertySource配置的random. * 属性值

   

==由jar包外向jar包内进行寻找,优先加载带profile==SpringBoot的配置

==6.== jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件

==7.== jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件

   

==再来加载不带profile==

==8.== jar包外部的application.properties或application.yml(不带spring.profile)配置文件

==9.== jar包内部的application.properties或application.yml(不带spring.profile)配置文件

==10.== @Configuration注解类上的@PropertySource

==11.== 通过SpringApplication.setDefaultProperties指定的默认属性

所有支持的配置加载来源:参考官方文档SpringBoot的配置

以上就是SpringBoot的配置【配置文件、加载顺序、配置原理】(超详细)的全部内容。

看完如果对你有帮助,感谢点赞支持!如果你是电脑端的话,看到右下角的 “==一键三连==” 了吗,没错点它[哈哈]SpringBoot的配置

加油!

共同努力!

Keafmd


本篇文章来源于微信公众号: 牛哄哄的柯南

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/11954.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!