SpringBoot maven 配置多环境 & Junit单元测试加载src/main/resources目录下资源文件

SpringBoot项目配置多环境方式

(1) SpringBoot官方推荐的形式properties多环境配置,通过 application.properties 设置 spring.profile.active 的值实现,具体的实现可以参考这篇文章

SpringBoot多环境切换: https://blog.csdn.net/liu911025/article/details/81489117

(2)通过maven配置实现环境配置切换。上面第一种配置文件的切换有一个很不友好的地方,它切换的只是properties不同环境的配置,但是正常使用中,配置文件往往有很多份,比如下图是一个企业级项目的配置文件,这时候就要使用maven配置多环境,在编译的时候指定对应的编译环境

在这里插入图片描述

使用Maven的形式配置项目多环境

 <!--配置环境-->
    <profiles>
        <profile>
            <id>ae</id>
            <properties>
                <profile.env>ae</profile.env>
            </properties>
        </profile>
        <profile>
            <id>jp</id>
            <properties>
                <profile.env>jp</profile.env>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profile.env>test</profile.env>
            </properties>
            <!-- 默认环境 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    </profiles>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <resources>
            <!--指定编译打包包含的resources目录下的配置文件-->
            <resource>
                <directory>src/main/resources/</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.properties</include>
                    <include>*.xml</include>
                    <include>i18n/*</include>
                </includes>
            </resource>
            <!--指定当前环境变量下的配置文件夹目录的配置文件,如果和resources目录下的文件名同名会覆盖-->
            <resource>
                <directory>src/main/resources/${profile.env}</directory>
                <!--必须要增加filtering,会扰乱执行顺序 -->
                <filtering>true</filtering>
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
        </resources>
    </build>

单元测试使用和 src/main/resources 一样的配置文件

没有加以下的配置的时候,@SpringBootTest 注解,只会加载test路径下的资源文件(即xml配置),并不会加载main路径下的资源文件,要使用main路径下的配置文件,需要加上以下的配置,指定testResources使用的配置文件

    <!--和上面的配置一样,放到 build 节点里面 -->
        <!--单元测试时引用src/main/resources下的资源文件-->
        <testResources>
            <testResource>
                <directory>src/main/resources/</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.properties</include>
                    <include>*.xml</include>
                    <include>i18n/*</include>
                </includes>
            </testResource>
             <testResource>
                <directory>src/main/resources/${profile.env}</directory>
            </testResource>
        </testResources>

参考文献

  1. SpringBoot多环境切换
  2. springboot Junit单元测试之坑
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是对应的步骤: 1. 使用 IntelliJ IDEA 新建 Maven Web 项目 ex08。 2. 在 pom.xml 文件的 <dependencies/> 元素中加入相关依赖信息,如下所示: ```xml <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> <!-- Jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.4</version> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- JUnit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> ``` 3. 在 pom.xml 文件的 <plugins/> 元素中加入 tomcat 插件配置信息,如下所示: ```xml <build> <plugins> <!-- Tomcat plugin --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> <path>/</path> </configuration> </plugin> </plugins> </build> ``` 4. 在 src/main/resources 中创建 config 文件夹,并在其中创建 Spring MVC 配置文件 spring-mvc.xml,如下所示: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置组件扫描 --> <context:component-scan base-package="com.example.ex08" /> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置消息转换器 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd HH:mm:ss" /> </bean> </property> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans> ``` 5. 在 web.xml 文件中,加入前端控制器的配置,如下所示: ```xml <web-app version="4.0" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"> <!-- 配置 Spring MVC 前端控制器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` 以上就是使用 IntelliJ IDEA 新建 Maven Web 项目 ex08,并配置 Spring MVC 的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值