SpringBoot项目实现配置分离

简介: SpringBoot项目实现配置分离

SpringBoot项目实现配置分离



spring-boot=2.2.5.RELEASE



实现原理

SpringBoot 项目读取配置文件顺序:(可用于实现伪配置文件分离)


当前项目config目录下 > 当前项目根目录下 > 类路径config目录下 > 类路径根目录下


pom 配置


<?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>org.example</groupId>
    <artifactId>spring-boot-package</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <finalName.text>spring-boot-package</finalName.text>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-resources-plugin -->
        <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin -->
        <maven.jar.plugin.version>3.2.0</maven.jar.plugin.version>
        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-dependency-plugin -->
        <maven.dependency.plugin.version>3.1.2</maven.dependency.plugin.version>
        <!--https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
        <spring-cloud-dependencies.version>Hoxton.RELEASE</spring-cloud-dependencies.version>
        <!--https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <fastjson.version>1.2.62</fastjson.version>
        <!--https://mvnrepository.com/artifact/com.google.guava/guava -->
        <guava.version>28.2-jre</guava.version>
        <!--https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
        <hutool-all.version>5.1.3</hutool-all.version>
    </properties>
    <!--spring-boot 和 spring-cloud 版本兼容参考 https://spring.io/projects/spring-cloud -->
    <!-- Inherit defaults from Spring Boot -->
    <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!--Core starter, including auto-configuration support, logging and YAML-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- spring-cloud-context,使 bootstrap.properties 配置文件生效 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
        </dependency>
        <!--Starter for using Spring Boot's Actuator which provides production ready features to help you monitor and manage your application-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--Spring Boot Configuration Processor-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
        <!--Spring Boot Developer Tools-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 表示依赖不会传递 -->
        </dependency>
        <!--Common tools starter-->
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool-all.version}</version>
        </dependency>
        <!--Common tools end -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>${finalName.text}</finalName>
        <plugins>
            <!--The Compiler Plugin is used to compile the sources of your project.-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!--The Resources Plugin handles the copying of project resources to the output directory.
                SpringBoot项目读取配置文件顺序:(可用于实现伪配置文件分离)
                当前项目config目录下>当前项目根目录下>类路径config目录下>类路径根目录下,
            -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>${maven-resources-plugin.version}</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!--Spring Boot Maven Plugin-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--main of springboot project-->
                    <mainClass>top.simba1949.Application</mainClass>
                    <!-- 如果没有该配置,devtools不会生效 -->
                    <fork>true</fork>
                    <!--将项目注册到linux服务上,可以通过命令开启、关闭以及伴随开机启动等功能-->
                    <executable>true</executable>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--Maven Surefire MOJO in maven-surefire-plugin.-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!--跳过测试-->
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <!--docker的maven插件,详情请见 https://blog.csdn.net/SIMBA1949/article/details/83064083-->
        </plugins>
        <!--IDEA是不会编译src的java目录的文件,如果需要读取,则需要手动指定哪些配置文件需要读取-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>
目录
相关文章
|
4天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的软件工程项目管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的软件工程项目管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
7天前
|
JavaScript Java 测试技术
基于小程序的家政项目小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
基于小程序的家政项目小程序+springboot+vue.js附带文章和源代码设计说明文档ppt
21 0
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的项目监管系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的项目监管系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
3天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的程序设计实践项目管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的程序设计实践项目管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
4天前
|
JavaScript Java 测试技术
基于springboot+vue.js的大学生科创项目在线管理系统附带文章和源代码设计说明文档ppt
基于springboot+vue.js的大学生科创项目在线管理系统附带文章和源代码设计说明文档ppt
12 1
|
4天前
|
JavaScript Java 测试技术
基于springboot+vue.js的科研项目验收管理系统附带文章和源代码设计说明文档ppt
基于springboot+vue.js的科研项目验收管理系统附带文章和源代码设计说明文档ppt
12 0
|
6天前
|
JavaScript Java 关系型数据库
springboot+vue毕业生信息招聘平台源码(前后端分离项目)
这是一个基于SpringBoot的毕业生信息招聘平台的项目介绍。该项目提供管理员、企业和毕业生三个角色的功能模块,包括但不限于: - 管理员可管理企业信息、空中宣讲会、招聘岗位、毕业生、个人简历、信息咨询、线上面试、面试回复、论坛和考试。 - 企业可管理个人信息、空中宣讲会、招聘岗位、信息咨询、线上面试、面试回复和考试记录。 - 毕业生可管理个人简历、求职信息、信息咨询、线上面试、面试回复、收藏和考试。
|
6天前
|
JavaScript Java 关系型数据库
springboot+vue闲一品交易平台源码(前后端分离项目)
本文是关于一个基于SpringBoot的闲一品交易平台的介绍。项目由Java开发者风歌分享,提供各类Java毕设项目,包括SSM、SpringBoot等框架的源码。开发环境包括Java 1.8、SpringBoot、MySQL 5.7+、Navicat11等。系统分为管理员、用户和前台首页三个功能模块,涵盖用户管理、零食信息管理、订单评价、零食资讯、轮播图管理、订单管理等多个子功能。文章还展示了部分界面截图。如需了解更多详情或获取项目源码,可联系作者风歌。
|
6天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的设计项目管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的设计项目管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
6天前
|
JavaScript Java 测试技术
基于h5移动网赚项目+springboot+vue.js附带文章和源代码设计说明文档ppt
基于h5移动网赚项目+springboot+vue.js附带文章和源代码设计说明文档ppt
9 0