【问题标题】:Is it possible to access a property value from another maven project?是否可以从另一个 Maven 项目访问属性值?
【发布时间】:2020-09-05 15:56:15
【问题描述】:

我有一个 SpringBoot 服务:

package test;

@Service
public class TestService {

  @Value("${service.readTimeout}")
  private Duration readTimeout;
}

service.readTimeout 未在我当前项目的属性文件中声明。 它链接在一个单独的项目中,都标记为 Maven 模块。

这个项目正在访问的其他属性文件的值如何?

该项目未列为依赖项或在使用该值的项目的pom.xml 中提及。

Maven 是否连接所有声明的模块?是否可以从所有项目访问所有文件?

这是怎么回事?

【问题讨论】:

  • service.readTimeout在哪里配置?
  • @SimonMartinelli 在另一个项目中。所有项目都在pom.xml 中标记为模块。它们都是主项目下的子模块。这能回答你的问题吗?

标签: java spring spring-boot maven properties-file


【解决方案1】:

要获取外部资源,您可以使用FileSystemResource,例如我有两个项目demodemo2

所以我想从demo2 访问demo application.properties,所以你可以使用这个代码:

Resource resource = new FileSystemResource("absolute path");
// for example : C:\\Users\\Developper\\eclipse workspace\\demo\\src\\main\\resources\\application.properties

Properties props = PropertiesLoaderUtils.loadProperties(resource);
System.out.println(props); 

如果强制使用注解,可以试试:@PropertySource("file:/path/to/propertiesfile")https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html

【讨论】:

猜你喜欢
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-30
  • 2019-11-12
相关资源
最近更新 更多