如何整合swing跟springboot,swing显示springboot启动进度的方法

1.使用spring管理swing的ui内容和控制器

2.监听spring中bean实例化的数量实现监控进度的作用,用rxjava事件通知实现swing界面进度更新

3.使用JFromDesigner设计页面

完整代码 https://gitee.com/sgz2/swingexamples

效果

如何整合swing跟springboot,swing显示springboot启动进度的方法_第1张图片

项目结构

如何整合swing跟springboot,swing显示springboot启动进度的方法_第2张图片

启动类

package com.saoft.swing;

import com.saoft.swing.mvc.main.MainFrameController;
import com.saoft.swing.mvc.splash.Splash;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

import javax.swing.*;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        //先启动启动画面
        banner();

        long start = System.currentTimeMillis();
        //启动springboot
        new SwingWorker(){
            @Override
            protected Object doInBackground() {
                ConfigurableApplicationContext context = new SpringApplicationBuilder(App.class)
                                                                .headless(false)
                                                                .run(args);

                MainFrameController bean = context.getBean(MainFrameController.class);
                bean.prepareAndOpenFrame();

                long end = System.currentTimeMillis();
                System.out.println("time:"+(end-start)+"ms");
                return null;
            }
        }.execute();
    }

    private static void banner(){
        Splash splash = new Splash();
        splash.setVisible(true);
        //监听进度并更新
        ProgressBeanPostProcessor.observe().subscribe(integer -> splash.getProgress().setValue(integer)
                ,e->{}
                ,() ->splash.setVisible(false));
    }
}

 

bean数量监听

package com.saoft.swing;

import io.reactivex.Observable;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subjects.Subject;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

@Component("ProgressBeanPostProcessor")
public class ProgressBeanPostProcessor implements BeanPostProcessor,ApplicationListener {

    //所有bean的总数随项目增加而增加
    private Integer total = 18;

    private AtomicInteger count = new AtomicInteger(0);

    private final static Subject beans = BehaviorSubject.create();


    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        count.incrementAndGet();
        beans.onNext(count.get()*100/total);
        return bean;
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent applicationEvent) {
        //最终应该设置的total值
        System.out.println("total:");
        System.out.println(count.get());
        beans.onComplete();
    }

    public static Observable observe() {
        return beans;
    }
}

 

你可能感兴趣的:(Swing基础,SpringBoot基础)