Spring boot for desktop application [closed]
Asked Answered
A

2

12

Can I use Spring boot for my Desktop application which is developed using Java Swing? Is that fine or is it not advisable? Can I get Spring boot advantages in Swing application or will it reduce performance?

Achromatic answered 17/10, 2018 at 13:48 Comment(0)
B
34

As stated by the official Spring Boot documentation:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

There is nothing stating that Spring Boot is only meant for web application development as it offers many capabilities around the Spring Framework itself and which does not relate to web applications such as the core container, Spring AOP, Spring JPA...

Here down a sample implementation where a Spring Boot annotated application runs a simple Swing frame:

@SpringBootApplication
public class SpringDesktopSampleApplication implements CommandLineRunner {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SpringDesktopSampleApplication.class).headless(false).run(args);
    }

    @Override
    public void run(String... args) {
        JFrame frame = new JFrame("Spring Boot Swing App");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
        JPanel panel = new JPanel(new BorderLayout());
        JTextField text = new JTextField("Spring Boot can be used with Swing apps");
        panel.add(text, BorderLayout.CENTER);
        frame.setContentPane(panel);
        frame.setVisible(true);
    }
}

You may note two differences between the classic way a Spring Boot application is run:

  • The main application class implements CommandLineRunner in order to be able to modify the runtime behavior of the application: this time launching a user interface.
  • The initialization of the Spring Boot application is done through SpringApplicationBuilder in order to be able to disable the headless feature which is meant for usage within server based applications.

In regards to performance, the Spring Boot desktop application remains a JVM deployed application and there will be no performance overhead (except a minimalistic startup one) that will be incurred by your application except the one you would introduce by yourself.

Benitez answered 17/10, 2018 at 15:39 Comment(0)
S
3

As it was already mentioned SpringBoot is mainly focused on developing web application. To have benefits that spring applications have you may want to use some other tools:

For dependency injection you may use Guice which may be more lighter alternatives as you will not be adding application with unnecessary web dependencies.

For database use you still are able to configure hibernate as ORM tool, however you will have to configure more things manually.

This may seem like more of work but in return you will have application that is more lightweight and have only needed dependencies for its purpose.

Snell answered 17/10, 2018 at 14:8 Comment(1)
What does spring brings to the table if you can't use autowired and value inside the app?Kendallkendell

© 2022 - 2024 — McMap. All rights reserved.