Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot CommandLineRunner invokes all classes with run method

I have multiple classes which uses CommandLineRunner for running the application. But when i start the application using spring-boot-maven-plugin all the classes are invoked one after another. This happens even if I run the class as main method. How can I overcome this. Any help will be greatly appreciated.

Using the below code in pom to identify the main method

<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>

like image 640
t10011 Avatar asked Aug 08 '26 22:08

t10011


1 Answers

A potential solution for you, that I used to get around a similar problem. Similar, but not exact same, so I can't guarentee success.

You can try using @ConditionalOnExpression on each implementation, with different values. Like so: @ConditionalOnExpression("'${some.property}'=='myValue'").

Add the property in application.properties to set a default implementation, and when running the jar you can override that implementation with java -jar myApp.jar --some.property=myOtherValue.

Spring boot should select the correct implementation to use, based on which implementation matches the value of the property you set.

like image 88
Pseudos Avatar answered Aug 10 '26 12:08

Pseudos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!