Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot for desktop application [closed]

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?

like image 909
Prasath Avatar asked Nov 28 '22 20:11

Prasath


1 Answers

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.

like image 56
tmarwen Avatar answered Dec 01 '22 00:12

tmarwen