网站开发还是软件开发,如何设计网站制作方案,物联网平台是什么意思,视频拍摄合同springboot整合javafx时候#xff0c;很多问题就在于controller没有被spring容器管理#xff0c;无法注入bean#xff0c;在这里提供一套自己的解决思路 执行逻辑
这里仅仅提供一个演示#xff0c;我点击按钮之后#xff0c;从service层返回一个文本并显示
项目结构
创…springboot整合javafx时候很多问题就在于controller没有被spring容器管理无法注入bean在这里提供一套自己的解决思路 执行逻辑
这里仅仅提供一个演示我点击按钮之后从service层返回一个文本并显示
项目结构
创建一个springboot项目 关键代码
springboot启动类中 FXApplication启动类中
全部代码
仅作示例自行修改
pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.4.0/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.shkj/groupIdartifactIdvideo-classification/artifactIdversion0.0.1-SNAPSHOT/versionnamevideo-classification/namedescriptionvideo-classification/descriptionurl/licenseslicense//licensesdevelopersdeveloper//developersscmconnection/developerConnection/tag/url//scmpropertiesjava.version17/java.versionjavafx.version21-ea24/javafx.versionmybatis-plus-boot-stater.version3.5.6/mybatis-plus-boot-stater.versionmysql-connector-java.varsion8.0.18/mysql-connector-java.varsiondruid.version1.1.23/druid.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.openjfx/groupIdartifactIdjavafx-base/artifactIdversion${javafx.version}/version/dependencydependencygroupIdorg.openjfx/groupIdartifactIdjavafx-controls/artifactIdversion${javafx.version}/version/dependencydependencygroupIdorg.openjfx/groupIdartifactIdjavafx-fxml/artifactIdversion${javafx.version}/version/dependencydependencygroupIdorg.openjfx/groupIdartifactIdjavafx-media/artifactIdversion${javafx.version}/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion${mysql-connector-java.varsion}/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-annotation/artifactIdversion${mybatis-plus-boot-stater.version}/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion${mybatis-plus-boot-stater.version}/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-annotation/artifactIdversion${mybatis-plus-boot-stater.version}/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion${druid.version}/version/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-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) {applicationContextSpringApplication.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 version1.0 encodingUTF-8??import javafx.scene.control.Button?
?import javafx.scene.control.Label?
?import javafx.scene.layout.AnchorPane?AnchorPane prefHeight400.0 prefWidth600.0 xmlnshttp://javafx.com/javafx/23.0.1xmlns:fxhttp://javafx.com/fxml/1fx:controllercom.shkj.videoclassification.controller.HelloControllerchildrenButton onAction#onButtonClick layoutX210.0 layoutY272.0 mnemonicParsingfalseprefHeight63.0 prefWidth181.0 textButton /Label fx:idtextLabel layoutX181.0 layoutY83.0 prefHeight95.0 prefWidth239.0 textLabel //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();
}