简体   繁体   English

弹簧与秋千

[英]Spring along with Swing

I'm using Spring along with Swing. 我正在将Spring和Swing一起使用。 At the click of a Button the following code block is being called. 单击一个按钮,将调用以下代码块。

@SpringBootApplication
public class ProcessFile {

    public String startProcess() {
        ConfigurableApplicationContext context = SpringApplication.run(ProcessFile.class);
        ReadFile readFile = context.getBean(ReadFile.class);
        return readFile.getData();
    }

}

But am just wondering if I click that multiple times then will multiple Spring context be created or is there some other issues. 但是我只是想知道我是否单击多次,是否会创建多个Spring上下文,或者还有其他问题。 My UI has just a Single JPanel to select a file and call this startProcess method which does many tasks. 我的UI仅有一个JPanel,可以选择一个文件并调用此startProcess方法来执行许多任务。 Or will the SpringContext be destroyed when the following code block execution finishes? 还是在以下代码块执行完成时销毁SpringContext?

I think about your health you should only have a spring application context in your application :) 我认为您的健康状况应该只在您的应用程序中包含spring应用程序上下文:)

For this you can implement a CommandLineRunner and display the Swing application: 为此,您可以实现CommandLineRunner并显示Swing应用程序:

@Component
public class Runner implements CommandLineRunner {

    /**
     * the JFrame to be displayed.
     */
    @Autowired
    private DemoFrame frame;

    @Override
    public void run(String... args) throws Exception {
        /* display the form using the AWT EventQueue */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                frame.setVisible(true);
            }
       });
    }

}



@Component
public class DemoFrame extends JFrame{
    // other code here....

    @Autowired
    ReadFile readFile;

    public String startProcess() {
        return readFile.getData();
    }

    // other code here....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM