如何使用swing应用程序配置spring-boot

时间:2014-04-04 13:20:12

标签: java 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();
            }
        }
    });
}

启动应用程序类看起来像:

@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 

问候。

4 个答案:

答案 0 :(得分:33)

自从您发布问题以来已经有一段时间了,但我在一个旧项目中遇到了同样的问题,我正在迁移并想出一个不同的方式,我认为更清楚,事情有效。

您可以使用<body> <form id="form1" runat="server"> <div class="form-horizontal"> <h4>Please add your information</h4> <hr /> <asp:ValidationSummary runat="server" CssClass="text-danger" /> <div class="form-group form-horizontal col-md-8"> <asp:Label ID="lblName" runat="server" AssociatedControlID="txtName" Text="Name" CssClass="col-md-2 control-label"></asp:Label> <div class="col-md-6"> <asp:TextBox ID="txtName" runat="server" CssClass="form-control"></asp:TextBox> </div> <br /> <br /> <asp:Label ID="lblLastName" runat="server" AssociatedControlID="txtLastName" Text="Last Name" CssClass="col-md-2 control-label"></asp:Label> <div class="col-md-6"> <asp:TextBox ID="txtLastName" runat="server" CssClass="form-control"></asp:TextBox> </div> <br /> <br /> <asp:Label runat="server" AssociatedControlID="Address1" CssClass="col-md-2 control-label" Text="Address1"></asp:Label> <div class="col-md-6"> <asp:TextBox runat="server" ID="Address1" CssClass="form-control" /> </div> <br /> <br /> <asp:Label runat="server" AssociatedControlID="Address2" CssClass="col-md-2 control-label" Text="Address2"></asp:Label> <div class="col-md-6"> <asp:TextBox runat="server" ID="Address2" CssClass="form-control" /> </div> <br /> <br /> <asp:Label ID="lblCity" runat="server" AssociatedControlID="txtCity" Text="City" CssClass="col-md-2 control-label" Font-Bold="true"></asp:Label> <div class="col-md-6"> <asp:TextBox runat="server" ID="txtCity" CssClass="form-control" /> </div> <br /> <br /> <div class="form-group"> <asp:Label ID="lblState" runat="server" AssociatedControlID="txtState" Text="State" CssClass="col-md-2 control-label" Font-Bold="true"></asp:Label> <div class="col-md-2"> <asp:TextBox runat="server" ID="txtState" CssClass="form-control" /> </div> <asp:Label ID="lblZip" runat="server" AssociatedControlID="txtZip" Text="Zip" CssClass="col-md-2 control-label" Font-Bold="true"></asp:Label> <div class="col-md-2"> <asp:TextBox runat="server" ID="txtZip" CssClass="form-control" /> </div> </div> <asp:Label ID="lblCountry" runat="server" AssociatedControlID="ddCountry" Text="Country" CssClass="col-md-2 control-label" Font-Bold="true"></asp:Label> <div class="col-md-6"> <asp:DropDownList ID="ddCountry" runat="server" CssClass="form-control"></asp:DropDownList> </div> </div> <div class="form-group"> <div class="col-md-offset-4 col-md-2"> <asp:Button ID="btnSubmit" runat="server" Text="Send" CssClass="btn btn-primary" /> </div> </div> </div> </form> 而不是使用SpringApplication.run(Application.class, args);,并且不必使玩具应用程序类从JFrame扩展。因此代码看起来像:

new SpringApplicationBuilder(Main.class).headless(false).run(args);

答案 1 :(得分:7)

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 integration教程。

答案 2 :(得分:2)

另一种简单优雅的解决方案是禁用无头属性,如图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

答案 3 :(得分:0)

我发现解决方案非常简单(先运行spring服务器,再运行swing服务器)

//@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,再调用spring服务器运行

//@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);
    }
}