Spring boot 我有两个CommandLineRunner在同一个弹簧靴中

Spring boot 我有两个CommandLineRunner在同一个弹簧靴中,spring-boot,Spring Boot,我在SpringBoot中有两个类实现了命令行运行程序。它们基本上是这样的: @SpringBootApplication @ComponentScan("com.xxxx") public class Application implements CommandLineRunner { 第二个看起来像: @SpringBootApplication @ComponentScan("com.xxxx") public class ApplicationWorklfow implem

我在SpringBoot中有两个类实现了命令行运行程序。它们基本上是这样的:

 @SpringBootApplication
 @ComponentScan("com.xxxx")
 public class Application implements CommandLineRunner {
第二个看起来像:

 @SpringBootApplication
 @ComponentScan("com.xxxx")
 public class ApplicationWorklfow implements CommandLineRunner {
他们编译得很好。但当我尝试用java-jar运行它时,我可能会得到一个错误,因为spring不知道该运行哪一个


是否有一个命令可以告诉jar我正在运行哪个应用程序?

您可以有任意数量的
CommandLineRunner
bean,但应该只有一个入口点类可以有
@SpringBootApplication
注释。尝试删除
ApplicationWorkfow
上的
@SpringBootApplication
注释

PS:

您的主要需求似乎是有条件地启用两个CommandLineRunner bean中的一个。您只能有一个应用程序类,并且可以使用
@Profile
@ConditionalOnProperty
等有条件地启用CLR bean

使用带有
@SpringBootApplication
注释的多个入口点类不是一个好主意

@SpringBootApplication
public class Application {

}

@Component
@Profile("profile1")
public class AppInitializer1 implements CommandLineRunner {

}

@Component
@Profile("profile2")
public class AppInitializer2 implements CommandLineRunner {

}
现在,您可以按如下方式激活所需的配置文件:

java -jar -Dspring.profiles.active=profile1 app.jar
启用profile1后,只有AppInitializer1将运行

附言:附言:

如果出于某种原因,您仍然希望配置mainClass,则可以执行以下操作:

   <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>          
      <configuration>
        <mainClass>${start-class}</mainClass>
      </configuration>

    </plugin>

org.springframework.boot

更多信息。

那么我如何告诉java我想要运行ApplicationWoklfow。在那次启动?我的答案中添加了更多信息。