4

I don't know what's the appropriate term, but by "one shot" program I meant a program that runs from the start of a main function to the end and exits.

Normally I do it with such boilerplate:

AnnotationApplicationContext ac = new AnnotationApplicationContext("myConfigClass") 
MyBean myStuff = ac.getBean("myBean");
myStuff.doSomething();
ac.flush();
ac.close();

I can still use a good deal of Spring features programming like that. However, when I tried out Spring boot, I simply do like this (please ignore syntax mistake as I scribble the snippet out on the top of my mind) - it thought it further simplify the programming (with fewer lines and fewer annotations):

@SpringBootApplication
public class App {
  public static void main(String[] args){
    SpringApplication.run(App.class);
  }
}

However, I noticed that the program, after launched, stays as a service until being killed explicitly.

My question, therefore, is: is Springboot suitable only for web application like Spring MVC or webservice style services? And it's not meant for simple "one shot" programs? And a relevant question is: does an embedded tomcat (or Jitty) always start when SpringBootApplication is employed and run?

Hope I made myself understood. Anyone can help?

J.E.Y
  • 1,173
  • 2
  • 15
  • 37
  • Possible duplicate of [Spring Boot: exception while exiting application](http://stackoverflow.com/questions/35936125/spring-boot-exception-while-exiting-application) – Patrick Oct 31 '16 at 15:16
  • yes its possible. Look at [my answer](http://stackoverflow.com/a/35936627/3493036). – Patrick Oct 31 '16 at 15:16
  • More informations about tomcat in spring boot here : [Spring Boot without the web server](http://stackoverflow.com/a/26105252/2425682) – alias_boubou Oct 31 '16 at 15:20
  • validated - worked like a charm, thanks to all! – J.E.Y Oct 31 '16 at 15:35

1 Answers1

4

If you run an spring boot application with CLR (CommandLineRunner), You will not need a server

@SpringBootApplication
public class App implements CommandLineRunner {

    @Autowired
    private MyBean myStuff;

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

    public void run(String... strings) throws Exception {
        myStuff.doSomething();
    }
}
D. Pineda
  • 66
  • 2