spring boot 读取配置文件信息的 demo 示例

1. 描述

一个spring boot 读取配置文件信息的 demo示例。

环境:

IDE(idea)2021.3
JDK:1.8
maven:3.8.4
spring boot:2.5.6

2. 结果

  • 接口返回 读取的配置文件信息
    在这里插入图片描述

3. demo

3.0 项目结构
在这里插入图片描述

3.1 pom.xml

  • 引入 web jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.byrc</groupId>
        <artifactId>byrc-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>demo-properties</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>${project.artifactId}</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--提供全栈的 web 开发特性,包括 Spring MVC 依赖和 Tomcat 容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- hutool工具类 -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <!-- lombok工具类(编译注解) -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- 提供通用单元测试依赖,包括 JUnit, Hamcrest , Mockito-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--
           在 META-INF/additional-spring-configuration-metadata.json 中配置
           可以去除 application.yml 中自定义配置的红线警告,并且为自定义配置添加 hint 提醒
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.2 SpringBootApplication 注解实现启动类

@SpringBootApplication
public class DemoPropertiesApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoPropertiesApplication.class, args);
        System.out.println("----- http://localhost:8880/demo/ -----");
    }
}

3.3 application.yml配置

  • 端口和应用路径
  • 应用信息和开发信者息(演示读取配置使用)

application.yml

server:
  port: 8880
  servlet:
    context-path: /demo

spring:
  profiles:
    active: dev
  application:
    name: ${project.artifactId}

application-dev.yml

application:
  name: dev环境 @artifactId@
  version: dev环境 @version@
developer:
  name: dev环境 byrc
  website: dev环境 http://byrced.com
  qq: dev环境 35174
  phone-number: dev环境 13517466666

3.4 其他代码

  • ApplicationProperty.java
@Getter
@Component
public class ApplicationProperty {

    @Value(value = "${application.name}")
    private String name;

    @Value(value = "${application.version}")
    private String version;
}
  • DeveloperProperty.java
@Data
@ConfigurationProperties(prefix = "developer")
@Component
public class DeveloperProperty {
    private String name;
    private String website;
    private String qq;
    private String phoneNumber;
}
  • PropertyController.java
@RestController
@RequestMapping("/property")
public class PropertyController {

    private final ApplicationProperty applicationProperty;
    private final DeveloperProperty developerProperty;
    @Resource
    private Environment env;

    @Autowired
    public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) {
        this.applicationProperty = applicationProperty;
        this.developerProperty = developerProperty;
    }

    @GetMapping("/property")
    public Dict getProperty() {
        return Dict.create().set("applicationProperty", applicationProperty).set("developerProperty", developerProperty);
    }

    @GetMapping("/env/{path}")
    public Dict getProperty(@PathVariable String path) {
        return Dict.create().set(path, env.getProperty(path));
    }
}

4. 资料

官网:https://spring.io/projects/spring-boot/

5. 注

  • spirng boot 已在父项目引入(父项目pom.xml配置);
  • 部分jar包版本已在父项目管理(如果对应不上,一定、肯定、决定是某些修改,没同步更新文档)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值