我需要能够在容器环境中运行 Spring Boot,并作为独立的 fat JAR。在前一种模式中,应用程序需要向 Consul 注册自身,因为这用于服务发现,而在后一种模式中,我们需要完全禁用此集成。
为了实现这一点,我创建了 2 个应用程序类:(实际上,我在这里使用了一个抽象父级来表示重复的注释,但为了简洁起见,我省略了它)
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigurationProperties
@Profile("!standalone")
public class Application extends SpringApplication {
...
}
和
@SpringBootApplication(exclude = {
ConsulAutoConfiguration.class, ConsulBusAutoConfiguration.class })
@EnableConfigurationProperties
@Profile("standalone")
public class ApplicationStandalone extends SpringApplication {
...
}
请注意配置文件的使用,并且独立版本不包括 Consul 的自动配置类。
当我尝试通过 Maven 构建 JAR 时,出现以下错误:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.M5:repackage (default) on project mio-lmpp-service: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.M5:repackage failed: Unable to find a single main class from the following candidates [tv.nativ.mio.lmpp.ApplicationConfig, tv.nativ.mio.lmpp.ApplicationConfigStandalone] -> [Help 1]
解决这个问题的最佳方法是什么?
谢谢
尼克
最佳答案
你有两个选择
定义main class during the build在 spring-boot-maven-plugin 中。 You can also parametrize it for the build.
在属性文件或命令行中定义主类。但你需要使用PropertyLauncher而不是默认的
org.springframework.boot.loader.JarLauncher
。
关于java - 多个带有配置文件的 Spring Boot 应用程序类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34016352/