Tôi muốn sử dụng các tính năng spring-boot-starter-data-jpa để tạo một ứng dụng phi web. Trong tài liệu 52,4 nói:Cách cấu hình khởi động mùa xuân bằng ứng dụng xoay
mã ứng dụng mà bạn muốn chạy như logic kinh doanh của bạn có thể được thực hiện như một CommandLineRunner và rơi vào bối cảnh như một định nghĩa @Bean .
My AppPrincipalFrame trông giống như:
@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();
}
}
});
}
Và khởi động ứng dụng lớp trông giống như:
@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);
}
}
Nhưng không hoạt động. Bất kỳ ai có một mẫu về điều này?
Chỉnh sửa và ngoại lệ được thêm vào.
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
Kính trọng.
Điều gì "không hoạt động"? Có ngoại lệ không? Nhật ký? –
@DaveSyer có, tóm tắt ngoại lệ được thêm vào. – Dapaldo
Mùa xuân được gọi là hàm tạo của bạn và nếu không thành công. Có vẻ như hàm tạo đó sẽ được gọi hai lần (một lần bằng Spring để tạo '@ Component' của bạn và một lần trong phương thức' run() 'của riêng nó). Hơi kỳ lạ, vì vậy có lẽ bạn không có ý định một trong số đó? –