Java 如何使用 Swing 应用程序配置 spring-boot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22864008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure spring-boot with swing application
提问by Dapaldo
I'd like using spring-boot-starter-data-jpa features to create a non-web aplication. In the 52.4 documentation says:
我想使用 spring-boot-starter-data-jpa 功能来创建非 Web 应用程序。在 52.4 文档中说:
Application code that you want to run as your business logic can be implemented as a CommandLineRunner and dropped into the context as a @Bean definition.
您希望作为业务逻辑运行的应用程序代码可以作为 CommandLineRunner 实现,并作为 @Bean 定义放入上下文中。
My AppPrincipalFramelooks like:
我的AppPrincipalFrame看起来像:
@Component
public class AppPrincipalFrame extends JFrame implements CommandLineRunner{
private JPanel contentPane;
@Override
public void run(String... arg0) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AppPrincipalFrame frame = new AppPrincipalFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
And boot application classlooks like:
和启动应用程序类的样子:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class, args);
AppPrincipalFrame appFrame = context.getBean(AppPrincipalFrame.class);
}
}
But does not work. Anybody have a sample about this?
但不起作用。有人有这方面的样本吗?
Edited and exception added.
编辑并添加了例外。
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appPrincipalFrame'.
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [es.adama.swing.ui.AppPrincipalFrame]: Constructor threw exception; nested exception is java.awt.HeadlessException
Regards.
问候。
回答by Rumal
It's been a while since you posted the question, but I run out with the same problem in a old project that I'm migrating and figured a different way, I think that more clear, to make the things work.
自从您发布问题以来已经有一段时间了,但是我在我正在迁移的旧项目中遇到了同样的问题,并且想出了一种不同的方式,我认为这更清楚,以使事情发挥作用。
Instead of using SpringApplication.run(Application.class, args);
you can use: new SpringApplicationBuilder(Main.class).headless(false).run(args);
and it isn't necessary to make toy Application class extends from JFrame. Therefore the code could look like:
而不是使用SpringApplication.run(Application.class, args);
您可以使用:new SpringApplicationBuilder(Main.class).headless(false).run(args);
它是没有必要做玩具类应用从JFrame的延伸。因此,代码可能如下所示:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(Application.class).headless(false).run(args);
AppPrincipalFrame appFrame = context.getBean(AppPrincipalFrame.class);
}
回答by Jan Bodnar
The Swing application must be placed on the Swing Event Queue. Not doing so is a serious mistake.
Swing 应用程序必须放在 Swing 事件队列中。不这样做是一个严重的错误。
So the correct way to do it:
所以正确的做法是:
public static void main(String[] args) {
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(SwingApp.class)
.headless(false).run(args);
EventQueue.invokeLater(() -> {
SwingApp ex = ctx.getBean(SwingApp.class);
ex.setVisible(true);
});
}
In addition, we can just use the @SpringBootApplication
annotation.
另外,我们可以只使用@SpringBootApplication
注解。
@SpringBootApplication
public class SwingApp extends JFrame {
See my Spring Boot Swing integrationtutorial for a full working example.
有关完整的工作示例,请参阅我的Spring Boot Swing 集成教程。
回答by George Z.
Another simple and elegant solution is to disable headless property as shown here.You will have to do it though, right before you create/show the JFrame.
另一个简单而优雅的解决方案是禁用无头属性,如下所示。但是,您必须在创建/显示 JFrame 之前执行此操作。
System.setProperty("java.awt.headless", "false"); //Disables headless
So, the thing that worked for me:
所以,对我有用的东西:
SpringApplication.run(MyClass.class, args);
System.setProperty("java.awt.headless", "false");
SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame("myframe");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
});
Annotations in MyClass.class (I am not aware if they play any role):
MyClass.class 中的注释(我不知道它们是否起作用):
@SpringBootApplication
@EnableWebMvc