Spring Boot 配置读取方式详解

一般而言,在Spring Boot项目中,都会有application.yml或者application.properties配置或者其它文件下面的properties文件配置,下面笔者就来说说读取这些配置文件的几种方式:

首先,笔者的application.yml(application.properties读取方式一致)文件中有如下内容:

book:
  title: test
  price: 100
  sell: 200

demo: demo2018

方式一、 通过@ConfigurationProperties注解的方式

package com.demo.demo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "book")
public class TestConfig {
    
    private String price;
    private String title;
    private String sell;

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSell() {
        return sell;
    }

    public void setSell(String sell) {
        this.sell = sell;
    }
}

这样我们就能获取到配置文件中price、title、sell的值了。

 

方式二: 通过@Value注解的方式

package com.demo.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class TestValue {
    @Value("${book.price}")
    private String price;
    @Value("${book.title}")
    private String title;
    @Value("${book.sell}")
    private String sell;

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSell() {
        return sell;
    }

    public void setSell(String sell) {
        this.sell = sell;
    }
}

 

如果我们想读取其它文件夹的配置文件,该如何读取呢:

比如,在src/main/resources下有config/test.properties文件,内容如下:

book.one=1
book.two=2

我们也有几种方式可以读取:

方式一: @Value + @PropertySource组合(仅支持properties文件)

package com.demo.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("config/test.properties")
public class TestOtherProperties {

    @Value("${book.one}")
    private String bookOne;
    @Value("${book.two}")
    private String bookTwo;

    public String getBookOne() {
        return bookOne;
    }

    public void setBookOne(String bookOne) {
        this.bookOne = bookOne;
    }

    public String getBookTwo() {
        return bookTwo;
    }

    public void setBookTwo(String bookTwo) {
        this.bookTwo = bookTwo;
    }
}

 

方式二: @PropertySource +  @ConfigurationProperties组合

package com.demo.demo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("config/test.properties")
@ConfigurationProperties(prefix = "book")
public class TestProperties {
    
    private String one;
    private String two;

    public String getOne() {
        return one;
    }

    public void setOne(String one) {
        this.one = one;
    }

    public String getTwo() {
        return two;
    }

    public void setTwo(String two) {
        this.two = two;
    }
}

欢迎关注笔者的公众号:Java互通,或下方扫描二维码, 每天都有干货分享,你还在等什么~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值