javafx 整合springboot 官网pom以来
时间: 2023-07-08 14:43:13 浏览: 164
你可以按照下面的步骤整合JavaFX和Spring Boot:
1. 在pom.xml中添加JavaFX依赖项:
```xml
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
</dependencies>
```
2. 添加JavaFX插件:
```xml
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
```
3. 在Application类中添加JavaFX启动代码:
```java
@SpringBootApplication
public class Application extends Application {
private ConfigurableApplicationContext context;
@Override
public void init() {
String[] args = getParameters().getRaw().toArray(new String[0]);
this.context = new SpringApplicationBuilder()
.sources(Application.class)
.run(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/main.fxml"));
fxmlLoader.setControllerFactory(context::getBean);
Parent root = fxmlLoader.load();
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void stop() {
context.close();
Platform.exit();
}
public static void main(String[] args) {
launch(args);
}
}
```
其中,`/fxml/main.fxml`是你的FXML文件路径,`context::getBean`是Spring Boot自动注入bean的方式。
希望这些步骤能够帮助到你。
阅读全文