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

这篇具有很好参考价值的文章主要介绍了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);
   }

}

Spring Boot读取yml或者properties配置信息,问题汇总,spring boot,数据库,后端

方法二:通过注解@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);
    }
}

Spring Boot读取yml或者properties配置信息,问题汇总,spring boot,数据库,后端

方法三:通过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);
    }
}

Spring Boot读取yml或者properties配置信息,问题汇总,spring boot,数据库,后端文章来源地址https://www.toymoban.com/news/detail-629606.html

到了这里,关于Spring Boot读取yml或者properties配置信息的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java-读取配置文件自定义字段(yml、properties)

    在springboot项目配置文件中设置自定义字段,项目代码按需读取,想换重要参数时直接更改配置文件即可,这篇文章说一说配置文件自定义字段的方法。 方法1 @Value 使用org.springframework.beans.factory.annotation包下的@Value注解读取yml文件里面的字段,代码如下: yml文件 读取 测试结果

    2024年02月08日
    浏览(53)
  • 【SpringBoot】两种配置文件, 详解 properties 和 yml 的语法格式, 使用方式, 读取配置

    各位读者好, 我是小陈, 这是我的个人主页, 希望我的专栏能够帮助到你: 📕 JavaSE基础: 基础语法, 类和对象, 封装继承多态, 接口, 综合小练习图书管理系统等 📗 Java数据结构: 顺序表, 链表, 堆, 二叉树, 二叉搜索树, 哈希表等 📘 JavaEE初阶: 多线程, 网络编程, TCP/IP协议, HTTP协议

    2024年02月10日
    浏览(40)
  • SpringBoot中的yml文件中读取自定义配置信息

    开发中遇到的问题,百度的答案我都没有找到,去找大佬获取到的经验总结,这只是其中的一种方法,如果其他大佬有新的方法,可以分享分享。 1.1 yml文件 自定义配置信息,通过我们编写的代码读取。 1.2 类 非静态属性中@Value生效 ps: 不是在控制层中拥有@Controller注解,说明

    2024年02月07日
    浏览(45)
  • SpringBoot支持bootstrap.yml/bootstrap.properties配置文件,需要导入spring-cloud-starter-bootstrap依赖

    说明: 在SpringBoot 2.4.x版本之后,对于bootstrap.properties/bootstrap.yaml配置文件的支持,需要导入如下依赖: 注意: 1、导入依赖后,还需要开启 spring.cloud.bootstrap.enabled=true 2、spring cloud starter alibaba nacos config,引用这个配置中心的依赖后,需要使用bootstrap.yml或bootstrap.properties 作

    2024年02月13日
    浏览(41)
  • Java实战:Spring Boot application.yml配置文件详解

    本文将详细介绍Spring Boot application.yml 配置文件的使用和配置项。我们将探讨 application.yml 文件的基本概念,以及如何使用它来配置Spring Boot应用程序的各个方面。此外,我们将通过具体的示例来展示如何配置不同的Spring Boot组件,如数据源、数据库、缓存、邮件服务等。本文适

    2024年04月24日
    浏览(34)
  • 在Spring Boot微服务使用jasypt-spring-boot加密和解密yml配置文件

    记录 :424 场景 :在Spring Boot微服务,使用jasypt-spring-boot加密和解密yml配置文件中的配置信息。 版本 :JDK 1.8,Spring Boot 2.6.3,jasypt-1.9.3,jasypt-spring-boot-2.1.2, jasypt-spring-boot-3.0.5。 开源地址 :https://github.com/ulisesbocchio/jasypt-spring-boot 1.在Spring Boot微服务使用jasypt-spring-boot-3.0.5版本

    2024年02月09日
    浏览(59)
  • 聊聊Spring Boot配置文件:优先级顺序、加载顺序、bootstrap.yml与application.yml区别详解

    在 Spring Boot 中,配置文件的优先级顺序是: application-{profile}.yml ( application-{profile}.properties ) application.yml ( application.properties ) bootstrap.yml ( bootstrap.properties )。其中, {profile} 表示不同的环境配置,如 dev 、 test 、 prod 等。 优先级从高到低,高优先级的配置覆盖低优先级

    2024年01月25日
    浏览(47)
  • Spring Boot微服务从yml文件中加载配置(使用@Value和@ConfigurationProperties)

    记录 :398 场景 :在Spring Boot的微服务中从application.yml等yml文件中加载自定义配置内容。使用@Value直接加载单个配置。使用@ConfigurationProperties注解把一个或者多个配置加载为Java对象。 版本 :JDK 1.8,SpringBoot 2.6.3 1.使用@Value注解加载配置 使用注解@RestController、@Service、@Component等

    2024年02月12日
    浏览(30)
  • Spring Boot Application.properties和yaml配置文件

    全局配置文件能够对一些默认配置值进行修改。Spring Boot使用一个application.properties或者application.yaml的文件作为全局配置文件,该文件存放在src/main/resource目录或者类路径的/config,一般会选择resource目录。 使用Spring Initializr方式创建项目——PropertiesDemo 单击【Next】按钮 添加W

    2024年01月24日
    浏览(59)
  • 社区版Intellij IDEA安装Spring Boot Assistant插件解决yml无提示问题

        如题所示,我们如果个人使用免费社区版的IDEA,它缺失了很多功能,使用起来没有专业版那么强大,比如无法直接创建springbootinit项目,spring配置yml文件没有提示。但是可以通过安装一些插件,比如spring boot helper 、spring boot assistant插件来分别解决这两个问题。     下面

    2023年04月18日
    浏览(50)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包