What is the official spring boot way to start a simple non web based java application?
Simply use the ApplicationContext
that SpringApplication.run
returns and then work with that. That's pretty much all that is required
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class, args);
HelloSpring bean = context.getBean(HelloSpring.class);
bean.printHello();
}
So you can open a gui, etc. and use the ApplicationContext to get your beans, etc.
From the docs: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-command-line-runner
Application.class
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
HelloSpring.class
@Component
public class HelloSpring implements CommandLineRunner {
@Override
public void run(String... args) {
this.printHello();
}
public void printHello() {
System.out.println("Hello Spring!");
}
}
You can even make it so the run() method actually prints out you message but this way keeps it closer to your intent where you have implemented a method and want it executed when the application starts.