swing-浅谈swing整合spring的启动优化

本文探讨了如何优化Spring与Swing结合的应用,提出四个关键优化点:避免main线程阻塞UI,提前显示GUI,减少Spring扫描bean对UI的影响,以及解耦业务和框架。通过这些措施,可以显著提高GUI应用程序的启动速度和响应性,实现更快的视图加载和切换。此外,建议使用自定义注解增强功能并利用缓存加速视图渲染。
摘要由CSDN通过智能技术生成

在这里插入图片描述

启动日志

15:19:05.342 INFO  [main] cn.note.swing.ViewApplication (ViewApplication.java:36) : start time 
15:19:05.534 DEBUG [main] o.s.c.a.AnnotationConfigApplicationContext (AbstractApplicationContext.java:629) : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@77cd7a0 
15:19:05.546 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' 
15:19:05.628 DEBUG [main] o.s.c.a.ClassPathBeanDefinitionScanner (ClassPathScanningCandidateComponentProvider.java:435) : Identified candidate component class: file [\note-swing-core\target\classes\cn\note\swing\core\util\core\SpringContext.class] 
15:19:05.631 DEBUG [main] o.s.c.a.ClassPathBeanDefinitionScanner (ClassPathScanningCandidateComponentProvider.java:435) : Identified candidate component class: file [\note-swing-single\target\classes\cn\note\swing\formatter\CodeFormatterView.class] 
15:19:05.634 DEBUG [main] o.s.c.a.ClassPathBeanDefinitionScanner (ClassPathScanningCandidateComponentProvider.java:435) : Identified candidate component class: file [\note-swing-single\target\classes\cn\note\swing\single\SingleAutoHelper.class] 
15:19:05.705 INFO  [AWT-EventQueue-0] cn.note.swing.ViewApplication (ViewApplication.java:66) : show gui application 
15:19:05.760 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor' 
15:19:05.759 INFO  [AWT-EventQueue-0] c.n.s.config.SpringBeanConfig (SpringBeanConfig.java:47) : 异步注册: @Component&ItemView==>CodeFormatterView 
15:19:05.761 INFO  [AWT-EventQueue-0] c.n.s.config.SpringBeanConfig (SpringBeanConfig.java:47) : 异步注册: @Component&ItemView==>SingleAutoHelper 
15:19:05.769 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory' 
15:19:05.770 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' 
15:19:05.772 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' 
15:19:05.781 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'springBeanConfig' 
15:19:05.785 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'springContext' 
15:19:05.786 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'codeFormatterView' 
15:19:06.047 INFO  [AWT-EventQueue-0] cn.note.swing.ViewApplication (ViewApplication.java:69) : slider menu load complete 
register==>JSON
15:19:06.200 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'singleAutoHelper' 
15:19:06.927 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'application' 
15:19:07.000 INFO  [main] cn.note.swing.ViewApplication (ViewApplication.java:55) : gui launch milli time:1659 15:19:05.342 INFO  [main] cn.note.swing.ViewApplication (ViewApplication.java:36) : start time 
15:19:05.534 DEBUG [main] o.s.c.a.AnnotationConfigApplicationContext (AbstractApplicationContext.java:629) : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@77cd7a0 
15:19:05.546 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' 
15:19:05.628 DEBUG [main] o.s.c.a.ClassPathBeanDefinitionScanner (ClassPathScanningCandidateComponentProvider.java:435) : Identified candidate component class: file [\note-swing-core\target\classes\cn\note\swing\core\util\core\SpringContext.class] 
15:19:05.631 DEBUG [main] o.s.c.a.ClassPathBeanDefinitionScanner (ClassPathScanningCandidateComponentProvider.java:435) : Identified candidate component class: file [\note-swing-single\target\classes\cn\note\swing\formatter\CodeFormatterView.class] 
15:19:05.634 DEBUG [main] o.s.c.a.ClassPathBeanDefinitionScanner (ClassPathScanningCandidateComponentProvider.java:435) : Identified candidate component class: file [\note-swing-single\target\classes\cn\note\swing\single\SingleAutoHelper.class] 
15:19:05.705 INFO  [AWT-EventQueue-0] cn.note.swing.ViewApplication (ViewApplication.java:66) : show gui application 
15:19:05.760 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor' 
15:19:05.759 INFO  [AWT-EventQueue-0] c.n.s.config.SpringBeanConfig (SpringBeanConfig.java:47) : 异步注册: @Component&ItemView==>CodeFormatterView 
15:19:05.761 INFO  [AWT-EventQueue-0] c.n.s.config.SpringBeanConfig (SpringBeanConfig.java:47) : 异步注册: @Component&ItemView==>SingleAutoHelper 
15:19:05.769 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory' 
15:19:05.770 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' 
15:19:05.772 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' 
15:19:05.781 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'springBeanConfig' 
15:19:05.785 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'springContext' 
15:19:05.786 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'codeFormatterView' 
15:19:06.047 INFO  [AWT-EventQueue-0] cn.note.swing.ViewApplication (ViewApplication.java:69) : slider menu load complete 
register==>JSON
15:19:06.200 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'singleAutoHelper' 
15:19:06.927 DEBUG [main] o.s.b.f.s.DefaultListableBeanFactory (DefaultSingletonBeanRegistry.java:225) : Creating shared instance of singleton bean 'application' 
15:19:07.000 INFO  [main] cn.note.swing.ViewApplication (ViewApplication.java:55) : gui launch milli time:1659 

在这里插入图片描述

优化一: main线程不要阻塞UI线程

spring的扫描bean时不要阻塞gui的渲染.

优化二: UI应该更早的显示

在使用UI框架渲染完成时,可以将JFrame立刻显示出来,

  • 在上图日志中大概只有0.4s, 此时看到的了GUI的外科轮廓
  • 这时GUI已经加载好了内置的UIManager
  • 此时spring还没有扫描bean完成

优化三: 框架应该与业务合理的解耦

左侧菜单为@Compoent注解的菜单项, 如何获取更多的菜单信息

  • 自定义注解,实现图标/ 分组 /描述等功能
    在这里插入图片描述
    在这里插入图片描述

  • 此时可以有好多好处
    1. spring内置的IOC使该业务可以解耦, 既可以作为独立应用测试, 也可以作为子应用注册.把控权在业务.
    2. 如果该业务不想作为左侧菜单项时, 可以直接去除@ItemView注解,UI上的解耦

  • 左侧菜单可以只扫描部分注解, 理论上肯定比spring加载快,因为spring还需要加载默认注解

  • 因为左侧菜单使用AWT 线程加载, spring在MAIN线程加载, 所以spring加载完成时, 需要判断左侧视图是否加载完成, 加载完成再去激活第一个视图
    在这里插入图片描述

优化四: 适当的使用缓存

 当左侧菜单加载完成时,可以去激活第一个视图, 此时左侧的菜单的图形UI已经显示
  • 通过建立SpringUtil上下文管理工具, 直接动态获取bean的名称,可以实现右侧视图的动态渲染
  • 每次渲染的视图 和他的class 类路径 可以分别作为缓存Map的value和key

优化的目的: 更快的看到,更快的更新,更快的切换

那么你的Swing小工具的速度 可以faster faster faster
请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值