读取properties配置文件中的数据

Spring Boot最常用的3种读取properties配置文件中数据的方法:

 

1.使用@Value注解读取

直接通过@Value注解注入一个配置信息到Spring管理的 Bean中。默认读取的是application.properties。

 application.properties

demo.name=Name
demo.age=18

java代码

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class GatewayController {
 
    @Value("${demo.name}")
    private String name;
 
    @Value("${demo.age}")
    private String age;
 
    @RequestMapping(value = "/gateway")
    public String gateway() {
        return "get properties value by ''@Value'' :" +
                //1、使用@Value注解读取
                " name=" + name +
                " , age=" + age;
    }
}

运行结果如下:

 

部分放到一个单独的类A中进行读取,然后在类B中调用,则要把类A增加@Component注解,并在类B中使用@Autowired自动装配类A,代码如下。

类A:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component
public class ConfigBeanValue {
 
    @Value("${demo.name}")
    public String name;
 
    @Value("${demo.age}")
    public String age;
}

类B:

import cn.wbnull.springbootdemo.config.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class GatewayController {
 
    @Autowired
    private ConfigBeanValue configBeanValue;
 
    @RequestMapping(value = "/gateway")
    public String gateway() {
        return "get properties value by ''@Value'' :" +
                //1、使用@Value注解读取
                " name=" + configBeanValue.name +
                " , age=" + configBeanValue.age;
    }
}

运行结果:

2.使用Environment读取

Environment是一个通用的读取应用程序运行时的环境变量的类,可以读取application.properties、命令行输入参数、系统属性、操作系统环境变量等。可以通过Spring容器自动注入,比如:

application.properties:

demo.sex=男
demo.address=山东

Java代码:

import cn.wbnull.springbootdemo.config.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class GatewayController {
 
    @Autowired
    private ConfigBeanValue configBeanValue;
 
    @Autowired
    private Environment environment;
 
    @RequestMapping(value = "/gateway")
    public String gateway() {
        return "get properties value by ''@Value'' :" +
                //1、使用@Value注解读取
                " name=" + configBeanValue.name +
                " , age=" + configBeanValue.age +
                "<p>get properties value by ''Environment'' :" +
                //2、使用Environment读取
                " , sex=" + environment.getProperty("demo.sex") +
                " , address=" + environment.getProperty("demo.address");
    }
}

运行结果如下:

 

3.使用@ConfigurationProperties注解读取

在实际项目中,当项目需要注入的变量值很多时,上述所述的两种方法工作量会变得比较大,这时候我们通常使用基于类型安全的配置方式,将properties属性和一个Bean关联在一起,即使用注解@ConfigurationProperties读取配置文件数据。

在src\main\resources下新建config.properties配置文件:

demo.phone=10086
demo.wife=self

创建ConfigBeanProp并注入config.properties中的值:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
 
@Component
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = "config.properties")
public class ConfigBeanProp {
 
    private String phone;
 
    private String wife;
 
    public String getPhone() {
        return phone;
    }
 
    public void setPhone(String phone) {
        this.phone = phone;
    }
 
    public String getWife() {
        return wife;
    }
 
    public void setWife(String wife) {
        this.wife = wife;
    }
}

@Component 表示将该类标识为Bean

@ConfigurationProperties(prefix = "demo")用于绑定属性,其中prefix表示所绑定的属性的前缀。

@PropertySource(value = "config.properties")表示配置文件路径,不添加的时候默认加载application.properties文件。

 

使用时,先使用@Autowired自动装载ConfigBeanProp,然后再进行取值,示例如下:

import cn.wbnull.springbootdemo.config.ConfigBeanProp;
import cn.wbnull.springbootdemo.config.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class GatewayController {
 
    @Autowired
    private ConfigBeanValue configBeanValue;
 
    @Autowired
    private Environment environment;
 
    @Autowired
    private ConfigBeanProp configBeanProp;
 
    @RequestMapping(value = "/gateway")
    public String gateway() {
        return "get properties value by ''@Value'' :" +
                //1、使用@Value注解读取
                " name=" + configBeanValue.name +
                " , age=" + configBeanValue.age +
                "<p>get properties value by ''Environment'' :" +
                //2、使用Environment读取
                " sex=" + environment.getProperty("demo.sex") +
                " , address=" + environment.getProperty("demo.address") +
                "<p>get properties value by ''@ConfigurationProperties'' :" +
                //3、使用@ConfigurationProperties注解读取
                " phone=" + configBeanProp.getPhone() +
                " , wife=" + configBeanProp.getWife();
    }
}

运行结果如下:

在处理ConfigurationProperties注解的类的时候。自动会将“-”或者“_”去掉,转化为Java命名规范,如将context-path转化为contextPath.

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值