web-dev-qa-db-ja.com

SwingアプリケーションでSpring-Bootを構成する方法

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

そしてboot application classは次のようになります:

@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 

よろしく。

21
Dapaldo

あなたが質問を投稿してからしばらく経ちましたが、私が移行している古いプロジェクトで同じ問題で不足し、別の方法を考えました。

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

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 {

完全に機能する例については、私の Spring Boot Swing統合 チュートリアルを参照してください。

10
Jan Bodnar

もう1つのシンプルでエレガントなソリューションは、次のようにヘッドレスプロパティを無効にすることです here。 ただし、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
2
George Z.