【问题标题】:Spring boot CommandLineRunner invokes all classes with run methodSpring boot CommandLineRunner 使用 run 方法调用所有类
【发布时间】:2016-03-04 01:26:50
【问题描述】:

我有多个使用 CommandLineRunner 运行应用程序的类。但是当我使用 spring-boot-maven-plugin 启动应用程序时,所有的类都被一个接一个地调用。即使我将类作为主要方法运行,也会发生这种情况。我该如何克服这一点。任何帮助将不胜感激。

在pom中使用下面的代码来识别main方法

<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.2.4.RELEASE</version> <configuration> <mainClass>com.Foo</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>

【问题讨论】:

  • 这就是CommandLineRunner 接口的用途。如果您不希望 Spring 调用这些类的 run 方法,请停止实现该接口。
  • @ESala 感谢您的回复。我正在寻找任何选项来覆盖此行为。

标签: java spring maven spring-boot


【解决方案1】:

一个潜在的解决方案,我曾经解决过类似的问题。类似,但不完全相同,所以我不能保证成功。

您可以尝试在每个实现上使用@ConditionalOnExpression,并使用不同的值。像这样:@ConditionalOnExpression("'${some.property}'=='myValue'")

application.properties 中添加属性以设置默认实现,运行jar 时可以使用java -jar myApp.jar --some.property=myOtherValue 覆盖该实现。

Spring boot 应该根据与您设置的属性值匹配的实现来选择要使用的正确实现。

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 1970-01-01
    • 2017-02-02
    • 2019-07-02
    • 2016-10-08
    • 2020-12-17
    • 2017-05-26
    • 2015-02-23
    • 1970-01-01
    相关资源
    最近更新 更多