java八股文面试[SpringBoot]——Spring Boot 有哪几种读取配置的方式

目录

@PropertySource

@Value

@Environment

@ConfigurationProperties

使用原生方式读取配置文件


Spring Boot默认的配置文件有两种格式: application.properties 和 application.yml。 查找顺序是首先从application.properties 查找,

@PropertySource

@PropertySource注解用于指定资源文件读取的位置,它不仅能读取properties文件,也能读取xml文件,并且通过YAML解析器,配合自定义PropertySourceFactory实现解析YAML文件。

@SpringBootApplication
@PropertySource("classpath:application.properties")
public class DemoApplication implements InitializingBean {
    
    @Value("${profile.name}")
    private String name;
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
 
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Name:" + name);
    }
    
}

执行结果如下图所示:

中文乱码
如果配置文件中出现中文乱码的情况,可通过指定编码格式的方式来解决中文乱码的问题,具体实现如下:

@PropertySource(value = "dev.properties", encoding = "utf-8")
注意事项
@PropertySource 注解默认是只支持 properties 格式配置文件的读取的。
 

@Value

使用 @Value 读取配置文件

这种方法适用于对象的参数比较少的情况

我们可以直接在对象的属性上使用 @Value 注解,同时以 ${} 的形式传入配置文件中对应的属性。同时需要在该类的上方使用 @Configuration 注解,将该类作为配置

具体使用方法如下,我们在 Spring Boot 配置文件 application.properties 添加以下内容:

profile.name=Spring Boot Profile
profile.desc=Spring Boot Profile Desc.

 使用 @Value 读取配置文件
使用 @Value 可以读取单个配置项,如下代码所示:

@SpringBootApplication
public class DemoApplication implements InitializingBean {
    
    @Value("${profile.name}")
    private String name;
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("My Profile Name:" + name);
    }

执行结果如下图所示:

@Environment

Environment 是 SpringCore 中的一个用于读取配置文件的类,将此类使用 @Autowired 注入到类中就可以使用它的getProperty方法来获取某个配置项的值。

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
 
@SpringBootApplication
public class DemoApplication implements InitializingBean {
 
    @Autowired
    private Environment environment;
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
 
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Profile Name:" + environment.getProperty("profile.name"));
    }
    
}

执行结果如下图所示:

@ConfigurationProperties

使用 @ConfigurationProperties 读取配置文件 如果对象的参数比较多情况下,推荐使用 @ConfigurationProperties 会更简单一些,不需要在每一个字段的上面的使用@Value注解。

@ConfigurationProperties注解声明当前类为配置读取类

prefix="rabbitmq" 表示读取前缀为rabbitmq的属性

@ConfigurationProperties 和 @Value 的使用略微不同,@Value 是读取单个配置项的,而@ConfigurationProperties 是读取一组配置项的,我们可以使用 @ConfigurationProperties 加实体类读取一组配置项,其中 prefix 表示读取一组配置项的根 name,相当于 Java 中的类名,最后再把此配置类,注入到某一个类中就可以使用了,如下代码所示:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
@Component
@ConfigurationProperties(prefix = "profile")
@Data
public class Profile {
    private String name;
    private String desc;
}
@SpringBootApplication
public class DemoApplication implements InitializingBean {
    
    @Autowired
    private Profile profile;
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
 
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Profile Object:" + profile);
    }
    
}

执行结果如下图所示:

使用原生方式读取配置文件

我们还可以使用最原始的方式 Properties 对象来读取配置文件,如下代码所示:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
 
@SpringBootApplication
public class DemoApplication implements InitializingBean {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
 
    @Override
    public void afterPropertiesSet() throws Exception {
        Properties props = new Properties();
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(
                    this.getClass().getClassLoader().getResourceAsStream("application.properties"),
                    StandardCharsets.UTF_8);
            props.load(inputStreamReader);
        } catch (IOException e1) {
            System.out.println(e1);
        }
        System.out.println("Properties Name:" + props.getProperty("profile.name"));
    }
    
}

执行结果如下图所示:


知识来源:马士兵教育

SpringBoot 读取配置文件的 5 种方法_springboot 获取配置文件中的值_ZwLemon的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小田田_XOW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值