spring boot2.1.5版本,启动时用命令行指定配置文件有个bug

前途未达 2019-06-10 09:56:45
我使用java -jar Demo.jar --spring.config.location=外部配置文件
这种格式启动服务时,发现报错了,仔细看了下是注解读取配置文件的内容时无法获取到,而jar包内的配置文件是配置好的,外部配置文件只写了一个端口号。
而同样的内容如果用1.5.21版本打包成jar就没有这个问题,外部配置文件的端口号起作用了,jar内配置文件中的字符串也能成功获取。有人遇到过这个问题吗?是哪里有写错还是新版的就是 启动时指定配置文件后其他的都会失效?

报错日志:
2019-06-10 21:40:28.576  WARN 24080 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: o
rg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of autowired dependencies failed; nested exception is java.lang.Illeg
alArgumentException: Could not resolve placeholder 'app.inside.description' in value "${app.inside.description}"
2019-06-10 21:40:28.579 INFO 24080 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2019-06-10 21:40:28.587 INFO 24080 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-10 21:40:28.591 ERROR 24080 --- [ main] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of autowired dependencies failed; nested exception is java.lang.Ille
galArgumentException: Could not resolve placeholder 'app.inside.description' in value "${app.inside.description}"
...全文
273 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
前途未达 2021-04-24
  • 打赏
  • 举报
回复
自己结贴,指定配置文件时加上【classpath:\,】可以让其他配置文件正常加载: --spring.config.location=classpath:\,d:\application.yml
  • 打赏
  • 举报
回复
前途未达 2019-06-11
  • 打赏
  • 举报
回复
引用 2 楼 PreyHard 的回复:
我觉得是配置文件内容有问题,${}是获取之前配置的内容,而你app.description是放在下面,你把app.description=...放在app.inside.description前面试试。
这个问题你可以亲自试一下,我这边demo工程在debug configuration在program arguments中添加一行--spring.config.location=j:\application.properties,pom.xml文件如果配置2.1.5版本,那么启动报错;其他任何不改变,直接将pom文件中的版本号改为1.5.21启动就ok,所以肯定不是配置文件的问题,研究了几个小时源码,我发现2.x版本在加载配置文件的时候,如果在启动命令中加入了--spring.config.location,它获取到的配置文件就只有命令中指定的这个properties,其他classpath、config下面的全都不读取了,而1.x版本的源码debug进去就会都加载。
PreyHard 2019-06-11
  • 打赏
  • 举报
回复
我觉得是配置文件内容有问题,${}是获取之前配置的内容,而你app.description是放在下面,你把app.description=...放在app.inside.description前面试试。
前途未达 2019-06-10
  • 打赏
  • 举报
回复
这是jar内application.properties:
server.servlet.context-path=/zpl
server.port=8011
app.inside.description=${app.description} + this is inside2
app.description=this is inside
这是放在F盘目录的application.properties: server.port=8088 app.description=this is outside 这是一个非常简单的controller
package springboot01.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Value("${app.inside.description}")
    private String msg;
    @RequestMapping("/hello")
    public String hello(){
        return msg;
    }
}

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