swing 어플리케이션을 사용한 스프링부트 설정 방법
spring-boot-starter-data-jpa 기능을 사용하여 웹 이외의 복제를 만들고 싶습니다.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
안부 전해요.
질문을 게시한 지 오래되었지만 이전 프로젝트에서도 이전 프로젝트와 동일한 문제가 발생했고, 더 명확하게 작업을 수행하기 위해 다른 방법을 생각해 냈습니다.
사용하는 대신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);
}
Swing 애플리케이션은 Swing 이벤트 큐에 배치해야 합니다.그렇게 하지 않는 것은 심각한 실수입니다.
올바른 방법은 다음과 같습니다.
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);
});
}
게다가, 우리는 단지,@SpringBootApplication
주석입니다.
@SpringBootApplication
public class SwingApp extends JFrame {
전체 작업 예는 스프링 부트 스윙 통합 튜토리얼을 참조하십시오.
또 하나의 심플하고 우아한 솔루션은 다음과 같이 헤드리스 속성을 비활성화하는 것입니다.단, JFrame을 작성/표시하기 전에 실행해야 합니다.
System.setProperty("java.awt.headless", "false"); //Disables headless
그래서, 제게 효과가 있었던 건
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);
});
MyClass.class의 주석(역할이 있는지 알 수 없음):
@SpringBootApplication
@EnableWebMvc
솔루션은 매우 심플하다는 것을 알았습니다(스프링 서버가 먼저 실행되고 스윙이 나중에 실행됨).
//@Component
public class SwingApp extends JFrame {
vv...
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class)
.headless(false).run(args);
SwingApp sw = new SwingApp();
sw.setVisible(true);
}
}
swing run 먼저 실행, call spring server 나중에 실행
//@Component
public class SwingApp extends JFrame {
vv...
private void jButtonRunSpringServer(java.awt.event.ActionEvent evt){
String [] args = {"abc","xyz"};
Application.runSpringServer(args);
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SwingApp sw = new SwingApp();
sw.setVisible(true);
}
public static void runSpringServer(String[] args) {
/*
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class)
.headless(false).run(args);
*/
SpringApplication.run(Application.class, args);
}
}
언급URL : https://stackoverflow.com/questions/22864008/how-to-configure-spring-boot-with-swing-application
'programing' 카테고리의 다른 글
워드프레스에서 모든 카테고리를 표시하는 방법 (0) | 2023.03.22 |
---|---|
React.js - 기본 프로펠이 null과 함께 사용되지 않습니다. (0) | 2023.03.22 |
사후 대응적인 Web Client가 3XX 리다이렉트를 따르도록 하려면 어떻게 해야 합니까? (0) | 2023.03.22 |
워드프레스 모든 투고 표시 (0) | 2023.03.22 |
React 상태 비저장 구성 요소의 TypeScript 반환 유형은 무엇입니까? (0) | 2023.03.22 |