时隔半年,再次使用Spring Boot快速搭建微服务,半年前使用的版本是1.2.5,如今看官网最新的release版本是1.4.0,那就用最新的来构建,由于部署环境可能有多套所以使用maven-filter插件,定义多套环境的配置文件,最后使用的时候:
可以采用下面的几个命令来构建不同环境的打包:
Java代码
1. maven clean package -Pdev
2. maven clean package -Ptest
3. maven clean package -Pproduct
项目结构截图如下:
pom依赖如下:
Java代码
1. <?xml versinotallow="1.0" encoding="UTF-8"?>
2. <project xmlns="http:///POM/4.0.0"
3. "http://www.w3.org/2001/XMLSchema-instance"
4. "http:///POM/4.0.0 http:///xsd/maven-4.0.0.xsd">
5. 4.0.0</modelVersion>
6.
7. <groupId>com.monitor.bigscreen</groupId>
8. <artifactId>monitor-bigscreen-sql</artifactId>
9. 1.0.0-SNAPSHOT</version>
10.
11. <properties>
12. 8</project.build.sourceEncoding>
13. 2.3.4</elasticsearch.version>
14. 4.12</junit.version>
15. 1.2.15</fast.json>
16. 1.4.0.RELEASE</spring.boot.version>
17. 1.0.15</druid.version>
18. 5.1.6</jdbc.mysql.version>
19. </properties>
20.
21.
22.
23.
24. <!--<parent>-->
25. <!--<groupId>org.springframework.boot</groupId>-->
26. <!--<artifactId>spring-boot-starter-parent</artifactId>-->
27. <!--<!–此处不能甩变量替代–>-->
28. 1.4.0.RELEASE</version>-->
29. <!--</parent>-->
30. <!--使用上面的父parent或者下面的依赖插件引入父依赖-->
31. <dependencyManagement>
32.
33. <dependencies>
34. <dependency>
35. <groupId>org.springframework.boot</groupId>
36. <artifactId>spring-boot-dependencies</artifactId>
37. <type>pom</type>
38. 1.4.0.RELEASE</version>
39. import</scope>
40. </dependency>
41. </dependencies>
42.
43.
44. </dependencyManagement>
45.
46.
47.
48. <dependencies>
49.
50.
51. <!--连接池druid-->
52. <dependency>
53. <groupId>com.alibaba</groupId>
54. <artifactId>druid</artifactId>
55. <version>${druid.version}</version>
56. </dependency>
57.
58. <!--spring-boot-web-->
59. <dependency>
60. <groupId>org.springframework.boot</groupId>
61. <artifactId>spring-boot-starter-web</artifactId>
62. <exclusions>
63. <exclusion>
64. <groupId>org.springframework.boot</groupId>
65. <artifactId>spring-boot-starter-tomcat</artifactId>
66. </exclusion>
67. </exclusions>
68. </dependency>
69. <!--使用jetty-->
70. <dependency>
71. <groupId>org.springframework.boot</groupId>
72. <artifactId>spring-boot-starter-jetty</artifactId>
73. </dependency>
74.
75. <!--引入mysql的连接驱动-->
76. <dependency>
77. <groupId>mysql</groupId>
78. <artifactId>mysql-connector-java</artifactId>
79. <version>${jdbc.mysql.version}</version>
80. </dependency>
81.
82. <!--模板使用velocity-->
83. <dependency>
84. <groupId>org.springframework.boot</groupId>
85. <artifactId>spring-boot-starter-velocity</artifactId>
86. </dependency>
87.
88. <!--spring-test-->
89. <dependency>
90. <groupId>org.springframework.boot</groupId>
91. <artifactId>spring-boot-starter-test</artifactId>
92. </dependency>
93. <!--spring-jdbc-->
94. <dependency>
95. <groupId>org.springframework.boot</groupId>
96. <artifactId>spring-boot-starter-jdbc</artifactId>
97. </dependency>
98.
99. </dependencies>
100.
101.
102.
103. <build>
104. <filters>
105. <filter>src/main/filters/xuele-${build.profile.id}.properties</filter>
106. </filters>
107.
108. <!--指定下面的目录为资源文件-->
109. <resources>
110. <!--设置自动替换-->
111. <resource>
112. <directory>src/main/resources</directory>
113. <includes>
114. <include>**/*</include>
115. </includes>
116. <!--也可以用排除标签-->
117. <!--<excludes></excludes>-->
118. <!--开启过滤-->
119. true</filtering>
120. </resource>
121.
122. </resources>
123.
124. <pluginManagement>
125. <plugins>
126. <plugin>
127. <groupId>org.springframework.boot</groupId>
128. <artifactId>spring-boot-maven-plugin</artifactId>
129. </plugin>
130. </plugins>
131. </pluginManagement>
132. </build>
133.
134.
135.
136. <profiles>
137. <!--默认激活开发配置,使用index-dev.properties来替换实际的文件key-->
138. <profile>
139. <id>dev</id>
140. <activation>
141. true</activeByDefault>
142. </activation>
143. <properties>
144. <build.profile.id>dev</build.profile.id>
145. </properties>
146. </profile>
147.
148. <!-- 测试环境配置 -->
149. <profile>
150. <id>test</id>
151. <properties>
152. <build.profile.id>test</build.profile.id>
153. </properties>
154. </profile>
155.
156. <!-- 生产环境配置 -->
157. <profile>
158. <id>product</id>
159. <properties>
160. <build.profile.id>product</build.profile.id>
161. </properties>
162. </profile>
163. </profiles>
164. </project>
然后在跑单元测试的时候,出乎意料的报了下面的一个错误:
Java代码
1. Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'jdbc.url' in property definitions
2. 141)
以前使用1.2.x的版本,没有遇到过这个错误,它的大致意思就是找不到jdbc.url这个属性,向配置文件里面赋值,我一直以为是自己某些文件,配置错误了,但检查了好几遍发现,并没有错误的地方,然后我把版本降到1.2.x的版本,确实可以编译通过,这说明了升级版本有一些api变化导致,于是网上几经google搜索,最后在stackoverflow找到了答案:
以前的赋值方式已经不支持了:
Java代码
1. jdbc.url=${jdbc.url}
2. jdbc.user=${jdbc.user}
3. jdbc.password=${jdbc.password}
4. jdbc.driveClassName=${jdbc.driveClassName}
最新的支持方式如下:
Java代码
1. jdbc.url=@jdbc.url@
2. jdbc.user=@jdbc.user@
3. jdbc.password=@jdbc.password@
4. jdbc.driveClassName=@jdbc.driveClassName@
总结:如果遇到这种类似的问题,仅仅是因为升级版本造成的,最快的解决办法就是上官网看changes
看看最新的版本的使用方式。