繁体   English   中英

在SpringBoot的@PropertySource中指定value属性

[英]Specifying value attribute in @PropertySource in SpringBoot

设定

我有一个罐子(比如说它的A),它添加了另一个罐子(比如说它的B)作为通过Maven依赖关系。 Jar A来自Spring Boot Application。

问题

我在jar B的路径src / conf /下有属性文件。 我正在尝试在jar B的一个java文件中的@PropertySource的value属性中设置路径值。尝试执行此操作时,它引用jar A的src / conf。我如何实现这一点。

@PropertySource( value = { "file:${spring.profiles.path}other-services-dev.properties" })

在这里, spring.profiles.path从jar A的部署脚本中获取其值。

使用“类路径:”代替file:

通过将属性文件移动到src/main/resources使其成为jar B的src/main/resources 然后,您可以像这样引用文件: @PropertySource("classpath:/other-services-dev.properties")

更好的封装方法是在jar B中定义一个类,该类被@PropertySource("classpath:/other-services-dev.properties")注释,并通过getter公开这些属性。 然后jar A可以简单地让此类注入。

在罐子B中:

 @PropertySource("classpath:/other-services-dev.properties")
 @Service
 class BProperties
   @Value("${setting1}")
   private String setting1;

   public String getSetting1() {
     return setting1;
   }
 }

在罐子A中

@Service
class SomeService {
  @Autowired
  private BProperties properties;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM