记一个IDEA-Sping Boot自动读取配置文件的bug

缘由

这是一个找了四天的bug,需求是做一个springboot服务,获取canal同步数据至ES中,本地测试无误后,提取部分信息至配置文件,随后在正式环境出现无法获取到binlog数据,只获取到一些事务信息,排查了各个问题后,终于看到了这个问题。


重现bug

准备

Intellij IDEA 2018.2.5
JDK1.8
Spring Boot 1.5.10.RELEASE

开始重现

新建一个Spring Boot项目

1.配置文件( application.properties )中添加两个配置项

test.esc1=hello\\\\esc1
test.esc2=hello\\esc2

来源是canal的示例配置


image.png

2.写一个测试的Controller,来获取配置文件中的信息,并创建一个变量

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Value("${test.esc1}")
    private String strTestESC1;

    @Value("${test.esc2}")
    private String strTestESC2;

    String strMethodCreate =  "hello\\8080";

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(){
        logger.info("配置esc1 --> "+strTestESC1);
        logger.info("配置esc2 --> "+strTestESC2);
        logger.info("字符串 --> "+strMethodCreate);
        return "OK";
    }
}

由于IDEA自动读取配置信息的功能,实际上看到的信息是这样的:


开发时看到的

可以看到hello\\\\esc1被转义成了hello\\esc1hello\\esc2被转义成了hello\esc2,因此我在使用时,配置文件中配置了类似hello\\\\esc1的配置数据。
然而,当实际运行时,日志输出的结果却是这样的:

实际输出的日志

就这样,在我找了四个工作日,排除了其他的问题后,终于看到了这个奇怪的现象。当我把canal的配置改成类似hello\\esc2时,不知道该哭还是该笑,不知道怎么解释我花了四天时间删了个\\

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容