java - 多个带有配置文件的 Spring Boot 应用程序类

标签 java spring spring-boot

我需要能够在容器环境中运行 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]

解决这个问题的最佳方法是什么?

谢谢

尼克

最佳答案

你有两个选择

  1. 定义main class during the build在 spring-boot-maven-plugin 中。 You can also parametrize it for the build.

  2. 在属性文件或命令行中定义主类。但你需要使用PropertyLauncher而不是默认的org.springframework.boot.loader.JarLauncher

关于java - 多个带有配置文件的 Spring Boot 应用程序类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34016352/

相关文章:

Java:使用字符串 charAt 方法的 for 循环崩溃

java - Spring 休息模板

java - android executorservice 多线程性能蒙特卡罗估计

Java从对象列表中提取单个数据

java - 登录关系型数据库并扩展日志接口(interface)

java - Spring如何为@RestController自动将对象转换为json

java - 在java spring-boot中使用rest xml文件

java - validator 调用 "Return value constraints"两次

reactjs - 实现 websockets 的问题(Spring 后端/React 前端)

java - 使用 SSLContext 创建 SocketChannel