遇到的一个坑, spring boot + maven maven fileter没有起作用。spring boot把默认占位符改了

参考:https://blog.csdn.net/mn960mn/article/details/78834875

创建一个maven项目,项目结构如下:

其中,pom.xml的内容如下:

  1.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.  
    <modelVersion>4.0.0</modelVersion>
  4.  
     
  5.  
    <groupId>com.pp.test</groupId>
  6.  
    <artifactId>properties-test</artifactId>
  7.  
    <version>0.0.1-SNAPSHOT</version>
  8.  
    <packaging>jar</packaging>
  9.  
     
  10.  
    <name>properties-test</name>
  11.  
    <url>http://maven.apache.org</url>
  12.  
     
  13.  
    <parent>
  14.  
    <groupId>org.springframework.boot</groupId>
  15.  
    <artifactId>spring-boot-starter-parent</artifactId>
  16.  
    <version>1.5.9.RELEASE</version>
  17.  
    </parent>
  18.  
     
  19.  
    <properties>
  20.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  21.  
    <maven.compiler.source>1.8</maven.compiler.source>
  22.  
    <maven.compiler.target>1.8</maven.compiler.target>
  23.  
    <java.version>1.8</java.version>
  24.  
    </properties>
  25.  
     
  26.  
    <dependencies>
  27.  
    <dependency>
  28.  
    <groupId>org.springframework.boot</groupId>
  29.  
    <artifactId>spring-boot-starter-web</artifactId>
  30.  
    </dependency>
  31.  
    </dependencies>
  32.  
     
  33.  
    <build>
  34.  
    <filters>
  35.  
    <filter>src/main/profiles/profile-${profiles.active}.properties</filter>
  36.  
    </filters>
  37.  
    <resources>
  38.  
    <resource>
  39.  
    <filtering>true</filtering>
  40.  
    <directory>src/main/resources</directory>
  41.  
    </resource>
  42.  
    </resources>
  43.  
    </build>
  44.  
     
  45.  
     
  46.  
    <profiles>
  47.  
    <profile>
  48.  
    <id>dev</id>
  49.  
    <properties>
  50.  
    <profiles.active>dev</profiles.active>
  51.  
    </properties>
  52.  
    <activation>
  53.  
    <activeByDefault>true</activeByDefault>
  54.  
    </activation>
  55.  
    </profile>
  56.  
    <profile>
  57.  
    <id>test</id>
  58.  
    <properties>
  59.  
    <profiles.active>test</profiles.active>
  60.  
    </properties>
  61.  
    </profile>
  62.  
    <profile>
  63.  
    <id>prod</id>
  64.  
    <properties>
  65.  
    <profiles.active>prod</profiles.active>
  66.  
    </properties>
  67.  
    </profile>
  68.  
    </profiles>
  69.  
    </project>

application.properties 文件内容:

  1.  
    jdbc.driverClassName=${jdbc.driverClassName}
  2.  
    jdbc.url=${jdbc.url}
  3.  
    jdbc.username=${jdbc.username}
  4.  
    jdbc.password=${jdbc.password}

profile-dev.properties 文件内容:

  1.  
    jdbc.driverClassName=com.mysql.jdbc.Driver
  2.  
    jdbc.url=jdbc:mysql:///db_users
  3.  
    jdbc.username=root
  4.  
    jdbc.password=root

启动应用,报错了,错误如下:

  1.  
    Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'jdbc.url' in property definitions
  2.  
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:141) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  3.  
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:162) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  4.  
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  5.  
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  6.  
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  7.  
    at org.springframework.core.env.AbstractPropertyResolver.resolveNestedPlaceholders(AbstractPropertyResolver.java:227) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  8.  
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:84) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  9.  
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:61) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  10.  
    at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:527) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  11.  
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:132) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  12.  
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:129) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  13.  
    at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:81) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  14.  
    at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString(PropertySourcesPropertyResolver.java:71) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  15.  
    at org.springframework.core.env.AbstractPropertyResolver$1.resolvePlaceholder(AbstractPropertyResolver.java:239) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  16.  
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:147) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  17.  
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  18.  
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  19.  
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  20.  
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  21.  
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:831) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  22.  
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  23.  
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  24.  
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  25.  
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  26.  
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
  27.  
    ... 17 common frames omitted

报这个错误的原因是,maven的filtering没有起作用,没有把占位符给替换掉。(大家可以执行mvn clean package,看看打包后的jar里面的application.properties文件,是否有替换占位符)

filtering无效的原因是,pom.xml继承了spring boot的依赖

  1.  
    <parent>
  2.  
    <groupId>org.springframework.boot</groupId>
  3.  
    <artifactId>spring-boot-starter-parent</artifactId>
  4.  
    <version>1.5.9.RELEASE</version>
  5.  
    </parent>

点开这个依赖的pom.xml,我们发现

spring boot把默认的占位符号${}改成了@

找到原因了,那怎么解决就很简单了。

方法一:

在pom.xml里面添加如下内容

  1.  
    <properties>
  2.  
    <resource.delimiter>${}</resource.delimiter>
  3.  
    </properties>

方法二:

application.properties里面不用${},改成@

  1.  
    jdbc.driverClassName=@jdbc.driverClassName@
  2.  
    jdbc.url=@jdbc.url@
  3.  
    jdbc.username=@jdbc.username@
  4.  
    jdbc.password=@jdbc.password@

方法三:

