SpringBoot整合JavaFx(十三)

在Java中,基本上万物可springboot… 整合了spring全家桶,你可以很方便整合它的生态框架。
JavaFx也能整合springboot,下面我就演示javafx+springboot操作数据库吧,学习了下面的方式,针对其他main工程也适用。
整合过程主要分三步:

1、引入springboot依赖
2、配置
3、加载fxml时注入bean

特别注意第三点,使用javafx提供的控制器工厂能轻松注入。开发模式类似MVC,只不过视图变成了fxml。

一、配置

用IDEA初始化一个springboot项目,,Maven依赖如下

   <?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>
<groupId>top.lingkang</groupId>
<artifactId>springboot-javafx-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-javafx-demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>top.lingkang.springbootjavafxdemo.SpringbootJavafxDemoApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>

配置application.properties

spring.main.web-application-type=none
spring.main.allow-bean-definition-overriding=true # 应用名称
spring.application.name=springboot-javafx-demo
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=123456

二、编写启动类

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext; import java.net.URL; @SpringBootApplication
public class SpringbootJavafxDemoApplication extends Application {
// 任何地方都可以通过这个applicationContext获取springboot的上下文
public static ConfigurableApplicationContext applicationContext;
private static String[] args; @Override
public void start(Stage primaryStage) throws Exception {
URL resource = getClass().getResource("/fxml/login.fxml");
if (resource == null) {
throw new Exception();
}
// 加载 fxml 下面的逻辑可以单独封装
FXMLLoader loader = new FXMLLoader(resource);
loader.setControllerFactory(new Callback<Class<?>, Object>() {
@Override
public Object call(Class<?> param) {
// 控制器工厂提供bean注入,此处的缺点是不能根据bean名字注入,只能通过class类型注入bean
// 解决方案:
// 1、SpringbootJavafxDemoApplication.applicationContext.getBean("Bean Name", Bean.class);
// 2、@Autowired private ApplicationContext applicationContext;
// Object bean_name = applicationContext.getBean("bean Name", Bean.class);
return applicationContext.getBean(param);
}
});
// 加载
VBox root = loader.load(); primaryStage.setScene(new Scene(root));
primaryStage.show();
} public static void main(String[] args) {
SpringbootJavafxDemoApplication.args = args;
launch(args);
} @Override
public void init() throws Exception {
// 启动springboot
applicationContext = SpringApplication.run(SpringbootJavafxDemoApplication.class, args);
} @Override
public void stop() throws Exception {
// 关闭springboot
applicationContext.stop();
}
}

/fxml/login.fxml内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="330.0"
fx:controller="top.oneit.jdownload.controller.LoginController"
prefWidth="430.0" xmlns="http://javafx.com/javafx/11" xmlns:fx="http://javafx.com/fxml/1">
<AnchorPane prefHeight="330.0" prefWidth="430.0" style="-fx-background-color: white;">
<children>
<Label layoutX="99.0" layoutY="183.0" text="账 号:">
<font>
<Font size="14.0"/>
</font>
</Label>
<Label layoutX="99.0" layoutY="217.0" text="密 码:">
<font>
<Font size="14.0"/>
</font>
</Label>
<Button fx:id="loginButton" layoutX="97.0" layoutY="270.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="236.0"
style="-fx-background-color: #1EC6FC;" text="登 录" textAlignment="CENTER" textFill="WHITE">
<font>
<Font size="14.0"/>
</font>
</Button>
<CheckBox layoutX="98.0" layoutY="243.0" mnemonicParsing="false" text="自动登录" textFill="#a6a6a6"/>
<CheckBox layoutX="188.0" layoutY="243.0" mnemonicParsing="false" text="记住密码" textFill="#a6a6a6"/>
<Label layoutX="277.0" layoutY="242.0" text="找回密码" textFill="#a6a6a6">
<font>
<Font size="14.0"/>
</font>
</Label>
<TextField layoutX="146.0" layoutY="183.0" prefHeight="23.0" prefWidth="182.0"/>
<PasswordField layoutX="146.0" layoutY="216.0" prefHeight="23.0" prefWidth="182.0"/>
</children>
</AnchorPane>
</VBox>

