Springboot develops PC desktop applications

 1. Requirement description:

1. The desktop is required to run on window, Linux and macos systems

2. User-defined data filtering strategies, which can be imported and exported through excel

3. Select multiple excel files to generate new excel through policy filtering.

2. Technology selection and integrated environment configuration:

1. Directly use javafx for desktop development on the PC side across platforms

2. Dynamic data rules use drools and in-memory database H2

3. For excel operation, choose Alibaba’s easyexcel.

4. Basic environment: win10 professional version, idea2023.1.3, springboot3.0.8, jdk17

3. Basic environment construction

1. Download jdk17 download address: Java Downloads | Oracle

2. Download address of javafx-sdk and jmode (used when running exe programs): JavaFX - Gluon

 

3. Download and install the tool (wix311) for packaging jars into executable files for other systems: Release WiX Toolset v3.11.2 · wixtoolset/wix3 · GitHub

4. Environment variable reference:

 

4. Development project configuration:

1. Create a new springboot project

2. pom file reference

<?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.0.8</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.stats.excel</groupId>
    <artifactId>stats-excel</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>stats-excel</name>
    <description>stats-excel</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>

        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</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>
        <!-- 桌面库-->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>17</version>
        </dependency>
        <!-- JavaFX -->
<!--        <dependency>-->
<!--            <groupId>org.openjfx</groupId>-->
<!--            <artifactId>javafx-controls</artifactId>-->
<!--            <version>17</version>-->
<!--        </dependency>-->
        <!-- 表格出来工具 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!-- 数据框架-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.13.5</version>
        </dependency>
        <!-- Drools -->
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>7.61.0.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3. Application startup file reference

package com.stats.excel.statsexcel;

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.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.net.URL;

@SpringBootApplication
public class StatsExcelApplication 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) {
        StatsExcelApplication.args = args;
        launch(args);
    }

    @Override
    public void init() throws Exception {
        // 启动springboot
        applicationContext = SpringApplication.run(StatsExcelApplication.class, args);
    }

    @Override
    public void stop() throws Exception {
        // 关闭springboot
        applicationContext.stop();
    }

}

4. For other service layer, controller layer and database configuration, please refer to the project: Non-biological Linguist/DesktopAPP desktop application development·GitCode

5. Startup class configuration:

5.1. Added javafx-sdk startup parameter configuration

--module-path
"D:\java\install\openjfx-17.0.7_windows-x64_bin-sdk\javafx-sdk-17.0.7\lib"
--add-modules
javafx.controls,javafx.fxml

5.2. Import Artifacts dependencies (all files under javafx-sdk-17.0.7\bin):

 6. Command reference for converting jar to win executable exe file:

jpackage --type exe --input . --dest . --main-jar .\[你打的jar包] --main-class [你工程的包名.启动类]p --module-path "[你的javafx-jmods路径]" --add-modules javafx.controls,javafx.fxml --win-shortcut --win-menu

5. Effect display briefly

Guess you like

Origin blog.csdn.net/qq_29653373/article/details/131398449