pom.xml不继承spring-boot-starter-parent,dependency里面配置全部的依赖和版本号(继承了之后,很多依赖不用写version)

方法四:

  1.  
    <parent>
  2.  
    <groupId>org.springframework.boot</groupId>
  3.  
    <artifactId>spring-boot-starter-parent</artifactId>
  4.  
    <version>1.5.9.RELEASE</version>
  5.  
    </parent>

改成

  1.  
    <dependencyManagement>
  2.  
    <dependencies>
  3.  
    <dependency>
  4.  
    <groupId>org.springframework.boot</groupId>
  5.  
    <artifactId>spring-boot-starter-web</artifactId>
  6.  
    <version>1.5.9.RELEASE</version>
  7.  
    <type>pom</type>
  8.  
    <scope>import</scope>
  9.  
    </dependency>
  10.  
    </dependencies>
  11.  
    </dependencyManagement>

maven filter不起作用的更多相关文章

  1. maven filter插件只替换了部分变量问题

    maven filter简介 maven的resources插件,有一个filter的作用,能够在打包的时候,从特定文件里读取key-value对,替换配置文件中的占位符变量.很多线上线下有不同环境的 ...

  2. Maven Filter与Profile隔离生产环境与开发环境

    Maven Filter与Profile隔离生产环境与开发环境 在不同的开发阶段,我们一般用到不同的环境,开发阶段使用开发环境的一套东西,测试环境使用测试环境的东西,可能有多个测试环境,生产环境使用的 ...

  3. maven filter 乱码,MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.

    <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI ...

  4. 用 maven filter 管理不同环境的配置文件

    使用 maven profile 一个项目可以部署在不同的环境当中,maven 的 profile 针对不同的环境指定各自的编译方法.在 pom.xml 的 profile 中,可以根据不同的环境定制 ...

  5. Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置

    由浅入深,主要介绍maven的用途.核心概念(Pom.Repositories.Artifact.Build Lifecycle.Goal).用法(Archetype意义及创建各种项目).maven常 ...

  6. org.springframework.web.filter.DelegatingFilterProxy的作用

    一.类结构 DelegatingFilterProxy类继承GenericFilterBean,间接实现了Filter,故而该类属于一个过滤器.那么就会有实现Filter中init.doFilter. ...

  7. spring security 4 filter 顺序及作用

    Spring Security 有两个作用:认证和授权 一.Srping security 4 filter 别名及顺序 spring security 4 标准filter别名和顺序,因为经常要用就 ...

  8. [转]Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置

    转自:http://www.trinea.cn/android/maven/ 两年半前写的关于Maven的介绍,现在看来都还是不错的,自己转下.写博客的一大好处就是方便自己以后查阅,自己总结的总是最靠 ...

  9. Maven 的classifier的作用

    直接看一个例子,maven中要引入json包,于是使用了 <dependency> <groupId>net.sf.json-lib</groupId> <a ...

随机推荐

  1. Linux rsync 企业级应用

    简介 rsync 是 Linux 下的数据同步工具, 其支持本地同步和远程同步, 远程同步分为 daemon 和 ssh 同步方式    rsync 可以代替 cp, scp 等命令, 且具有更高的可 ...

  2. 带领技术小白入门——基于java的微信公众号开发(包括服务器配置、java web项目搭建、tomcat手动发布web项目、微信开发所需的url和token验证)

    微信公众号对于每个人来说都不陌生,但是许多人都不清楚是怎么开发的.身为技术小白的我,在闲暇之余研究了一下基于java的微信公众号开发.下面就是我的实现步骤,写的略显粗糙,希望大家多多提议! 一.申请服 ...

  3. js高级程序设计 笔记 --- 面向对象的程序设计

    1,理解对象 通过对象字面量的方式,创建一个对象,为它添加属性和方法: var obj = { a: 1, b:2, sayA(){ console.log(this.a)}} 1,属性类型: 数据属 ...

  4. [转] watch 命令使用(linux监控状态)

    [From] https://jingyan.baidu.com/article/495ba841c5a31738b30eded4.html 可以使用watch 命令设置执行间隔,去反复间隔一条命令或 ...

  5. PIE SDK加载自定义服务数据

    1.功能简介 自定义服务数据,将符合要求的矢量数据和栅格数据集等数据以服务的方式发布,将数据存储在某服务器中,在有网络的情况下可以根据URL就可以访问,比较常见的服务数据类型的有ArcGIS Serv ...

  6. zero-copy总结

    基本概念 零拷贝,通常在java NIO编程中会使用,比如netty网络工具包. 其真实意思是: 网卡或者其他外设进行io操作时不经过CPU, 而是直接和主memory交互,不经过CPU寄存器,这样可 ...

  7. mysql出现 Unknown column 'bname' in 'where clause'和Unknown column 'bid' in 'field list'

    在用mysql数据库建表和修改数据库数据时,出现  Unknown column 'bname' in 'where clause'和Unknown column 'bid' in 'field li ...

  8. $bzoj1014-JSOI2008$ 火星人$prefix$ $splay$ $hash$

    题面描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:\(madamimadam\),我们将这个字符串的各个字符予以标号: 序号 1 2 3 4 5 6 7 8 ...

  9. Three.js 前言

    -----------------------------------本文非技术文章,着急开发的小伙伴请绕道----------------------------------------- 最近公司 ...

  10. C# DataTable 用法

    1.创建DataTable DataTable dataTable = new DataTable(); //创建一个空表 2.创建DataRow DataRow row = dataTable.Ne ...