如何使用摆动应用程序配置弹簧启动
2022-09-01 21:47:32
我想使用spring-boot-starter-data-jpa功能来创建一个非Web应用程序。在52.4文档中说:
要作为业务逻辑运行的应用程序代码可以作为 CommandLineRunner 实现,并作为@Bean定义放入上下文中。
我的 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();
}
}
});
}
引导应用程序类如下所示:
@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);
}
}
但不起作用。有人有这方面的样本吗?
已编辑并添加了例外。
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
问候。