springboot 常用配置之多环境配置

在spring mvc中我们都是通过spring.xml相关文件配置,在springboot中这些都已经不存在了,我们应该怎样配置呢?别急,马上为大家揭晓谜底,跟着我一起来吧!

NO1.我们在做项目的时候是不是都会区分很多环境呢?比如开发环境、测试环境、生产环境等,那么第一步我将先带大家配置好各个环境;

maven的过滤资源需要结合maven的2个定义才能实现,分别是:

  • profile:定义一系列的配置信息,然后指定其激活条件

  • resources:指定maven编译资源文件指定到何处的,例如maven的标准资源目录结构是src/main/resources(这个在超级pom中定义到了),maven进行编译时候就会将resources中的资源文件放到web的WEB-INF/classes下

1.首先打开我们项目的pom.xml文件加入以下内容:

<build>
   <finalName>${project.artifactId}-${project.version}</finalName>
   <plugins>

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

      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>3.3</version>
         <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>utf8</encoding>
         </configuration>
      </plugin>

   </plugins>

   <filters>
      <filter>src/main/resources/application-${filter-resource-name}.properties</filter>
   </filters>
   <resources>
      <resource>
         <directory>src/main/resources</directory>
         <filtering>true</filtering>
         <excludes>
            <exclude>filters/*</exclude>
            <exclude>filters/*</exclude>
            <exclude>application-dev.properties</exclude>
            <exclude>application-test.properties</exclude>
            <exclude>application-prod.properties</exclude>
         </excludes>
      </resource>
      <resource>
         <directory>src/main/resources</directory>
         <filtering>true</filtering>
         <includes>
            <include>application-${filter-resource-name}.properties</include>
         </includes>
      </resource>
   </resources>
</build>

<profiles>
   <profile>
      <id>dev</id>
      <activation>
         <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
         <filter-resource-name>dev</filter-resource-name>
      </properties>
   </profile>

   <profile>
      <id>test</id>
      <properties>
         <filter-resource-name>test</filter-resource-name>
      </properties>
   </profile>

   <profile>
      <id>prod</id>
      <properties>
         <filter-resource-name>prod</filter-resource-name>
      </properties>
   </profile>
</profiles>

这一段相信大家都很熟悉了吧,我就不多做解释了(有疑问的童鞋可以私信我哦);

2.然后打开application.properties文件,并在其中加入以下内容:

#表示激活的配置文件(dev|prod)
spring.profiles.active=@filter-resource-name@

整个项目变成了如下结构:

至此我们的springboot多环境配置已经完成,接下来就是打包了;

开发: mvn package -Pdev (因为配置了默认激活dev部分, 所以也可以使用mvn package, 这与 mvn package -Pdev 效果相同)
测试: mvn package -Ptest
预演:mvn package -Pprev
生产:mvn package -Pprod

至此,我们项目的基本环境配置已经搭建好,通过maven clean install以下选择dev|test|prod打入你指定的配置,然后run application运行,如果通过localhost:8888可以访问说明你的配置worked了;但是这还远远不够,我们项目开发总得操作数据库吧,哈哈 是的,接下来让我们进入springboot + mysql + mybatis的世界吧! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值