Spring Boot 应用程序属性 Maven 多模块设置

3

我在使用Maven多模块项目时遇到了application.properties的设置问题。我的项目结构如下:

--parent
----core-api
----infrastructure (infrastructure.properties)
----command
----web (application.properties)

我正在使用不同的application.properties名称,因为我想将它们全部加载到目标模块中。
我已经阅读了thisthis的帖子,所以我决定放置它们。
@PropertySource(value = { "classpath:application.properties","classpath:infrastructure.properties" })

我希望你能帮我翻译一下,我的Application.classweb模块中。但是当我从父项目运行命令mvn spring-boot:run时,会出现异常:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.github.bilak.axonframework.poc.Application]; nested exception is java.io.FileNotFoundException: class path resource [infrastructure.properties] cannot be opened because it does not exist
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:180)

我的项目可以在这里找到。

  1. 如何在使用Maven插件的同时使其工作?有没有办法在Java中设置它并与Maven一起运行?
  2. 这更多地涉及到Maven,但是我能否从Web模块运行mvn spring-boot:run而不必将依赖模块安装到本地存储库中?

提前感谢。

1个回答

1
你可以重复使用Spring Boot基础设施,而不是自己创建。你可以有一个名为applications-infrastructure.properties的文件,并启用infrastructure配置文件。
关于你的第二个问题,不,你应该先安装其他模块;你也可以在#3436中找到相关讨论。

没错,我可以将属性用作配置文件。但是我猜这并不起作用,因为应用程序基础设施的属性没有被加载。我已经在Github上更新了项目。 - bilak
我运行了你的应用程序,基础设施配置文件已加载。所以我不知道你的问题在哪里。顺便说一下,你的Web项目中没有ServletInitializer,因此你将无法正确部署那个war包。请查看https://spring.io/guides/gs/convert-jar-to-war/。 - Stephane Nicoll
它对我来说就在那里 (-rw-r--r-- 1 snicoll staff 208896 Aug 12 16:16 /Users/snicoll/h2db/axonpoc.mv.db)。不过,有更好的方法来确定配置文件是否已加载(例如使用执行器)。 - Stephane Nicoll
很奇怪,它对我不起作用 - 我看不到那个数据库文件,即使我尝试在 infrastructure.properties 中更改服务器端口也没有变化。你有更改我的测试项目让它工作吗? - bilak
好的,我已经将执行器添加到项目中并扩展了ServletInitializer。当我查看/env端点并查找基础设施时,我无法在那里找到application-infrastructure.properties。只有当我将它们放到Web模块中时才能找到它们。您正在运行我的应用程序的操作系统是哪个? - bilak
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接