使用SpringBoot1.4.0的一个坑
时隔半年,再次使用Spring Boot快速搭建微服务,半年前使用的版本是1.2.5,如今看官网最新的release版本是1.4.0,那就用最新的来构建,由于部署环境可能有多套所以使用maven-filter插件,定义多套环境的配置文件,最后使用的时候:
可以采用下面的几个命令来构建不同环境的打包:
Java代码
[*]maven clean package -Pdev
[*]maven clean package -Ptest
[*]maven clean package -Pproduct
项目结构截图如下:
pom依赖如下:
Java代码
[*]<?xml version="1.0" encoding="UTF-8"?>
[*]<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">
[*] <modelVersion>4.0.0</modelVersion>
[*]
[*] <groupId>com.monitor.bigscreen</groupId>
[*] <artifactId>monitor-bigscreen-sql</artifactId>
[*] <version>1.0.0-SNAPSHOT</version>
[*]
[*] <properties>
[*] <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
[*] <elasticsearch.version>2.3.4</elasticsearch.version>
[*] <junit.version>4.12</junit.version>
[*] <fast.json>1.2.15</fast.json>
[*] <spring.boot.version>1.4.0.RELEASE</spring.boot.version>
[*] <druid.version>1.0.15</druid.version>
[*] <jdbc.mysql.version>5.1.6</jdbc.mysql.version>
[*] </properties>
[*]
[*]
[*]
[*]
[*] <!--<parent>-->
[*] <!--<groupId>org.springframework.boot</groupId>-->
[*] <!--<artifactId>spring-boot-starter-parent</artifactId>-->
[*] <!--<!–此处不能甩变量替代–>-->
[*] <!--<version>1.4.0.RELEASE</version>-->
[*] <!--</parent>-->
[*] <!--使用上面的父parent或者下面的依赖插件引入父依赖-->
[*] <dependencyManagement>
[*]
[*] <dependencies>
[*] <dependency>
[*] <groupId>org.springframework.boot</groupId>
[*] <artifactId>spring-boot-dependencies</artifactId>
[*] <type>pom</type>
[*] <version>1.4.0.RELEASE</version>
[*] <scope>import</scope>
[*] </dependency>
[*] </dependencies>
[*]
[*]
[*] </dependencyManagement>
[*]
[*]
[*]
[*] <dependencies>
[*]
[*]
[*] <!--连接池druid-->
[*] <dependency>
[*] <groupId>com.alibaba</groupId>
[*] <artifactId>druid</artifactId>
[*] <version>${druid.version}</version>
[*] </dependency>
[*]
[*] <!--spring-boot-web-->
[*] <dependency>
[*] <groupId>org.springframework.boot</groupId>
[*] <artifactId>spring-boot-starter-web</artifactId>
[*] <exclusions>
[*] <exclusion>
[*] <groupId>org.springframework.boot</groupId>
[*] <artifactId>spring-boot-starter-tomcat</artifactId>
[*] </exclusion>
[*] </exclusions>
[*] </dependency>
[*] <!--使用jetty-->
[*] <dependency>
[*] <groupId>org.springframework.boot</groupId>
[*] <artifactId>spring-boot-starter-jetty</artifactId>
[*] </dependency>
[*]
[*] <!--引入mysql的连接驱动-->
[*] <dependency>
[*] <groupId>mysql</groupId>
[*] <artifactId>mysql-connector-java</artifactId>
[*] <version>${jdbc.mysql.version}</version>
[*] </dependency>
[*]
[*] <!--模板使用velocity-->
[*] <dependency>
[*] <groupId>org.springframework.boot</groupId>
[*] <artifactId>spring-boot-starter-velocity</artifactId>
[*] </dependency>
[*]
[*] <!--spring-test-->
[*] <dependency>
[*] <groupId>org.springframework.boot</groupId>
[*] <artifactId>spring-boot-starter-test</artifactId>
[*] </dependency>
[*] <!--spring-jdbc-->
[*] <dependency>
[*] <groupId>org.springframework.boot</groupId>
[*] <artifactId>spring-boot-starter-jdbc</artifactId>
[*] </dependency>
[*]
[*] </dependencies>
[*]
[*]
[*]
[*] <build>
[*] <filters>
[*] <filter>src/main/filters/xuele-${build.profile.id}.properties</filter>
[*] </filters>
[*]
[*] <!--指定下面的目录为资源文件-->
[*] <resources>
[*] <!--设置自动替换-->
[*] <resource>
[*] <directory>src/main/resources</directory>
[*] <includes>
[*] <include>**/*</include>
[*] </includes>
[*] <!--也可以用排除标签-->
[*] <!--<excludes></excludes>-->
[*] <!--开启过滤-->
[*] <filtering>true</filtering>
[*] </resource>
[*]
[*] </resources>
[*]
[*] <pluginManagement>
[*] <plugins>
[*] <plugin>
[*] <groupId>org.springframework.boot</groupId>
[*] <artifactId>spring-boot-maven-plugin</artifactId>
[*] </plugin>
[*] </plugins>
[*] </pluginManagement>
[*] </build>
[*]
[*]
[*]
[*] <profiles>
[*] <!--默认激活开发配置,使用index-dev.properties来替换实际的文件key-->
[*] <profile>
[*] <id>dev</id>
[*] <activation>
[*] <activeByDefault>true</activeByDefault>
[*] </activation>
[*] <properties>
[*] <build.profile.id>dev</build.profile.id>
[*] </properties>
[*] </profile>
[*]
[*] <!-- 测试环境配置 -->
[*] <profile>
[*] <id>test</id>
[*] <properties>
[*] <build.profile.id>test</build.profile.id>
[*] </properties>
[*] </profile>
[*]
[*] <!-- 生产环境配置 -->
[*] <profile>
[*] <id>product</id>
[*] <properties>
[*] <build.profile.id>product</build.profile.id>
[*] </properties>
[*] </profile>
[*] </profiles>
[*]</project>
然后在跑单元测试的时候,出乎意料的报了下面的一个错误:
Java代码
[*]Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'jdbc.url' in property definitions
[*] at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:141)
以前使用1.2.x的版本,没有遇到过这个错误,它的大致意思就是找不到jdbc.url这个属性,向配置文件里面赋值,我一直以为是自己某些文件,配置错误了,但检查了好几遍发现,并没有错误的地方,然后我把版本降到1.2.x的版本,确实可以编译通过,这说明了升级版本有一些api变化导致,于是网上几经google搜索,最后在stackoverflow找到了答案:
以前的赋值方式已经不支持了:
Java代码
[*]jdbc.url=${jdbc.url}
[*]jdbc.user=${jdbc.user}
[*]jdbc.password=${jdbc.password}
[*]jdbc.driveClassName=${jdbc.driveClassName}
最新的支持方式如下:
Java代码
[*]jdbc.url=@jdbc.url@
[*]jdbc.user=@jdbc.user@
[*]jdbc.password=@jdbc.password@
[*]jdbc.driveClassName=@jdbc.driveClassName@
总结:如果遇到这种类似的问题,仅仅是因为升级版本造成的,最快的解决办法就是上官网看changes
看看最新的版本的使用方式。
参考链接:
http://stackoverflow.com/questions/36501017/maven-resource-filtering-not-working-because-of-spring-boot-dependency
https://github.com/spring-projects/spring-boot/issues/980
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#maven-resources-filtering
有什么问题可以扫码关注微信公众号:我是攻城师(woshigcs),在后台留言咨询。
技术债不能欠,健康债更不能欠, 求道之路,与君同行。
页:
[1]