Sander_M Sander_M - 1 year ago 114
Java Question

What is the official spring boot way to start a simple non web based java application?

I am in the proces of converting a simple java project into a spring boot variant. The Spring Boot Reference Guide has been very helpful in general, but most examples of setting up a simple configuration involve some web based application. The getting started tutorial from tutorial doesn't provide the answer I am looking for either.

I have one class

that I need to run one method on
. I have configured the following classes, placed in the same package for simplicity:


public class Application {

public static void main(String[] args) {, args);



public class HelloConfiguration {

public HelloSpring helloSpring(){
HelloSpring hs = new HelloSpring();
return hs;

public HelloSpring hs;



public class HelloSpring {

public void printHello() {
System.out.println("Hello Spring!");

public void printHelloAgain() {
System.out.println("Hello Spring?");


It prints (spring logging omitted):

Hello Spring!
Hello Spring?
Hello Spring?

However, I am unsure of the correct way to execute my HelloSpring class.

Given above example, what is the official way to wire and "run" a class when using spring boot?

Answer Source

Simply use the ApplicationContext that returns and then work with that. That's pretty much all that is required

public static void main(String[] args) {
    ApplicationContext context =, args);
    HelloSpring bean = context.getBean(HelloSpring.class);

So you can open a gui, etc. and use the ApplicationContext to get your beans, etc.