spring-boot 读取配置文件中的内容
目录
前言
此系列博客是个人在学习springBoot的学习笔记,算是一个记录吧,一方面记录学习的内容,一方面记录学习中犯下的错误。学习springBoot选择的是GitHub上一个比较著名的项目,链接如下https://github.com/xkcoding/spring-boot-demo.git。这是一个从易到难的项目,目标:完成本地实现。技术差,文笔差,学习能力差,啥都差,欢迎指正,拒绝喷人
一、实现demo
本 demo 演示如何获取配置文件的自定义配置,以及如何多环境下的配置文件信息的获取。实现前大致阅读了下代码,大致有以下几点要注意:
1.additional-spring-configuration-metadata.json这个文件的作用是自定义提示,类似于配置文件中的代码提示,具体用法很多,这个demo并没有展示的很详尽,毕竟这个demo主要是展示配置文件的,一开始我直接忽略了这个文件。建议找博客详细了解下,反正内容不多。
2.有时候idea上方会有红色的提示提示你这个文件没有配置,忽略,不影响,也可以直接关掉。
3.多环境配置读取是如何操作的呢?直接更改application.yml文件中的spring: profiles: active: prod/dev,会从不同配置文件中读取。建议多操作几次
详细内容写在文件注释中。
二、实现步骤
1.新建子模块
新建多模块项目见系列第一篇。
2.代码编写
1.父模块的pom.xml文件:
<?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>
<groupId>com.xkcoding</groupId>
<artifactId>spring-boot-learning-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- 子模块-->
<modules>
<module>spring-boot-demo-helloworld</module>
<module>spring-boot-demo-properties</module>
<!-- 以后才会用先注释掉-->
<!--
<module>spring-boot-demo-actuator</module>
<module>spring-boot-demo-admin</module>
<module>spring-boot-demo-logback</module>
<module>spring-boot-demo-log-aop</module>
<module>spring-boot-demo-exception-handler</module>
<module>spring-boot-demo-template-freemarker</module>
<module>spring-boot-demo-template-thymeleaf</module>
<module>spring-boot-demo-template-beetl</module>
<module>spring-boot-demo-template-enjoy</module>
<module>spring-boot-demo-orm-jdbctemplate</module>
<module>spring-boot-demo-orm-jpa</module>
<module>spring-boot-demo-orm-mybatis</module>
<module>spring-boot-demo-orm-mybatis-mapper-page</module>
<module>spring-boot-demo-orm-mybatis-plus</module>
<module>spring-boot-demo-orm-beetlsql</module>
<module>spring-boot-demo-upload</module>
<module>spring-boot-demo-cache-redis</module>
<module>spring-boot-demo-cache-ehcache</module>
<module>spring-boot-demo-email</module>
<module>spring-boot-demo-task</module>
<module>spring-boot-demo-task-quartz</module>
<module>spring-boot-demo-task-xxl-job</module>
<module>spring-boot-demo-swagger</module>
<module>spring-boot-demo-swagger-beauty</module>
<module>spring-boot-demo-rbac-security</module>
<module>spring-boot-demo-rbac-shiro</module>
<module>spring-boot-demo-session</module>
<module>spring-boot-demo-oauth</module>
<module>spring-boot-demo-social</module>