记录一下SpringBoot 项目读取配置文件数据的集中方式,这里以yml格式的文件为例
读取方式有三种:

  • 1、使用@Value注解(推荐使用)
  • 2、使用Environment对象
  • 3、自定义对象

application.yml文件如下,通过上面三种方式读取配置数据

server:
  port: 9080

1、使用@Value注解(推荐使用)

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试读取配置文件的几种方式:
 * @Date 2023/1/8 14:02
 **/
@RestController
public class ReadApplicationProperties {
    @Value("${server.port}")
    private Integer serverPort;
 
	//测试方式2:通过@Value注解读取配置信息
    @GetMapping("/readApplicationProperties2")
    public void readApplicationProperties2(){
        System.out.println("通过@Value注解读取配置信息:" + serverPort);
    }
}

注意点:
@Value不可以给static的属性进行赋值,否则得到的值为null

2、使用Environment对象

springboot可以使用@Autowired注解注入 Environment 对象的方式读取数据,这种方式springboot会将配置文件中的所有数据都封装到Environment对象中,需要使用哪个数据,只需要通过调用Environment对象的getProperty(String name)的方法获取,具体代码如下:

controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 测试读取配置文件的几种方式:
 * @Date 2022/7/20 14:02
 **/
@RestController
public class ReadApplicationProperties {
    @Autowired
    private Environment environment;
    
	//测试方式1:通过Environment读取配置信息
    @GetMapping("/readApplicationProperties1")
    public Map<String,Object> readApplicationProperties1(){
        Map<String,Object> map = new HashMap<>();
        map.put("port",environment.getProperty("server.port"));
        System.out.println("通过Environment读取配置信息:" + environment.getProperty("server.port"));
        return  map;
    }
}

3、自定义对象

通过@ConfigurationProperties注解读取配置信息

注意点说明:

  • 注意点1:
    – @ConfigurationProperties注解用于指定前缀,下方的属性名称必须和要获取的配置信息名称一致,比如必须叫port,否则获取值为null
    – 使用@ConfigurationProperties首先建立配置文件与对象的映射关系,然后在控制器方法中使用@Autowired注解将对象注入.
  • 注意点2:
    配置生效的两种方式:
    – 方式1:配置@Component
    – 方式2:启动类添加@EnableConfigurationProperties(ReadProperties.class)

总结:注解@Component和注解@EnableConfigurationProperties(ReadProperties.class)是等价的,写一个就行。

  • 注意点3:
    @ConfigurationProperties也可以和@Value和@Bean一起使用,只不过我没写案例。
  • 注意点4:
    @ConfigurationProperties只能加载以application为前缀开头的配置文件,比如application-dev.properties,加载自定义名称配置文件内容无效。
  • 注意点5:
    问题:从上面的示例中,我们可以看到在属性绑定中@EnableConfigurationProperties和@Component的效果一样,那么为啥springboot还要使用这个注解呢?

    答案:当我们引用第三方jar包时,@Component标注的类是无法注入到spring容器中的,这时我们可以用@EnableConfigurationProperties来代替@Component

ReadProperties

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
/**
 * 1)注解@ConfigurationProperties中的prefix用于设置前缀
 * 2)下方的属性名称必须和要获取的配置信息名称一致,比如必须叫port,否则获取值为null
 */
@ConfigurationProperties(prefix = "server")//这个注解是用找到类
@Component  //生效的两种方式:方式1:配置@Component,方式2:启动类添加@EnableConfigurationProperties(ReadProperties.class)
@Data
public class ReadProperties {
    private Integer port;
}

Controller

import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
 
/**
 * 测试读取配置文件的几种方式:
 * @Author 211145187
 * @Date 2022/7/20 14:02
 **/
@RestController
public class ReadApplicationProperties {
    @Autowired
    private ReadProperties readProperties;
 
	//测试方式3:通过@ConfigurationProperties注解读取配置信息
    @GetMapping("/readApplicationProperties3")
    public void readApplicationProperties3(){
        System.out.println("通过@ConfigurationProperties注解读取配置信息:" + readProperties.getPort());
    }
}