Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ 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 https://spring.io/guides/gs/spring-boot/ tutorial doesn't provide the answer I am looking for either.

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

Application.class

@SpringBootApplication
public class Application {

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

}

HelloConfiguration.class

@Configuration
public class HelloConfiguration {

    @Bean
    public HelloSpring helloSpring(){
        HelloSpring hs = new HelloSpring();
        hs.printHello();
        hs.printHelloAgain();
        return hs;
    }

    @Autowired
    public HelloSpring hs;

}

HelloSpring.class

public class HelloSpring {

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

    @PostConstruct
    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?

like image 887
Sander_M Avatar asked Aug 14 '16 19:08

Sander_M


2 Answers

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.

like image 165
Florian Schaetz Avatar answered Oct 27 '22 01:10

Florian Schaetz


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.

like image 27
Shawn Clark Avatar answered Oct 27 '22 01:10

Shawn Clark