分析
SpringBoot分别提供3中方式读取项目的application.properties配置文件的内容。这个方式分别为:Environment类、@Value注解以及@ConfigurationProperties注解。
你必须要知道的事情:下面提供的三种方式,都可以拿到配置文件的信息,不要纠结那种方式好与坏。只要能解决问题就可以了。
01、Environment获取属性值
Environment是用来读取应用程序运行时的环境变量的类,可以通过key-value的方式读取application.properties和系统环境变量,命令行输入参数,系统属性等,具体如下:
在application.yml文件定义如下:
# 属性配置类的
server:
  port: 8082
spring:
  main:
    banner-mode: console
# 自定义
alipay:
  pay:
    appid: 123456
    notify: http://www.xxx.com
定义读取的类如下:
package com.kuangstudy.web.properties;
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;
import java.util.Properties;
import java.util.Set;
/**
 * Description:
 * Author: yandi Administrator
 * Version: 1.0
 * Create Date Time: 2021/12/11 21:25.
 * Update Date Time:
 *
 * @see
 */
@RestController
public class ReadPropertiesEnvironment {
    @Autowired
    private Environment environment;
    @GetMapping("/read/file")
    public Map<String,Object> readInfo(){
        Map<String,Object> map = new HashMap<>();
        map.put("port",environment.getProperty("server.port"));
        map.put("appid",environment.getProperty("alipay.pay.appid"));
        map.put("notify",environment.getProperty("alipay.pay.notify"));
        map.put("javaversion",environment.getProperty("java.version"));
        map.put("javahome",environment.getProperty("JAVA_HOME"));
        map.put("mavenhome",environment.getProperty("MAVEN_HOME"));
        return  map;
    }
    public static void main(String[] args) {
        Properties properties = System.getProperties();
        Set<String> strings = properties.stringPropertyNames();
        for (String string : strings) {
            System.out.println(string+"===>"+properties.get(string));
        }
    }
}
在浏览器访问
http://localhost:8082/read/file

02、读取配置文件属性-@Value
使用@Value注解读取配置文件内容,具体如下:
package com.kuangstudy.web.properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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;
import java.util.Properties;
import java.util.Set;
/**
 * Description:
 * Author: yandi Administrator
 * Version: 1.0
 * Create Date Time: 2021/12/11 21:25.
 * Update Date Time:
 *
 * @see
 */
@RestController
public class ReadPropertiesValue {
    @Value("${server.port}")
    private Integer port;
    @Value("${alipay.pay.appid}")
    private String appid;
    @Value("${alipay.pay.notify}")
    private String notify;
    @Value("${java.version}")
    private String javaVersion;
    @Value("${JAVA_HOME}")
    private String javaHome;
    @Value("${MAVEN_HOME}")
    private String mavenHome;
    @GetMapping("/read/value")
    public Map<String, Object> readInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("port", port);
        map.put("appid", appid);
        map.put("notify", notify);
        map.put("javaversion", javaVersion);
        map.put("javahome", javaHome);
        map.put("mavenhome", mavenHome);
        return map;
    }
}
浏览器如下:

结论:其实@Value底层就是Environment.java
03、读取配置文件属性- @ConfigurationProperties
使用@ConfigurationProperties首先建立配置文件与对象的映射关系,然后在控制器方法中使用@Autowired注解将对象注入。具体如下:
01、在application.yml自定义属性
# 自定义属性
ksd:
  alipay:
    appid: 2021003100625328
    mkey: MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC9kGK4VMbYm
    ckey: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx5i5LhEtDZw6Q6mUxkC5f6sAvZm9OncAkRXwfoBdDeKk
    callback: https://www.txnh.net/api/alipay/returnUrl
    charset: UTF-8
02、定义属性配置类和属性类
属性配置类如下:
package com.kuangstudy.properties.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
 * Description:
 * Author: yandi Administrator
 * Version: 1.0
 * Create Date Time: 2021/12/14 20:53.
 * Update Date Time:
 *
 * @see
 */
@ConfigurationProperties(prefix = "ksd.alipay")//这个注解是用找到类
@Data
public class AlipayProperties {
    private String appid;
    private String mkey;
    private String ckey;
    private String callback;
    private String charset ="UTF-8";
}
属性配置类,一定要进行注册在配置类中,如下:
package com.kuangstudy.properties.config;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
/**
 * Description:
 * Author: yandi Administrator
 * Version: 1.0
 * Create Date Time: 2021/12/14 20:53.
 * Update Date Time:
 *
 * @see
 */
// 用配置类去注册:属性配置类
@EnableConfigurationProperties(AlipayProperties.class)
@SpringBootConfiguration
public class AlipayConfiguration {
}
03、属性配置类使用
package com.kuangstudy.properties;
import com.kuangstudy.properties.config.AlipayProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Description:
 * Author: yandi Administrator
 * Version: 1.0
 * Create Date Time: 2021/12/14 20:36.
 * Update Date Time:
 *
 * @see
 */
@RestController
@Slf4j
public class AlipayController2 {
    @Autowired
    private AlipayProperties alipayProperties;
    @GetMapping("/alipay2")
    public String alipay2() {
        log.info("你支付是的:{}", alipayProperties);
        return "success";
    }
}
04、关于自定义属性的自动提示问题和警告问题

解决步骤如下:
01、在pom.xml进行以来自动提示procossor
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
02、把idea打开的application.yml文件务必一定要关闭

03、然后在重新打开application.yml
就可以出现自动提示,警告也会消失,然后输入ksd.就会出现自动提示
总结
 1、Environment @Value 不具有面向对象的特征,属性过多就不方便管理和控制,太多就会现得杂乱无章。而且也没用自动提示的功能。
 2、@ConfigurationProperties 就是面向对象的机制,可以自动提示。
 所以底层springboto用的是@ConfigurationProperties
 3、当然在开发中,你用那种都可以获取到相同的效果,根据业务选择最佳的方式即可。
