Spring boot for desktop application [closed]

拟墨画扇 提交于 2020-12-29 04:43:06

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/52856512/spring-boot-for-desktop-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!