桌面应用程序的Spring Boot [英] Spring boot for desktop application

查看:108
本文介绍了桌面应用程序的Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以对使用Java Swing开发的桌面应用程序使用Spring Boot吗?可以吗?还是不建议这样做?我可以在Swing应用程序中获得Spring Boot的优势还是会降低性能?

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?

推荐答案

如官方 所述Spring Boot 文档:

使用Spring Boot可以轻松地创建独立的,基于生产级别的基于Spring的应用程序,您可以对其进行运行".

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

没有任何说明说 Spring Boot 仅用于Web应用程序开发,因为它围绕 Spring Framework 本身提供了许多功能,并且与Web应用程序无关,例如作为核心容器, Spring AOP Spring JPA ...

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...

下面是一个示例实现,其中带有 Spring Boot 注释的应用程序运行一个简单的 Swing 框架:

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);
    }
}

您可能会注意到运行 Spring Boot 应用程序的经典方式之间的两个区别:

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

  • 主要应用程序类实现 CommandLineRunner 以便能够修改应用程序的运行时行为:这次启动用户界面.
  • Spring Boot 应用程序的初始化是通过 SpringApplicationBuilder 完成的,以便能够禁用 headless 功能(该功能仅供使用)在基于服务器的应用程序中.
  • 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.

关于性能, Spring Boot 桌面应用程序仍然是 JVM 部署的应用程序,不会产生性能开销(除了极简启动之外).根据您的应用程序,但您自己介绍的应用程序除外.

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.

这篇关于桌面应用程序的Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