当前位置: 首页 > news >正文

springboot3整合javafx解决bean注入问题

springboot整合javafx时候,很多问题就在于controller没有被spring容器管理,无法注入bean,在这里提供一套自己的解决思路

 

执行逻辑

这里仅仅提供一个演示,我点击按钮之后,从service层返回一个文本并显示
PixPin_2024-12-13_23-26-38.gif

项目结构

创建一个springboot项目
image.png

 

关键代码

springboot启动类中

image.png
FXApplication启动类中
image.png

全部代码

仅作示例,自行修改
 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.shkj</groupId><artifactId>video-classification</artifactId><version>0.0.1-SNAPSHOT</version><name>video-classification</name><description>video-classification</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version><javafx.version>21-ea+24</javafx.version><mybatis-plus-boot-stater.version>3.5.6</mybatis-plus-boot-stater.version><mysql-connector-java.varsion>8.0.18</mysql-connector-java.varsion><druid.version>1.1.23</druid.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.openjfx</groupId><artifactId>javafx-base</artifactId><version>${javafx.version}</version></dependency><dependency><groupId>org.openjfx</groupId><artifactId>javafx-controls</artifactId><version>${javafx.version}</version></dependency><dependency><groupId>org.openjfx</groupId><artifactId>javafx-fxml</artifactId><version>${javafx.version}</version></dependency><dependency><groupId>org.openjfx</groupId><artifactId>javafx-media</artifactId><version>${javafx.version}</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql-connector-java.varsion}</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-annotation</artifactId><version>${mybatis-plus-boot-stater.version}</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus-boot-stater.version}</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-annotation</artifactId><version>${mybatis-plus-boot-stater.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

VideoClassificationApplication

package com.shkj.videoclassification;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication(scanBasePackages = "com.shkj.videoclassification")
public class VideoClassificationApplication   {public static ConfigurableApplicationContext applicationContext;public static void main(String[] args) {applicationContext=SpringApplication.run(VideoClassificationApplication.class, args);FXApplication.main(args);}}

FXApplication


package com.shkj.videoclassification;import com.shkj.videoclassification.controller.HelloController;
import com.shkj.videoclassification.service.impl.TestServiceImpl;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.io.IOException;/*** @author shkj-大地* @create 2024-12-13 21:41* @description:*/
public class FXApplication  extends Application {@Overridepublic void start(Stage stage) throws IOException {FXMLLoader fxmlLoader = new FXMLLoader(VideoClassificationApplication.class.getResource("/view/hello.fxml"));// 注入fxmlLoader.setControllerFactory(VideoClassificationApplication.applicationContext::getBean);Scene scene = new Scene(fxmlLoader.load());stage.setTitle("Hello!");stage.setScene(scene);stage.show();}public static void main(String[] args) {launch(args);}}

application.yaml

这里也就是你自己平时用的连接数据库的配置,还想看我的?

hello.fxml 一个简单的页面

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?><AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/23.0.1"xmlns:fx="http://javafx.com/fxml/1"fx:controller="com.shkj.videoclassification.controller.HelloController"><children><Button onAction="#onButtonClick" layoutX="210.0" layoutY="272.0" mnemonicParsing="false"prefHeight="63.0" prefWidth="181.0" text="Button" /><Label fx:id="textLabel" layoutX="181.0" layoutY="83.0" prefHeight="95.0" prefWidth="239.0" text="Label" /></children>
</AnchorPane>

HelloController

package com.shkj.videoclassification.controller;import com.shkj.videoclassification.service.TestService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;/*** @author shkj-大地* @create 2024-12-13 21:36* @description:*/
@Component
public class HelloController {@Autowiredprivate TestService testService;@FXMLpublic Label textLabel;@FXMLpublic void onButtonClick(ActionEvent actionEvent) {String test = testService.test();textLabel.setText("Hello World!"+test);}
}

TestServiceImpl


package com.shkj.videoclassification.service.impl;import com.shkj.videoclassification.service.TestService;
import org.springframework.stereotype.Service;/*** @author shkj-大地* @create 2024-12-13 21:45* @description:*/
@Service
public class TestServiceImpl  implements TestService {@Overridepublic String test() {//你自己去写数据库查询语句吧//到这里了还用我教你?return "我是service测试信息";}
}

TestService

package com.shkj.videoclassification.service;/*** @author shkj-大地* @create 2024-12-13 21:44* @description:*/
public interface TestService {String test();
}

相关文章:

springboot3整合javafx解决bean注入问题

springboot整合javafx时候&#xff0c;很多问题就在于controller没有被spring容器管理&#xff0c;无法注入bean&#xff0c;在这里提供一套自己的解决思路 执行逻辑 这里仅仅提供一个演示&#xff0c;我点击按钮之后&#xff0c;从service层返回一个文本并显示 项目结构 创…...

.NET 8 Blazor Web项目中的 .razor 文件与 .cshtml 文件的本质区别

在.NET 8 Blazor Web项目中&#xff0c;.razor 和 .cshtml 文件是常用的视图文件格式。尽管它们看起来有相似之处&#xff0c;但在使用方式、功能和渲染机制上有着根本的不同。理解它们的本质区别&#xff0c;有助于开发者更好地选择合适的文件格式&#xff0c;并构建符合需求的…...

SpringBoot快速使用

一些名词的碎碎念: 1> 俩种网络应用设计模式 C/S 客户端/服务器 B/S 浏览器/服务器 俩者对比: 2> 集群和分布式的概念 集群: 分布式: 例子: 一个公司有一个人身兼多职 集群: 招聘N个和上面这个人一样身兼多职 分布式: 招聘N个人,分担上面这个人的工作,进行工作的拆分. 工…...

【C语言实现:用队列模拟栈与用栈模拟队列(LeetCode 225 232)】

LeetCode刷题记录 &#x1f310; 我的博客主页&#xff1a;iiiiiankor&#x1f3af; 如果你觉得我的内容对你有帮助&#xff0c;不妨点个赞&#x1f44d;、留个评论✍&#xff0c;或者收藏⭐&#xff0c;让我们一起进步&#xff01;&#x1f4dd; 专栏系列&#xff1a;LeetCode…...

远程控制软件对比与使用推荐

远程控制软件对比与使用推荐 远程控制软件在现代工作环境中扮演着重要角色&#xff0c;无论是远程办公、技术支持、还是家庭成员之间的协助。以下是对几种常见远程控制软件的详细对比和推荐使用场景。 1. TeamViewer 特点 跨平台&#xff1a;支持Windows、macOS、Linux、iO…...

vue canvas 绘制选定区域 矩形框

客户那边文档相当的多&#xff0c;目前需要协助其将文档转为数据写入数据库&#xff0c;并与其他系统进行数据共享及建设&#xff0c;所以不得不搞一个识别的功能&#xff0c;用户上传PDF文档后&#xff0c;对于关键信息点进行识别入库&#xff01; 以下为核心代码&#xff0c…...

【SpringCloud】OpenFeign配置时间Decode

文章目录 1.自定义反序列化器2.配置类与自定义 ObjectMapper客户端 需求&#xff1a;OpenFeign配置自定义decode&#xff0c;解析http请求返回的时间字符串 1.自定义反序列化器 Date 自定义反序列化器 import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.j…...

Xerces-C,一个成熟的 C++ XML 解析库!

嗨&#xff0c;大家好&#xff01;我是一行。今天咱们来探索 Xerces-C&#xff0c;它可是 C里超棒的 XML 解析库哦&#xff01;能帮咱轻松处理 XML 数据&#xff0c;在很多数据交互、配置文件读取场景都超实用&#xff0c;快来一起学习使用它的妙招吧。 一、Xerces-C 是什么&am…...

6.2 MapReduce工作原理

MapReduce工作原理涉及将大数据集分割成小块并行处理。Map任务读取数据块并输出中间键值对&#xff0c;而Reduce任务则处理这些排序后的数据以生成最终结果。MapTask工作包括读取数据、应用Map函数、收集输出、内存溢出时写入磁盘以及可选的Combiner局部聚合。ReduceTask工作则…...

一次旧业务系统迁移收缩的经历

单位的一个业务系统&#xff0c;在几年前已经更换了。但旧的系统里面还有很多没有转移过来的数据&#xff0c;虽然普通用户不再需要用旧的系统&#xff0c;但相应部门的管理人员还需要在旧系统查询数据资料&#xff0c;这应该是旧系统向新系统迁移时&#xff0c;数据不彻底&…...

MVC配置文件及位置

配置文件位置 默认位置 WEB-INF目录下&#xff0c;文件名&#xff1a;<servlet-name>-servlet.xml <?xml version"1.0" encoding"UTF-8"?> <web-app xmlns"http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi"http://www.w3.…...

如何解决samba服务器共享文件夹不能粘贴文件

sudo vim /etc/samba/smb.conf在samba的配置文件中增加一个选项 writable yes重启Samba服务以使更改生效&#xff1a; sudo service smbd restart...

【中工开发者】鸿蒙商城app

这学期我学习了鸿蒙&#xff0c;想用鸿蒙做一个鸿蒙商城app&#xff0c;来展示一下。 项目环境搭建&#xff1a; 1.开发环境&#xff1a;DevEco Studio2.开发语言&#xff1a;ArkTS3.运行环境&#xff1a;Harmony NEXT base1 软件要求&#xff1a; DevEco Studio 5.0.0 Rel…...

(九)机器学习 - 多项式回归

多项式回归&#xff08;Polynomial Regression&#xff09;是一种回归分析方法&#xff0c;它将自变量 xx 和因变量 yy 之间的关系建模为 nn 次多项式。多项式回归的目的是找到一个 nn 次多项式函数&#xff0c;使得这个函数能够最好地拟合给定的数据点。 多项式回归的数学表达…...

Qt编写区位码gb2312、机内码、国标码————附带详细介绍和编码实现

文章目录 0 背景1 了解编码1.1 ASCII码1.2 机内码、国标码、区位码1.2.1 区位码1.2.2 国标码&#xff08;GB 2312-80&#xff09;1.2.3 汉字机内码&#xff08;GB 2312&#xff09; 1.3 GBK和GB2312的区别2 编码实现2.1 QString数据转QByteArray类型2.1.1 使用QTextCodec2.1.2 …...

linux网络编程 | c | epoll实现IO多路转接服务器

epoll实现IO多路转接服务器 可通过以下视频学习 06-opell函数实现的多路IO转接_哔哩哔哩_bilibili 通过响应式–多路IO转接实现 文章目录 epoll实现IO多路转接服务器1.思路&功能核心思路 2.代码实现multi_epoll_sever.c运行图 1.思路&功能 **功能&#xff1a;**客…...

Source Insight的使用经验汇总

01-Add All"和“Add Tree”有何区别&#xff1f; 在 Source Insight 中&#xff0c;“Add All”和“Add Tree”是两种向项目&#xff08;Project&#xff09;中添加文件的操作选项&#xff0c;它们的区别在于处理文件和目录的方式不同&#xff1a; 1. Add All 范围&am…...

VSCode 报错:rust-analyzer requires glibc >= 2.28 in latest build

报错信息 /home/jake/.vscode-server-insiders/extensions/matklad.rust-analyzer-0.3.953/server/rust-analyzer: /lib/x86_64-linux-gnu/libc.so.6: version GLIBC_2.29 not found (required by /home/jake/.vscode-server-insiders/extensions/matklad.rust-analyzer-0.3.9…...

Android Link to Death 使用

Java侧&#xff1a; 【android学习】使用linkToDeath对AIDL双向死亡监听_unlinktodeath-CSDN博客 Native侧&#xff1a; Service端 using namespace android; class MyService :public IBinder::DeathRecipient{void MyService::binderDied(const wp<IBinder>& wh…...

【C++游记】string的使用和模拟实现

枫の个人主页 你不能改变过去&#xff0c;但你可以改变未来 算法/C/数据结构/C Hello&#xff0c;这里是小枫。C语言与数据结构和算法初阶两个板块都更新完毕&#xff0c;我们继续来学习C的内容呀。C是接近底层有比较经典的语言&#xff0c;因此学习起来注定枯燥无味&#xf…...

OpenClaw+Qwen3.5-4B-Claude-4.6-Opus-Reasoning-Distilled-GGUF:3个低成本自动化场景实测

OpenClawQwen3.5-4B-Claude-4.6-Opus-Reasoning-Distilled-GGUF&#xff1a;3个低成本自动化场景实测 1. 为什么选择这个组合&#xff1f; 上个月在折腾个人自动化工作流时&#xff0c;我遇到了一个典型矛盾&#xff1a;既希望AI能处理复杂的代码和文档任务&#xff0c;又受限…...

如何保证代码质量?

一、编码阶段&#xff1a;从源头控制质量1. 统一代码规范&#xff08;强制执行&#xff09;核心目标&#xff1a;减少风格差异&#xff0c;提高可读性常见工具&#xff1a;ESLint&#xff1a;代码规范校验Prettier&#xff1a;自动格式化Stylelint&#xff1a;样式规范&#x1…...

卡证检测矫正模型效果展示:高清四角点定位+正视角矫正图实拍

卡证检测矫正模型效果展示&#xff1a;高清四角点定位正视角矫正图实拍 你有没有遇到过这样的烦恼&#xff1f;需要上传身份证、驾照或者护照照片时&#xff0c;手机随手一拍&#xff0c;结果照片歪歪扭扭&#xff0c;背景杂乱&#xff0c;关键信息还被手指挡住了。这时候要么…...

Android-Animation-Set转场动画实战:共享元素与Activity切换的完美结合

Android-Animation-Set转场动画实战&#xff1a;共享元素与Activity切换的完美结合 【免费下载链接】Android-Animation-Set :books: Android 所有动画系列详尽教程。 Explain all animations in Android. 项目地址: https://gitcode.com/gh_mirrors/an/Android-Animation-S…...

IAR平台华大HC32F460工程搭建避坑指南:从零到调试成功的全流程解析

1. 从KEIL到IAR的转型背景 最近两年芯片市场的价格波动&#xff0c;让很多工程师不得不重新评估开发工具链的选择。我作为一个用了五年KEIL的老用户&#xff0c;最近也被迫开始学习IAR平台。原因很简单——当ST单片机价格涨到华大HC32F460的十倍时&#xff0c;任何成本敏感的项…...

原神抽卡记录导出工具:一键备份分析你的抽卡历史数据

原神抽卡记录导出工具&#xff1a;一键备份分析你的抽卡历史数据 【免费下载链接】genshin-wish-export biuuu/genshin-wish-export - 一个使用Electron制作的原神祈愿记录导出工具&#xff0c;它可以通过读取游戏日志或代理模式获取访问游戏祈愿记录API所需的authKey。 项目…...

SerialTransfer:Arduino轻量级高可靠串行通信协议栈

1. SerialTransfer 库概述SerialTransfer 是一款专为 Arduino 平台设计的轻量级、高可靠性串行通信协议栈&#xff0c;其核心目标是解决嵌入式系统中跨设备数据交换的通用性、鲁棒性与工程可维护性问题。它并非简单的Serial.write()封装&#xff0c;而是一套完整的面向帧&#…...

被裁员后,我用这个 AI 助手每天只工作 2 小时|OpenClaw 实战

&#x1f62d; 被裁员后&#xff0c;我用这个 AI 助手每天只工作 2 小时“真正的自由&#xff0c;不是想做什么就做什么&#xff0c;而是不想做什么就可以不做什么”01 一个普通打工人的至暗时刻 上个月&#xff0c;公司裁员 30%。 我所在的部门&#xff0c;5 个人走了 3 个。 …...

虚拟机自动化新范式:CUA Computer SDK十分钟入门指南

虚拟机自动化新范式&#xff1a;CUA Computer SDK十分钟入门指南 【免费下载链接】cua Create and run high-performance macOS and Linux VMs on Apple Silicon, with built-in support for AI agents. 项目地址: https://gitcode.com/GitHub_Trending/cua/cua 在当今的…...

LingBot-Depth部署教程:Docker Compose编排+模型缓存卷自动初始化

LingBot-Depth部署教程&#xff1a;Docker Compose编排模型缓存卷自动初始化 1. 引言&#xff1a;从稀疏数据到精准3D测量 你有没有遇到过这样的场景&#xff1f;手头有一个深度摄像头&#xff0c;但采集到的深度图总是零零散散&#xff0c;像一张被撕破的旧地图&#xff0c;…...