Spring Boot读取yml或者properties配置信息

Spring Boot读取yml或者properties配置信息

方法一:@Value获取基本信息,适用于少量信息

package com.geekmice.springbootselfexercise;

import com.geekmice.springbootselfexercise.config.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;

@Slf4j
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
class SpringBootSelfExerciseApplicationTests {

    @Value("${server.port}")
    private String port;

    @Test
    void contextLoads() {
        log.info("端口号:【{}】",port);
   }

}

在这里插入图片描述

方法二:通过注解@ConfigurationProperties(prefix = “spring.datasource”)

编写配置类

package com.geekmice.springbootselfexercise.config;

import com.sun.media.jfxmedia.logging.Logger;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: spring-boot-self-exercise
 * @BelongsPackage: com.geekmice.springbootselfexercise.config
 * @Author: pingmingbo
 * @CreateTime: 2023-08-05  23:12
 * @Description: TODO
 * @Version: 1.0
 */

@ConfigurationProperties(prefix = "spring.datasource")
@Component
@Data
public class DataSourceProperties {
    private String username;
    private String password;
    private String url;
    private String driverClassName;

}

开始使用

package com.geekmice.springbootselfexercise;

import com.geekmice.springbootselfexercise.config.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;

@Slf4j
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
class SpringBootSelfExerciseApplicationTests {

    @Autowired
    private DataSourceProperties dataSourceProperties;

    @Test
    void contextLoads() {
        String username = dataSourceProperties.getUsername();
        String password = dataSourceProperties.getPassword();
        String url = dataSourceProperties.getUrl();
        String driverClassName = dataSourceProperties.getDriverClassName();
        log.info("用户名:【{}】",username);
        log.info("密码:【{}】",password);
        log.info("地址URL:【{}】",url);
        log.info("驱动类:【{}】",driverClassName);
    }
}

在这里插入图片描述

方法三:通过api Environment

package com.geekmice.springbootselfexercise;

import com.geekmice.springbootselfexercise.config.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;

@Slf4j
@SpringBootTest(classes = SpringBootSelfExerciseApplication.class)
@RunWith(SpringRunner.class)
class SpringBootSelfExerciseApplicationTests {

    @Autowired
    private Environment environment;

    @Test
    public void t1(){
        String username = environment.getProperty("spring.datasource.username");
        String password = environment.getProperty("spring.datasource.password");
        String url = environment.getProperty("spring.datasource.url");
        String driverClassName = environment.getProperty("spring.datasource.driver-class-name");
        log.info("用户名:【{}】",username);
        log.info("密码:【{}】",password);
        log.info("地址URL:【{}】",url);
        log.info("驱动类:【{}】",driverClassName);
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值