Spring Boot 配置说明

作者: 右耳菌 | 来源:发表于2022-04-24 23:48 被阅读0次

    一、SpringBoot 中的配置文件

    1. springboot为什么还需要用配置文件?
    • 方便我们修改Springboot默认的配置
    • 我们有其他的信息需要保存在配置文件中
    2. springboot中的配置文件有哪些?
    • properties 配置文件
      • application.properties
    • yml配置文件
      • application.yml
    • 两者同时存在时,properties文件优先生效
    3. springboot中的配置文件使用中注意事项
    • 文件放在src/main/resources目录或者类路径/config目录下
    • springboot默认读取以application开头的配置文件

    二、yml文件的语法说明

    1. yml文件的介绍
    • yml的全称为YAML (YAML Ain't Markup Language)它是一种标记语言,它是一种直观的被电脑识别的数据序列化格式,并且容易被人类阅读,这种标记语言和其他标记语言不同的地方是,它是以数据为中心,比json、xml等更适合做配置文件。
    • 更具体的内容,请点击右方链接查看官网:https://yaml.org/
    2. yml文件的语法说明

    yml语法

    • yml的基本语法
      • key: value的格式(value前面的空格不能少,可以有多个,不能用tab替代)
      • 大小写敏感
      • 字符串默认不需要使用引号,单引号-不能转义双引号-可以转义 的 ,所以单双引号的区别在于是否能用转义字符
      • 注释方式:#
    • 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: 男}]
    
    • 一个类似上方配置的properties版本如下,这里的中文可能会有乱码的情况,需要在 file->setting-> file encoding 中设置字符集为utf-8,且勾选 Transparent native-to-ascii conversion,然后再重新运行,如果还不行则需要重新将中文打一次或者复制到记事本后再复制进来
    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. 自动注入配置文件的信息到对象中
    • 我们假设有上方的yml文件
    • 然后创建Person类
    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 +
                    '}';
        }
    }
    
    • 创建Friend类
    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 + '\'' +
                    '}';
        }
    }
    
    • 在Person类上加上这两个注解
      @Component
      @ConfigurationProperties(prefix = "person") // 这是一种对象整个注入的方式

    具体如下(截选)

    /**
     * @Author: Neco
     * @Description:
     * @Date: create in 2022/4/24 23:09
     */
    @Component
    @ConfigurationProperties(prefix = "person")
    public class Person {
    
    • 在xxxxApplicationTests 文件中定义并自动注入person,然后打印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='男'}]}
    
    • @Value的注入方式
    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}
    

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

    相关文章

      网友评论

        本文标题:Spring Boot 配置说明

        本文链接:https://www.haomeiwen.com/subject/kdpxyrtx.html