java - 使用 spring.config.location 命令行参数时,为 Spring 的 @PropertySource 设置什么?

标签 java spring spring-mvc spring-boot spring-properties

我定义了一个 @Config 类,如下所示:

@Config
@PropertySource(value = "")
public class Foo {
...
}

我通过包含以下内容来运行我的程序:

 --spring.config.location=file:///Users/dev/workspace/application.yml

问题是我无法省略 PropertySource 注释的“value”属性。为什么需要这样设置?这会覆盖我在 --spring.config.location 中设置的内容吗?我应该使用 SimpleCommandLinePropertySource 而不是 --spring.config.location 吗?

最佳答案

当您使用 spring.config.location 来使用外部属性文件时,您只需在配置类中设置值:

@Config
@PropertySource(value = "${spring.config.location}")
public class Foo {
...
}

关于java - 使用 spring.config.location 命令行参数时,为 Spring 的 @PropertySource 设置什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44358392/

相关文章:

java - Spring MVC 拦截器模式

java - Angular 6 Spring Boot POST 问题

Spring 3 JSR-286(portlets)文档

java - 禁用 springframework 日志记录

java - 没有找到带有 URI 的 HTTP 请求的映射....在 spring mvc 中名称为 'dispatcher' 的 DispatcherServlet

java - 带有 OSGi : Bundle stopped, 套接字的套接字仍然打开

java - RowMapper是抽象的,无法实例化

java - Spring Scheduled 注解是如何工作的

Java servlet和Ajax上传文件进度条

java - Spring 批问题