学习spring boot2的一些记录1

最近接了个私活.....emmmmmm, 我一个Android工程师现在写上了后台代码,也是贫穷限制呀。不过话说spring boot还是挺好玩的。接下来会更新一些学到的东西。
spring boot版本:2.1.1.RELEASE

今天记录一下关于application.yml配置文件在多 环境下切换的小技巧。开发一开始都在release上开发,现在系统交付推上线了,就不敢乱玩了,用配置文件区分一个测试数据库,还有一些其他东西吧。

首先,你需要把基础的东西都写在resource/application.yml里面,比如这些基础的

server:
  port: 8080
  tomcat:
    uri-encoding: UTF-8  

spring:
  profiles:
    active: #spring.profiles.active#
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    database: MYSQL
    show-sql: true
    hibernate:
      ddl-auto: update
      naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
      dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect

然后你在同目录下写一个application-dev,再写一个application-release,这两个里面就写上差异部分的配置就行了,比如数据库连接测试数据库啥的。我放一个例子吧,以免自己最后忘了

spring:
  datasource:
    url: jdbc:mysql://xxxxx:3306/theater?characterEncoding=UTF-8
    username: user
    password: pass

这样就可以了。就把差异的部分拆分出来放就好。

接下来配置一下pom.xml, 这个用于替换掉你在application中的那一串占位符为你想要的版本。先增加profiles字段

	<!-- 环境配置 -->
	<profiles>
	    <!-- 开发环境 -->
	    <profile>
	        <id>dev</id>
	        <properties>
	            <spring.profiles.active>dev</spring.profiles.active>
	        </properties>
	        <activation>
	            <activeByDefault>true</activeByDefault>
	        </activation>
	    </profile>
	    <!-- 生产环境 -->
	    <profile>
	        <id>release</id>
	        <properties>
	            <spring.profiles.active>release</spring.profiles.active>
	        </properties>
	    </profile>
	</profiles>

然后在plugins下增加一个插件,用于替换

			<!-- maven打包插件 -->
			<plugin>
			    <groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-resources-plugin</artifactId>
			    <version>2.7</version>
			    <executions>
			        <execution>
			            <id>default-resources</id>
			            <phase>validate</phase>
			            <goals>
			                <goal>copy-resources</goal>
			            </goals>
			            <configuration>
			                <outputDirectory>target/classes</outputDirectory>
			                <useDefaultDelimiters>false</useDefaultDelimiters>
			                <delimiters>
			                    <delimiter>#</delimiter>
			                </delimiters>
			                <resources>
			                    <resource>
			                        <directory>src/main/resources/</directory>
			                        <filtering>true</filtering>
			                    </resource>
			                    <resource>
			                        <directory>src/main/resources.${spring.profiles.active}</directory>
			                        <filtering>false</filtering>
			                    </resource>
			                </resources>
			            </configuration>
			        </execution>
			    </executions>
			</plugin>

这样就可以了。最后用命令打包,分别对应两个环节

sh .\mvnw package -P release
sh .\mvnw package -P dev

最后用java -jar执行就可以了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值