右耳菌-邓小白的Java架构师的修炼之路

Spring Boot 配置说明

2022-04-24  本文已影响0人  右耳菌

一、SpringBoot 中的配置文件

1. springboot为什么还需要用配置文件?
2. springboot中的配置文件有哪些?
3. springboot中的配置文件使用中注意事项

二、yml文件的语法说明

1. yml文件的介绍
2. yml文件的语法说明

yml语法

server:
  port: 8099

person:
    name: neco deng
    age: 18
    isMarried: false
#    friend:
#      name: zhangsan
#      sex: 女
    friend: {name: zhangsan, sex: 女}
#    pets:
#      - 小猫
#      - 小狗
#      - 小兔子
    pets: [小猫, 小狗, 小兔子]
    friends: [{name: zangsan, sex: 女}, {name: lisi, sex: 男}]
server.port=8088

person1.name=zhangsan
person1.sex=femmale
person1.age=19
person1.isMarried=false
person1.pets[0]=小猫
person1.pets[1]=小狗
person1.pets[2]=小兔子
person1.friends[0].name="王五"
person1.friends[0].sex="男"
person1.friends[1].name="李四"
person1.friends[2].sex="女"
3. 自动注入配置文件的信息到对象中
package cn.lazyfennec.springboot.bean;

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

import java.util.List;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/4/24 23:09
 */
public class Person {
    private String name;
    private Integer age;
    private String sex;
    private boolean isMarried;
    private Friend friend;
    private List<String> pets;
    private List<Friend> friends;

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public boolean isMarried() {
        return isMarried;
    }

    public void setMarried(boolean married) {
        isMarried = married;
    }

    public Friend getFriend() {
        return friend;
    }

    public void setFriend(Friend friend) {
        this.friend = friend;
    }

    public List<String> getPets() {
        return pets;
    }

    public void setPets(List<String> pets) {
        this.pets = pets;
    }

    public List<Friend> getFriends() {
        return friends;
    }

    public void setFriends(List<Friend> friends) {
        this.friends = friends;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", isMarried=" + isMarried +
                ", friend=" + friend +
                ", pets=" + pets +
                ", friends=" + friends +
                '}';
    }
}
package cn.lazyfennec.springboot.bean;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/4/24 23:12
 */
public class Friend {
    private String name;
    private String sex;

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

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

具体如下(截选)

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/4/24 23:09
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
package cn.lazyfennec.springboot;

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

@SpringBootTest
class HelloSpringbootApplicationTests {
    @Autowired
    private Person person;
    @Test
    void contextLoads() {
        System.out.println(person);
    }
}
Person{name='neco deng', age=18, sex='男', isMarried=false, friend=Friend{name='zhangsan', sex='女'}, pets=[小猫, 小狗, 小兔子], friends=[Friend{name='zangsan', sex='女'}, Friend{name='lisi', sex='男'}]}
package cn.lazyfennec.springboot.bean;

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

import java.util.List;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/4/24 23:09
 */
@Component
//@ConfigurationProperties(prefix = "person1")
public class Person {
    @Value("${person.name}")
    private String name;
    @Value("${person.age}")
    private Integer age;
    @Value("#{11*2}")
    private String sex;
    private boolean isMarried;
    private Friend friend;
    private List<String> pets;
    private List<Friend> friends;

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public boolean isMarried() {
        return isMarried;
    }

    public void setMarried(boolean married) {
        isMarried = married;
    }

    public Friend getFriend() {
        return friend;
    }

    public void setFriend(Friend friend) {
        this.friend = friend;
    }

    public List<String> getPets() {
        return pets;
    }

    public void setPets(List<String> pets) {
        this.pets = pets;
    }

    public List<Friend> getFriends() {
        return friends;
    }

    public void setFriends(List<Friend> friends) {
        this.friends = friends;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", isMarried=" + isMarried +
                ", friend=" + friend +
                ", pets=" + pets +
                ", friends=" + friends +
                '}';
    }
}

三、自定义properties的读取

  1. 自定义一个properties(person2.properties)文件,并且让key和之前的properties不一样;
// person2.properties

server.port=8088

person2.name=zhangsan222
person2.sex=female2
person2.age=22
person2.isMarried=false
  1. 使用@PropertySource注解来标明你要读取的properties文件名;
  2. 跟之前一样的读取方式,注意修改prefix的值;
package cn.lazyfennec.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.List;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/4/24 23:09
 */
@Component
@ConfigurationProperties(prefix = "person2")
@PropertySource("classpath:person2.properties")
public class Person {
    private String name;
    private Integer age;
    private String sex;
    private boolean isMarried;
    private Friend friend;
    private List<String> pets;
    private List<Friend> friends;

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public boolean isMarried() {
        return isMarried;
    }

    public void setMarried(boolean married) {
        isMarried = married;
    }

    public Friend getFriend() {
        return friend;
    }

    public void setFriend(Friend friend) {
        this.friend = friend;
    }

    public List<String> getPets() {
        return pets;
    }

    public void setPets(List<String> pets) {
        this.pets = pets;
    }

    public List<Friend> getFriends() {
        return friends;
    }

    public void setFriends(List<Friend> friends) {
        this.friends = friends;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", isMarried=" + isMarried +
                ", friend=" + friend +
                ", pets=" + pets +
                ", friends=" + friends +
                '}';
    }
}
  1. 运行ApplcaitonTest文件
package cn.lazyfennec.springboot;

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

@SpringBootTest
class HelloSpringbootApplicationTests {
    @Autowired
    private Person person;
    @Test
    void contextLoads() {
        System.out.println(person);
    }
}
  1. 结果如下
Person{name='zhangsan222', age=22, sex='female2', isMarried=false, friend=null, pets=null, friends=null}

更多知识,请点击关注查看我的主页信息哦

上一篇下一篇

猜你喜欢

热点阅读