使用spring+maven不同环境读取配置方式

 更新时间:2023年08月29日 17:14:06   作者:小淼同学  
这篇文章主要介绍了使用spring+maven不同环境读取配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

首先这个我看了网上很多资料,但我发现,由于自己一些技术的不熟悉,对于他人的文章有些误解,导致我打包部署失败。

话不多说,现在我们开始一步步工程

第一步,项目读取不通环境的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- 测试环境配置文件 -->
    <beans profile="local">
        <context:property-placeholder location="classpath:local/config.properties" />
    </beans>
    <!-- 生产环境配置文件 -->
    <beans profile="product">
        <context:property-placeholder location="classpath:product/config.properties" />
    </beans>
</beans>

这是我定义的一个读配置文件的xml——applicationContext-profile.xml

这是由web.xml里面配置,来区分去读哪个配置文件

 <!-- 配置spring的默认profile
         可选值:product(生产环境) local(本地环境)  -->
  <context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>product</param-value>
  </context-param>
  <!-- spring hibernate -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
          classpath:public/spring/applicationContext-profile.xml
          classpath:public/spring/spring-hibernate.xml
    </param-value>
  </context-param>

web.xml的一部分,根据配置去读product还是local,这里根据配置字段能看到对于的配置文件,这对应关系应该是能看懂的!

到这里,你应该能根据自己的配置文件去运行程序

第二步,根据pom文件打包

