在做springboot整合redisson的时候,将redisson的redis链接信息设置在配置文件中。但是由于系统设置了多环境的配置。每个环境的redis配置不一样,故需要引入对应环境的配置。如下:
结果在启动项目的时候,发现并没有解析读取到redis的相关配置。
查看编译完的文件,发现此时并没有将值替换掉,还是原有的${redis.host}等,
查看了一下其他编译完的文件发现只有多环境设置激活的profile(spring.profiles.active=@profiles.active@)使用的是@才被正常替换了。
而后查看了POM文件的配置,发现是继承了spring-boot-starter-parent:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
而在spring-boot-starter-parent中设置了<resource.delimiter>@</resource.delimiter>。
至此,关于编译后的资源文件中的${redis.host}并没有被正常替换的原因找到了,正是因为parent设置了resource.delimiter为@,所以${redis.host}没有被正常解析替换。
此处贴出POM文件的build配置,方便有出现类似问题的朋友核对:
<build> <finalName>ROOT</finalName> <filters> <!-- 使用filters中指定的文件中的配置对resource中的引用变量进行解析 --> <filter>src/main/resources/application-${profiles.active}.properties</filter> </filters> <plugins> ..... </plugins> <resources> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> <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-${profiles.active}.properties</include> <include>application.yml</include> <include>redisson-config.yml</include> </includes> </resource> </resources> </build>
注意:在build中需要加入<filters><filter>环境文件路径<filter><filters>,用于对resource中的引用变量进行解析
方法一:修改resource.delimiter
<properties> <resource.delimiter>${}</resource.delimiter> </properties>
方法二:将redisson-config.yml中的redis配置调整为:
singleServerConfig: address: redis://@redis.host@:@redis.port@ password: @redis.password@
方法三:pom.xml不继承spring-boot-starter-parent,不过这样的话,所导入的依赖就需要设置版本号。