栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合JavaFx(十三)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合JavaFx(十三)

SpringBoot整合JavaFx(十三)

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

1、引入springboot依赖
2、配置
3、获取bean

特别注意第三点,首先知道javafx的内部是static,无runtime,所以在springboot+javafx中获取容器Bean属于静态获取。

一、配置

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

    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
            org.springframework.boot
            spring-boot-starter-data-jdbc
            2.3.7.RELEASE
        

        
        
            mysql
            mysql-connector-java
            runtime
        
    

配置application.properties

# 应用名称
spring.application.name=jdownload

spring.main.web-application-type=none
spring.main.allow-bean-definition-overriding=true

# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库连接地址
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.image.Image;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.net.URL;

@SpringBootApplication
public class JdownloadApplication extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.getIcons().add(new Image("assets/茶壶.png"));
        URL resource = getClass().getResource("/fxml/login.fxml");
        if (resource == null) {
            throw new Exception();
        }
        VBox root = FXMLLoader.load(resource);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    public static void main(String[] args) {
        SpringApplication.run(JdownloadApplication.class, args);
        launch(args);
    }

}

/fxml/login.fxml内容如下:








    
        
            
            
            
            
            
            
            
            
        
    

编写一个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.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

@Component
public class LoginController implements Initializable, ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (applicationContext != null) {
            this.applicationContext = applicationContext;
        }
    }

    @FXML
    private Button loginButton;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        loginButton.setOnAction(new EventHandler() {
            @Override
            public void handle(ActionEvent actionEvent) {
                // 此处处于javafx线程的static方法,顾使用 applicationContext 静态获取bean,,当然,你也可以用你喜欢的方式封装获取bean工具类
                JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
                List res = jdbcTemplate.queryForList("select * from t_user");
                if (!res.isEmpty()) {
                    System.out.println(res.toString());
                }
                System.out.println("你点击了我!");
            }
        });
    }
}
三、效果


工程截图

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号