编写一个controller触发按钮事件

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller; import java.net.URL;
import java.util.List;
import java.util.ResourceBundle; /**
* @author lingkang
* @date 2021/11/9
*/
@Controller
public class LoginController implements Initializable {
@FXML
private Button loginButton;
@Autowired
private JdbcTemplate jdbcTemplate; @Override
public void initialize(URL url, ResourceBundle resourceBundle) {
loginButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
List res = jdbcTemplate.queryForList("select * from t_user");
if (!res.isEmpty()) {
System.out.println(res.toString());
}
System.out.println("你点击了我!");
}
});
}
}

三、效果

四、一键三连

创作不易,你觉得对你有帮助请给我点个赞!一健三连…谢谢啦!

SpringBoot整合JavaFx(十三)的更多相关文章

  1. springboot整合javafx

    原文(原码)参考地址: https://github.com/roskenet/springboot-javafx-support https://github.com/spartajet/javaf ...

  2. 很详细的SpringBoot整合UEditor教程

    很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529    版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...

  3. SpringBoot整合SpringBatch

    一.引入依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...

  4. spring-boot整合mybatis(1)

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...

  5. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  6. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  7. springboot整合mybaits注解开发

    springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...

  8. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  9. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  10. SpringBoot整合Kafka和Storm

    前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...

随机推荐

  1. 关于wake on lan远程唤醒主机的问题,长时间关机无法远程唤醒

    英特尔在年初发布了几款低功耗的CPU,国内厂商在迷你主机领域纷纷搭载新款CPU,卖的火爆.之前关注过迷你主机这块,于是,我也入手一个迷你主机玩玩,买的是板载N100的迷你主机.使用过程中会涉及到如何远 ...

  2. MySQL系列之MHA高可用——主从复制架构演变介绍、高可用MHA、管理员在高可用架构维护的职责

    文章目录 1. 主从复制架构演变介绍 1.1 基本结构 1.2 高级应用架构演变 1.2.1 高性能架构 1.2.2 高可用架构 2. 高可用MHA ***** 2.1 架构工作原理 2.2 架构介绍 ...

  3. MAC版本vmware无法识别虚拟机网卡适配器

    一.问题 莫名其妙的突然mac上的vmware无法识别网络适配器了 二.解决过程 1.重装vmware-无效 2.降级安装vmware-无效 3.安装pd虚拟机,并使用sudo命令启动-偶尔有效 4. ...

  4. AirSim 自动驾驶仿真 (6) 设置采集参数和属性

    https://cloud.tencent.com/developer/article/2011384 1.配置文件在哪 默认情况下,文件位于用户目录下的AirSim文件夹,比如在Windows下,文 ...

  5. 【随手记】python免api调用谷歌翻译

    pip3 install googletrans==4.0.0-rc1 from googletrans import Translator translator = Translator() tra ...

  6. 1. Linux 软件介绍

    重点: rpm -i -e -qi -ql -qf -qa --scripts. yum install remove info list repolist provides. 配置系统源. 搭建私有 ...

  7. JAVAweek6

    本周学习 Java语言基础:运算符[有所区别] 算术运算符 +(3+2) - * / %(取余,模运算) +(连接符)(3+''2'') ++ -- class VarDemo { //算术运算符 p ...

  8. 从物理机到K8S:应用系统部署方式的演进及其影响

    公众号「架构成长指南」,专注于生产实践.云原生.分布式系统.大数据技术分享. 概述 随着科技的进步,软件系统的部署架构也在不断演进,从以前传统的物理机到虚拟机.Docker和Kubernetes,我们 ...

  9. Google Colab 现已支持直接使用 🤗 transformers 库

    Google Colab,全称 Colaboratory,是 Google Research 团队开发的一款产品.在 Colab 中,任何人都可以通过浏览器编写和执行任意 Python 代码.它尤其适 ...

  10. C语言-变量常量数据类型

    常量:不会变化的数据.不能被修改. 1. "hello".'A'.-10.3.1415926(浮点常量) 2. #define PI 3.1415 [强调]:没有分号结束标记. [ ...