springBoot 配置文件语法

SprignBoot配置文件现在基本都用yml格式

一、语法

1.基本类型数据(k-v 键值对)

(1).键的后面加空格

  port: 90

(2).空格区分层次,左边对齐的为相同层次

server:
  servlet:
    context-path: /
  port: 90

2.对象数组类型

2.1对象多行写法:

server:
  port: 90

2.2对象单行写法

server: {port: 8080}

2.3数组多行写法

person
  - xiaoming
  - xiaohong
  - xiaolong 

2.4数组单行写法

person: [xiaoming,xiaohong,xiaolong]
 

二 列子:

首相创建俩个基本类

@Component:把类加载到容器
@ConfigurationProperties(prefix = "person"):把该类的属性与配置文件的属性对应起来

package com.csy.test3.entity;

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

import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private boolean isBoss;
    private Date birth;
    private Map<String,Object> maps;
    private List 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 isBoss() {
        return isBoss;
    }

    public void setBoss(boolean boss) {
        isBoss = 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 getLists() {
        return lists;
    }

    public void setLists(List 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 +
                ", isBoss=" + isBoss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
package com.csy.test3.entity;

import java.util.Date;
import java.util.List;
import java.util.Map;

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

    public Integer getAge() {
        return age;
    }

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

    public String getName() {

        return name;
    }

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

配置文件与java类对应起来

person:
  lastName: zhangsan
  age: 18
  isBoss: false
  birth: 2018/1/1
  maps: {k1,k2,k3}
  lists:
    - ls
    - zs
    - ww
  dog:
    name: xg
    age: 1

在pom.xml中提添加对yml文件与java类对应的提示:

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

 

测试

在springBoot提供的测试启动类中取出注入的javaBean

package com.csy.test3;

import com.csy.test3.entity.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;

import javax.xml.ws.soap.Addressing;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test3ApplicationTests {

    @Autowired
    Person person;

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

}

运行该文件查看控制台

三、@Value注解:

springBoot配置文件的值在其他java组件中也可以通过注解@Value来获取,这样很方便,但是只能设置或取出字面量的只,复杂结构的值不能取出:

通个断电能清楚的看到

4.分散配置:

SpringBoot设计的初中就是让开发者少写配置,如果有时候配置文件实在过大可以考虑分撒配置,这样维护更加方便,代码清晰:

1.编写分散配置文件:

person.lastName=zhangsan
person.age=18
person.isBoss=false
person.birth=2018/1/1
person.maps.k1=v1
person.maps.k2=v2
person.maps.k3=v3
person.lists=a,b,c
person.dog.name=xg
person.dog.age=12 

2.导入文件使文件属性生效

@PropertySource(value = {"classpath:person.properties"})指定配置文件路劲

package com.csy.test3.entity;

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

import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"})
public class  Person {
    private String lastName;
    private Integer age;
    private boolean isBoss;
    private Date birth;
    private Map<String,Object> maps;
    private List 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 isBoss() {
        return isBoss;
    }

    public void setBoss(boolean boss) {
        isBoss = 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 getLists() {
        return lists;
    }

    public void setLists(List 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 +
                ", isBoss=" + isBoss +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值