美文网首页
SpringBoot(6) — 自定义.properties 格

SpringBoot(6) — 自定义.properties 格

作者: 奔跑的佩恩 | 来源:发表于2021-06-28 10:18 被阅读0次

前言

在上一节中,我们讲述了如何创建一个自定义配置文件。有兴趣的同学可参看
SpringBoot(5) — 新建自定义配置文件
我们也知道配置文件有两种格式: .properties.yml。那么今天就让我们来学习下自定义.properties 格式配置文件的使用吧。

今天涉及内容:

  1. 添加依赖
  2. 新建编写配置文件
  3. bean读取自定义配置文件
  4. controller调用bean
  5. 运行验证

给出一波效果图


image.png

一. 添加依赖

pom.xml中添加bean读取自定义配置文件中属性的依赖:

        <!-- 自定义配置文件注解依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

二. 新建编写配置文件

新建自定义配置文件的知识上节已经讲过,这里不再赘述。然后这里我在src/main/resources/文件夹下新建了一个配置文件my.properties,其中添加了几个属性。my.properties代码如下:

big.name: 西园寺世界
big.age: 17

三.bean读取自定义配置文件

新建Java bean-----My.class,这里因为是接收自定义配置文件my.properties中的值,而不是项目自带的配置文件application.properties中的值,所以My.class上的注解有所不同,需要添加以下三个注解:

相关文章

网友评论

      本文标题:SpringBoot(6) — 自定义.properties 格

      本文链接:https://www.haomeiwen.com/subject/ajbwyltx.html