Springboot2--配置文件1

前言

Springboot通过配置文件来改变自动配置的默认值或者给组件注入参数,配置文件的名字是固定的application.properties或application.yml

一、基本语法

1.application.properties

server.port=8080

#person
person.last-name=李冰
person.age=12
person.birth=2020/11/01
person.boss=false
person.dog.dname=dog
person.dog.dage=12
person.lists=a,b,s
person.maps.k1=v1
person.maps.k2=50
#占位符${}
随机数
person.age=12${random.uuid}
person.age=12${random.int}
引用前面的变量
person.dog.dname=${person.last-name:默认值}dog

2.application.ym

#通过换行缩进和空格控制层级关系
server:
  port: 8080

#对象
person:
  lastName: chenri
  age: 18
  boss: true
  birth: 2019/12/08
  #集合
  maps:
    k1: v1
    k2: v2
  #数组
  lists:
    - lisi
    - ligg
  #对象
  dog:
    dname: kkkk
    dage: 14

  #数据的另外一种写法
  lists: [lisi,ligg]

  #对象和集合的另外一种写法
  dog: {dname: kkkk,dage: 14}
  #一般字符串不需要加引号
  双引号:\n是换行的意思
  单引号:\n就是字符\n
  也可使用占位符

二、配置文件映射到组件

1.ConfigurationProperties注解

yml版配置文件

person:
  lastName: chenri
  age: 18
  boss: true
  birth: 2019/12/08
  #集合
  maps:
    k1: v1
    k2: v2
  #数组
  lists:
    - lisi
    - ligg
  #对象
  dog:
    dname: kkkk
    dage: 14

properties版配置文件

person.last-name=李冰
person.age=12
person.birth=2020/11/01
person.boss=false
person.dog.dname=dog
person.dog.dage=12
person.lists=a,b,s
person.maps.k1=v1
person.maps.k2=50

在pom.xml中引入配置文件处理器

<!--导入配置文件处理器-->
 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
  </dependency>

加注解@ConfigurationProperties

package com.example.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 java.util.Date;
import java.util.List;
import java.util.Map;
/*
 * 将配置文件中配置的每一个属性的值,映射到这个组件中 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
 * prefix = "person":配置文件中哪个下面的所有属性进行一一映射 ** 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
* */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
	private String lastName;
	private Integer age;
	private Boolean boss;
	private Date birth;

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


	@Override
	public String toString() {
		return "Person{" +
				"lastName='" + lastName + '\'' +
				", age=" + age +
				", boss=" + boss +
				", birth=" + birth +
				", maps=" + maps +
				", lists=" + lists +
				", 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 getBirth() {
		return birth;
	}

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

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

测试代码

package com.example.springboot;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.example.springboot.bean.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 ApplicationTests {

	@Autowired
	Person p;

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

结果

Person{lastName='chenrili', age=18, boss=true, birth=Sun Dec 08 00:00:00 CST 2019, maps={k1=v1, k2=v2}, lists=[lisi, ligg], dog=Dog{dname='kkkk', dage=14}}

还可以支持JSR303数据校验

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

	private String lastName;
	private Integer age;
	
	@Email//数据校验,必须为email格式
	private String email;
	private Boolean boss;
	private Date birth;

2.Value注解

用Value注解给组件注入参数

@Component

public class Person {

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

Value和ConfigurationProperties比较

Value支持spEl,如@Value("#{10*2}"),不支持松散语法,不支持JSR303数据校验,不能获取复杂封装,如集合
ConfigurationProperties不支持spEl,但支持松散语法和JSR303数据校验

3.PropertySource注解

建立一个person.proerties文件

person.last-name=李冰
person.age=12
person.birth=2020/11/01
person.boss=false
person.dog.dname=dog
person.dog.dage=12
person.lists=a,b,s
person.maps.k1=v1
person.maps.k2=50

加PropertySource注解(指定配置文件)

package com.example.springboot.bean;

@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")

public class Person {

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

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

测试结果

Person{lastName='李冰', age=12, boss=false, birth=Sun Nov 01 00:00:00 CST 2020, maps={k1=v1, k2=50}, lists=[a, b, s], dog=Dog{dname='dog', dage=12}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值