springboot报ScannerException:character ‘@‘ that cannot start any token. (Do not use @ for indentation

1、springboot @@,报ScannerException

Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '@' that cannot start any token. (Do not use @ for indentation)
 in 'reader', line 28, column 20:
          defaultZone: @eurekaDefaultZone@

在解决问题之前,我们先来看看为什么会出现这个问题? 

2、springboot项目使用@@占位符引用maven项目属性

我们知道,在springboot项目中,可以使用Maven的资源过滤(resource filter)自动暴露来自Maven项目的属性,如果使用parent

spring-boot-starter-parent 作为项目的父工程的话,我们可以通过@..@占位符引用Maven项目的属性。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

你在maven配置文件 pom.xml中设置启动文件:

<profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <build.profile.id>local</build.profile.id>
                <profileActive>local</profileActive>
            </properties>
        </profile>
        <profile>
            <id>production</id>
            <properties>
                <build.profile.id>production</build.profile.id>
                <profileActive>production</profileActive>DefaultZone>
        </profile>
    </profiles>

你就可以在application.yml文件里读取到上面profileActive的值:

spring:
  profiles:
    active: @profileActive@

这是为什么呢?这是因为, spring-boot-starter-parent自带自动化的资源过滤,那什么是资源过滤呢?

上面我们为本地环境和开发环境配置了不同的profile,在我们的代码里,就可以使用@xxx@来引用到属性,例如上面例子中,我们默认开启的环境是 带有 activeByDefault = true 的local环境,所以@profileActive@读到的是  profile id = local 的文件下,profileActive的值,即local。

3、ScannerException

但是如果你的springboot项目如果没有指定spring-boot-starter-parent的话,使用@@的时候就会报ScannerException异常:

Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '@' that cannot start any token. (Do not use @ for indentation)
 in 'reader', line 28, column 20:
          defaultZone: @eurekaDefaultZone@
                       ^

这时候,你需要在你pom文件的build节点加上如下的配置:

    <build>     
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!--开启过滤,用指定的参数替换directory下的文件中的参数-->
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

启动项目,一切正常,这是因为我们这里手动配置了资源过滤。

但是有个问题,就是我父工程指定的是spring-boot-starter-parent,但是我在bootstrap.yml文件中还是不能使用@@,报错跟上面的一样,而且加了资源过滤就好了,不知道为啥,还要继续研究下,有大神指导的话,感谢科普!

  • 28
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值