SpringBoot读取配置文件的几种方式

一、@Value注解读取yml文件配置

1.在application.yml文件中配置信息

aliyun:
  host: 110.121.111.111

2.读取配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class YmlTest {
    @Value("${aliyun.host}")
    private String host;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }
}

3、使用

    @Autowired
    YmlTest test;
    @Test
    void test1(){
        String host = test.getHost();
        System.out.println(host);
    }
}
二、使用@ConfigurationProperties注解读取配置

1、pom.xml引入configuration依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

2、配置application.yml

springcss:
  proints:
    order1: test1
    order2: test2
    order3: test3

3、读取配置

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

@Component
@ConfigurationProperties(prefix = "springcss.proints")
public class OrderAddress {
    private String order1;
    private String order2;
    private String order3;

    public String getOrder1() {
        return order1;
    }

    public void setOrder1(String order1) {
        this.order1 = order1;
    }

    public String getOrder2() {
        return order2;
    }

    public void setOrder2(String order2) {
        this.order2 = order2;
    }

    public String getOrder3() {
        return order3;
    }

    public void setOrder3(String order3) {
        this.order3 = order3;
    }
}


    @Autowired
    OrderAddress orderAddress;
    
    @Test
    void contextLoads() {
        String order1 = orderAddress.getOrder1();
        System.out.println(order1);
    }

三、读取properties资源文件
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.stereotype.Component;

@Component
@PropertySources({
        @PropertySource("classpath:config/mq.properties"),
        @PropertySource("classpath:config/redis.properties")
})
public class OrderAddress {
}

mq.properties

## 防止中文乱码
spring.http.encoding.enabled = true
host = jdbc:mysql://localhost:3306/DatebaseName(数据库))

3、读取配置

    @Autowired
    Environment environment;
    @Test
    void contextLoads() {
        String host = environment.getProperty("host");
        System.out.println(host);
    }

附上Demo目录
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值