I need to launch application with fxgl animations with springboot to utilize it's functionality.
I dont really know how to do it.
I tried putting initialization into different init() methods of the inheritor of the GameApplication.
I tried it like this:
@Override
protected void onPreInit() {
context = SpringApplication.run(getClass(), savedArgs);
context.getAutowireCapableBeanFactory().autowireBean(this);
super.onPreInit();
}
Or tried to mimic web version:
public class App extends Application {
private ConfigurableApplicationContext applicationContext;
@Override
public void init() {
String[] args = getParameters().getRaw().toArray(new String[0]);
this.applicationContext = new SpringApplicationBuilder()
.sources(DiplomaBaseApplication.class)
.run(args);
}
@Override
public void stop() {
this.applicationContext.close();
Platform.exit();
}
@Override
public void start(Stage stage) {
GameApplication gameApplication = new SimulationApplication();
GameApplication.embeddedLaunch(gameApplication);
}
}
@SpringBootApplication
public class DiplomaBaseApplication {
public static void main(String[] args) {
Application.launch(App.class, args);
}
}
Using javafx-weaver-spring-boot-starter.
The application started, but spring initialization ended to early end spring features and beans doesn't work:
22:08:38.879 [JavaFX Application Thread] INFO Engine - FXGL-11.17 (16.07.2021 15.46) on WINDOWS (J:11.0.10 FX:16)
22:08:38.879 [JavaFX Application Thread] INFO Engine - Source code and latest versions at: https://github.com/AlmasB/FXGL
22:08:38.880 [JavaFX Application Thread] INFO Engine - Join the FXGL chat at: https://gitter.im/AlmasB/FXGL
22:08:39.418 [FXGL Background Thread 1 ] INFO FXGLApplication - FXGL initialization took: 0,325 sec
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.7)
2022-05-13 22:08:39.900 INFO 31948 --- [lication Thread] o.s.boot.SpringApplication : Starting application using Java 11.0.10 on DESKTOP-2DKK20I with PID 31948 (started by pro56 in C:\Users\pro56\Desktop\Course)
2022-05-13 22:08:39.903 INFO 31948 --- [lication Thread] o.s.boot.SpringApplication : No active profile set, falling back to 1 default profile: "default"
2022-05-13 22:08:39.985 INFO 31948 --- [lication Thread] o.s.boot.SpringApplication : Started application in 0.425 seconds (JVM running for 1.813)
22:08:40.056 [FXGL Background Thread 1 ] INFO FXGLApplication - Game initialization took: 0,024 sec
22:08:40.852 [FXGL Background Thread 2 ] INFO UpdaterService - Your current version: 11.17
22:08:40.852 [FXGL Background Thread 2 ] INFO UpdaterService - Latest stable version: 17.1
I'm not exactly sure why this did not work for you. I tried it and it worked fine for me.
This warning is generated:
I guess that is just how fxgl works, it runs JavaFX off the classpath in an unsupported configuration by default. I see from the FXGL documentation that it can be used as a module, so I guess in that configuration it will run a supported configuration using JavaFX modules rather than off the classpath. However, SpringBoot isn't currently coded to use the modulepath (that will happen with SpringBoot 3, I believe). So, for now, it is probably best to run everything in the unsupported configuration as demonstrated by this example.
Although in the example I placed a main method in the DemoSpringApplication for testing, the actual main class to run is not that one, it is instead the BasicSpringGameApp which extends the FXGL GameApplication.
The BasicSpringGameApp will call the SpringApplication static method to run the Spring application (which will create an instance of the Spring application).
The Spring application is separate from the FXGL GameApplication (which is internally separate from the JavaFX Application), so all of those things have different instances (just one of each), applying a separation of concerns.
src/main/java/com/example/glboot/BasicSpringGameApp.java
Extends the FXGL GameApplication. The FXGL GameApplication will (internally) launch a JavaFX application. This example will also run a spring application so that spring services will be available. The spring context is autowired into the GameApplication class so that spring services are available for use within the GameApplication class. The use of a Spring inject service is demonstrated by making calls to the UserService, which is a Spring service.
src/main/resources/DemoSpringApplication.java
SpringBoot application, configures and starts up a spring services via the SpringBoot framework.
src/main/java/com/example/glboot/UserService.java
An example user information spring service with some injected values from the spring context and application configuration properties.
src/main/resoruces/application.properties
Spring configuration properties.
pom.xml
Maven project file.
console output