<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">
    <parent>
        <artifactId>officialProject</artifactId>
        <groupId>com.wm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>offical-web</artifactId>
    <packaging>war</packaging>
    <name>offical-web</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <shiro-version>1.3.2</shiro-version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>com.wm</groupId>
            <artifactId>offical-manager-service</artifactId>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.wm</groupId>
            <artifactId>official-manager-core</artifactId>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.wm</groupId>
            <artifactId>offical-common</artifactId>
            <type>jar</type>
        </dependency>
        <!-- mysql数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <!--后台权限控制框架-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>${shiro-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>${shiro-version}</version>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <id>local</id><!-- 本地开发环境 -->
            <properties>
                <package.env>local</package.env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>product</id><!-- 生产环境 -->
            <properties>
                <package.env>product</package.env>
            </properties>
        </profile>
    </profiles>
    <build>
        <finalName>${project.artifactId}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>local/*</exclude>
                    <exclude>product/*</exclude>
                </excludes>
                <includes>
                    <include>public/**/*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>com/wm/back/entity/**/hbm/*.hbm.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <!-- jetty插件 -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.3.8.v20160314</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                        <defaultsDescriptor>src/main/resources/public/webdefault.xml</defaultsDescriptor>
                    </webAppConfig>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <warName>${project.artifactId}</warName>
                    <webResources>
                        <!-- 不同的环境,使用不同的配置文件 -->
                        <resource>
                            <directory>src/main/resources/${package.env}</directory>
                            <targetPath>WEB-INF/classes/${package.env}</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <!-- 公共的配置文件 -->
                        <resource>
                            <directory>src/main/resources/public</directory>
                            <!--targetPath用来指定文件放到哪里-->
                            <targetPath>WEB-INF/classes/public</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <resource>
                            <directory>src/main/webapp/WEB-INF/config</directory>
                            <targetPath>WEB-INF</targetPath>
                            <filtering>true</filtering>
                            <includes>
                                <include>web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

先贴出全部代码,然后再一一作出解释

<profiles>
    <profile>
        <id>local</id><!-- 本地开发环境 -->
        <properties>
            <package.env>local</package.env>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>product</id><!-- 生产环境 -->
        <properties>
            <package.env>product</package.env>
        </properties>
    </profile>
</profiles>

properties里配置的,下文都是可以使用的,我这里配置package.env  ,所以下面使用package.env就是代表local或者product根据我传入的 -P后面的那个值来决定

打包语句是mvn clean package -P +(我传入的值 profile的id)来决定package.env的变量

完整的打包语句是mvn clean package -P local

<activeByDefault>true</activeByDefault>

来决定激活哪个profile

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
            <exclude>local/*</exclude>
            <exclude>product/*</exclude>
        </excludes>
        <includes>
            <include>public/**/*</include>
        </includes>
    </resource>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>com/wm/back/entity/**/hbm/*.hbm.xml</include>
        </includes>
    </resource>
</resources>

在我的理解下,不加resources的话,所有项目里的resources文件都会被加载,但java里面的配置文件可能还是加载不了的

但你定义了resources,你就要把你需要通过加载的文件都要写出来,不然他不会去加载。

我这里把各环境的配置文件都取消了加载,把通用的资源文件进行了加载。

忘记提一句,这样下,项目本地启动的时候会报错,因为你把local或product都去掉了,程序找不到里面的文件。这只是打包的时候才试用

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
        </archive>
        <warName>${project.artifactId}</warName>
        <webResources>
            <!-- 不同的环境,使用不同的配置文件 -->
            <resource>
                <directory>src/main/resources/${package.env}</directory>
                <targetPath>WEB-INF/classes/${package.env}</targetPath>
                <filtering>true</filtering>
            </resource>
            <!-- 公共的配置文件 -->
            <resource>
                <directory>src/main/resources/public</directory>
                <!--targetPath用来指定文件放到哪里-->
                <targetPath>WEB-INF/classes/public</targetPath>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/webapp/WEB-INF/config</directory>
                <targetPath>WEB-INF</targetPath>
                <filtering>true</filtering>
                <includes>
                    <include>web.xml</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>

打war包的时候需要这样配置

  • directory:代表你要加载的目录
  • targetPath:代表你要把你加载的文件放在哪里
<filtering>true</filtering>

这个网上说一定要设置为true,在我看来没什么关系,因为我根据命令打包,也能把文件打出来

 <resource>
                <directory>src/main/webapp/WEB-INF/config</directory>
                <targetPath>WEB-INF</targetPath>
                <filtering>true</filtering>
                <includes>
                    <include>web.xml</include>
                </includes>
            </resource>

我这里是为了把web.xml选择读哪个配置文件给替换掉

为什么要这样设计能,因为如果不这样,我们本地启动项目,pom是不会吧${package.env}这个替换的。所以这样会报错。

但我们打包的时候希望灵活替换。所以就写一个config文件目录,里面放过web.xml,当要打包的时候替换WEB-INF下的web.xml文件就可以了

到此,一个完整的流程就走完了

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java中的排序与内部比较器Compareable解析

    Java中的排序与内部比较器Compareable解析

    这篇文章主要介绍了Java中的排序与内部比较器Compareable解析,一般没有特殊要求时,直接调用(底层默认的升序排列)就可以得到想要的结果,所谓的 sort 方法排序底层都是基于这两种排序,故如果需要设计成所想要的排序就需要了解底层排序原理,需要的朋友可以参考下
    2023-11-11
  • Spring事务处理原理步骤详解

    Spring事务处理原理步骤详解

    这篇文章主要介绍了Spring事务处理原理步骤详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • SpringBoot2.x集成Dozer的示例代码

    SpringBoot2.x集成Dozer的示例代码

    本文主要介绍了SpringBoot2.x集成Dozer的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • SpringBoot整合Milvus的实现

    SpringBoot整合Milvus的实现

    本文主要介绍了SpringBoot整合Milvus的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • Java垃圾收集之对象存活判定、回收流程与内存策略详解

    Java垃圾收集之对象存活判定、回收流程与内存策略详解

    Java对象的内存分配主要发生在堆内存(新生代、老年代),少数情况可能分配在栈(栈上分配)或直接内存(堆外内存),这篇文章主要介绍了Java垃圾收集之对象存活判定、回收流程与内存策略的相关资料,需要的朋友可以参考下
    2026-03-03
  • 在Spring AI 中配置多个 LLM 客户端的详细过程

    在Spring AI 中配置多个 LLM 客户端的详细过程

    本文探讨了如何在单个Spring AI应用中集成多个LLM,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2025-10-10
  • SpringCloud集成MybatisPlus实现MySQL多数据源配置方法

    SpringCloud集成MybatisPlus实现MySQL多数据源配置方法

    本文详细介绍了SpringCloud集成MybatisPlus实现MySQL多数据源配置的方法,包括在application.properties中配置多数据源,配置MybatisPlus,创建Mapper接口和使用多数据源等步骤,此外,还解释了每一个配置项目的含义,以便读者更好地理解和应用
    2024-10-10
  • javaWeb使用验证码实现简单登录

    javaWeb使用验证码实现简单登录

    这篇文章主要为大家详细介绍了javaWeb使用验证码实现简单登录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • java获取当前时间并格式化代码实例

    java获取当前时间并格式化代码实例

    这篇文章主要介绍了java获取当前时间并格式化代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • 在Java的JDBC使用中设置事务回滚的保存点的方法

    在Java的JDBC使用中设置事务回滚的保存点的方法

    这篇文章主要介绍了在Java的JDBC使用中设置事务回滚的保存点的方法,JDBC是Java用于连接各种数据库的API,需要的朋友可以参考下
    2015-12-12

最新评论