• SpringBoot读取配置文件的内容


    1.@Value读取

    在springboot项目中,如果要读取配置文件application.properties或application.yml文件的内容,可以使用自带的注解@Value。以properties方式为例说明,yml方式同上:

    1.1单层内容

    application.properties配置文件内容:

    name=huihui
    age=22
    sex=1

    读取方式:

    //导入的类org.springframework.beans.factory.annotation.Value;   
       @Value("${name}")
        private String name;
    
        @Value("${age}")
        private Integer age;
    
        @Value("${sex}")
        private String sex;

    1.2多层内容

    application.properties配置文件内容:

    user.name=huihui
    user.age=22
    user.sex=1

    读取方式:

    //导入的类org.springframework.beans.factory.annotation.Value;   
       @Value("${user.name}")
        private String name;
    
        @Value("${user.age}")
        private Integer age;
    
        @Value("${user.sex}")
        private String sex;

    对于多层内容,读取时必须指定到最后一级。如上例必须到path,否则会报错。实际上单层和多层没有区别,只要指定到最后一级即可。

    1.3设置默认值

    对于有些配置,如果没有在配置文件中配置,那么在启动时就会报错,它也可以设置默认值。

    以上面的多层配置为例,并没有配置user.addr的值,在注入时指定默认值:

        @Value("${user.name}")
        private String name;
    
        @Value("${user.age}")
        private Integer age;
    
        @Value("${user.sex}")
        private String sex;
    
        @Value("${user.addr:湖北武汉}")
        private String addr;

    由此可见,要指定默认值,只需要在注入的时候在参数后面添加“:”及默认值即可。

    2.@Value+@PropertySource读取

    上述只是读取application的配置文件,那么如何读取自定义的配置文件呢?那就需要使用另外一个注解@PropertySource,指定配置文件的路径和名称。

    2.1读取properties文件内容

    在resources目录下新建testAAA.properties文件,内容如下:

    user.name=huihui
    user.age=22
    user.sex=1

    使用组件的方式读取:

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    @Component
    @PropertySource("classpath:testAAA.properties")
    public class TestService {
    
        @Value("${user.name}")
        private String name;
    
        @Value("${user.age}")
        private Integer age;
    
        @Value("${user.sex}")
        private String sex;
    
        public void test() {
            System.out.println(name);
            System.out.println(age);
            System.out.println(sex);
        }
    }

    2.1读取yml文件内容

    对于yml的内容,如果还是采用上面的方式,是无法读取到内容的,还会报错。需要在上述基础上指定自定义的工厂类。

    1)新建一个类,继承DefaultPropertySourceFactory类重写其方法

    package com.zys.springboottestexample.config;
    
    import org.springframework.boot.env.YamlPropertySourceLoader;
    import org.springframework.core.env.PropertySource;
    import org.springframework.core.io.support.DefaultPropertySourceFactory;
    import org.springframework.core.io.support.EncodedResource;
    
    import java.io.IOException;
    import java.util.List;
    
    public class PropertySourceFactory extends DefaultPropertySourceFactory {
        @Override
        public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
            if (resource == null) {
                return super.createPropertySource(name, resource);
            }
            List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource());
            return sources.get(0);
        }
    }

    2)在读取时指自定义的工厂类

    @Component
    @PropertySource(value="classpath:testAAA.yml", factory = PropertySourceFactory.class)
    public class TestService {
    
        @Value("${user.name}")
        private String name;
    
        @Value("${user.age}")
        private Integer age;
    
        @Value("${user.sex}")
        private String sex;
    
        public void test() {
            System.out.println(name);
            System.out.println(age);
            System.out.println(sex);
        }
    }

    yml的内容同上,可粘贴:

    user:
      name: huihui
      age: 22
      sex: 1

    3.@Value+@ConfigurationProperties读取

    3.1读取默认的application.properties文件

    当对同一对象有多个属性时,每次在使用这些值时都重复的使用Value注解注入,有些麻烦,实际上也可以把这些属性注入到bean上,需要使用的地方直接使用此bean即可。读取application.yml文件可参考此方法,是一样的读取方式。

    properties内容:

    user.name=huihui
    user.age=22
    user.sex=1

      读取内容:

    @Component
    @ConfigurationProperties(prefix="user")
    @Data
    public class TestService {
    
        String name;
        String age;
        String sex;
    
        public void test() {
            System.out.println(name);
            System.out.println(age);
            System.out.println(sex);
        }
    }

    需要注意的是,读取时,属性必须有set方法。

    3.2读取自定义的properties文件

    要读取自定义的文件,根据第二章的思想,如果可以指定文件的位置和名称就可以读取。但ConfigurationProperties注解并不能指定,这就需要PropertySource结合来指定自定义的配置文件。

    @Component
    @ConfigurationProperties(prefix="user")
    @PropertySource("classpath:testAAA.properties")
    @Data
    public class TestService {
    
        private String name;
    
        private Integer age;
    
        private String sex;
    
        public void test() {
            System.out.println(name);
            System.out.println(age);
            System.out.println(sex);
        }
    }

    对于yml的内容,参考第二章自行。

  • 相关阅读:
    做好测试计划和测试用例的工作的关键是什么?
    windows创建虚拟环境
    面试题整理
    【爬虫】使用selenium设置cookie
    【爬虫】随机获取UA
    Scrapy爬虫提高效率
    【爬虫】多线程爬取糗事百科写入文件
    【爬虫】多线程爬取表情包
    【爬虫】Condition版的生产者和消费者模式
    【爬虫】Load版的生产者和消费者模式
  • 原文地址:https://www.cnblogs.com/zys2019/p/14252914.html
Copyright © 2020-2023  润新知