SpringApplication.run main method

144,858

Solution 1

You need to run Application.run() because this method starts whole Spring Framework. Code below integrates your main() with Spring Boot.

Application.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

ReconTool.java

@Component
public class ReconTool implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        main(args);
    }

    public static void main(String[] args) {
        // Recon Logic
    }
}

Why not SpringApplication.run(ReconTool.class, args)

Because this way spring is not fully configured (no component scan etc.). Only bean defined in run() is created (ReconTool).

Example project: https://github.com/mariuszs/spring-run-magic

Solution 2

Using:

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);  

        //do your ReconTool stuff
    }
}

will work in all circumstances. Whether you want to launch the application from the IDE, or the build tool.

Using maven just use mvn spring-boot:run

while in gradle it would be gradle bootRun

An alternative to adding code under the run method, is to have a Spring Bean that implements CommandLineRunner. That would look like:

@Component
public class ReconTool implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
       //implement your business logic here
    }
}

Check out this guide from Spring's official guide repository.

The full Spring Boot documentation can be found here

Share:
144,858
Alexander Mills
Author by

Alexander Mills

Dev, Devops, soccer coach. https://www.github.com/oresoftware

Updated on July 09, 2022

Comments

  • Alexander Mills
    Alexander Mills almost 2 years

    I created a project in Eclipse using the Spring Starter project template.

    It automatically created an Application class file, and that path matches the path in the POM.xml file, so all is well. Here is the Application class:

    @Configuration
    @ComponentScan
    @EnableAutoConfiguration
    public class Application {
    
        public static void main(String[] args) {
            //SpringApplication.run(ReconTool.class, args);  
            ReconTool.main(args);
        }
    }
    

    This is a command line app that I am building and in order to get it to run I had to comment out the SpringApplication.run line and just add the main method from my other class to run. Other than this quick jerry-rig, I can build it using Maven and it runs as a Spring application, sort of.

    I'd rather, however, not have to comment out that line, and use the full Spring framework. How can I do this?

  • MariuszS
    MariuszS about 10 years
    You steal all my ideas :)
  • geoand
    geoand about 10 years
    It seams like we are thinking in parallel :)
  • Alexander Mills
    Alexander Mills about 10 years
    I think you meant: SpringApplication.run(ReconTool.class, args);
  • MariuszS
    MariuszS about 10 years
    No, RecontTool has @Component added, so spring will execute RecontTool.run() and RecontTool.main() ofcourse :)
  • MariuszS
    MariuszS about 10 years
    But you are true, SpringApplication.run(ReconTool.class, args) works too :)
  • MariuszS
    MariuszS about 10 years
    I would rather leave SpringApplication.run(Application.class because this works, second solution looks too magic to me :)
  • MariuszS
    MariuszS about 10 years
    Your solution works without @Component :) github.com/mariuszs/spring-run-magic
  • Villat
    Villat about 5 years
    Nowadays, you could replace @Configuration, @ComponentScan and @EnableAutoConfiguration and use @SpringBootApplication instead