I am learning the javafx and springboot integration and created a small program. Essentially a user can see a person’s details, click an add new button, a new fxml page loads and a user can add in a new person.
PersonEntity.java
@Entity
@Getter
@NoArgsConstructor(force = true)
@Data
@Table(name = "persons")
public class PersonEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "personID", nullable = false)
private final Integer personID;
@Column(name = "first_name")
@Setter
private String firstName;
@Column(name = "last_name")
@Setter
private String lastName;
@Column(name = "dob")
@Setter
private String dob;
PersonRepo.java
@Repository
public interface PersonRepo extends JpaRepository<PersonEntity, String> {
List<PersonEntity> findAll();
}
PersonService.java
public interface PersonService{
Optional<PersonEntity> save(PersonEntity personEntity);
PersonEntity update(PersonEntity personEntity);
PersonEntity findAll();
}
PersonServiceImpl.java
@Service
@RequiredArgsConstructor
public class PersonServiceImplimplements PersonService {
@Autowired
private PersonRepo repo;
@Override
public Optional<PersonEntity> save(PersonEntity personEntity) {
return Optional.of(repo.save(personEntity));
}
@Override
public PersonEntity update(PersonEntity personEntity){ return repo.save(personEntity);}
@Override
public PersonEntity findAll() {
return (PersonEntity ) repo.findAll();
}
}
PersonController.java
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class PersonController {
public TextField lName;
public TextField fName;
public TextField dob;
public Button AddNew
public void onAdd(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/NewPerson.fxml"));
Parent root = loader.load();
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
NewPersonController.java
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class NewPersonController{
private final PersonService personService;
public TextField lName;
public TextField fName;
public TextField dob;
public Button onSaveNewPerson;
@FXML
public void onSaveNewPerson(ActionEvent event) throws SQLException {
if(fName != null){
PersonEntity newPerson = new PersonEntity ();
fName.setText(newPerson.getFirstName());
personService.save(newPerson);
}
}
FxApplicationLauncher.java
@SpringBootApplication
@EnableJpaRepositories("com.example.repo")
public class FxApplicationLauncher {
public static void main(String[] args) {
Application.launch(FxApplication.class, args);
}
public static class FxApplication extends Application {
private ConfigurableApplicationContext context;
@Override
public void init() throws IOException {
context = new SpringApplicationBuilder()
.sources(FxApplicationLauncher.class)
.initializers((ApplicationContextInitializer<GenericApplicationContext>) applicationContext -> {
applicationContext.registerBean(Application.class, () -> this);
applicationContext.registerBean(Parameters.class, this::getParameters);
applicationContext.registerBean(HostServices.class, this::getHostServices);
})
.run(getParameters().getRaw().toArray(new String[0]));
}
@Override
public void start(Stage stage) throws IOException {
try {
this.context.publishEvent(new StageIsReadyEvent(stage));
} catch (Throwable t) {
t.printStackTrace();
}
}
@Override
public void stop() throws Exception {
context.close();
Platform.exit();
System.exit(0);
}
@Component
public static class FxApplicationStageIsReadyListener implements ApplicationListener<StageIsReadyEvent>{
@Value("${spring.application.name}")
private String applicationTitle;
private final ConfigurableApplicationContext applicationContext;
public FxApplicationStageIsReadyListener(
@Value("${spring.application.name}") String applicationTitle,
ConfigurableApplicationContext applicationContext) {
this.applicationTitle = applicationTitle;
this.applicationContext = applicationContext;
}
@Override
public void onApplicationEvent(StageIsReadyEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/PersonModule.fxml"));
fxmlLoader.setControllerFactory(applicationContext::getBean);
System.out.println(applicationContext);
Parent root = fxmlLoader.load();
Scene scene = new Scene(root, 450, 240);
Stage stage = event.getStage();
stage.setScene(scene);
stage.setTitle(this.applicationTitle);
stage.show();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public static class StageIsReadyEvent extends ApplicationEvent {
public Stage getStage() {
return (Stage) getSource();
}
public StageIsReadyEvent(Stage source) {
super(source);
}
}
}
}
The program launches and to the initial PersonModule fxml page just fine, When I click on the button to load the new fxml, NewPerson.fxml, it throws an error. However if I comment out the – private final PersonService personService within the NewPersonController file – the page will load but when trying to save the person I get a personService null.
I have tried to just @Autowire the PersonService personService annotation but that is also a personService null error. I have ensured that the applicationConext bean is configured. I inputed a system.out.println(applicationConext) and I receive a message in the stack trace of – org.springframework.context.annotation.AnnotationConfigApplicationContext@6d9e511, started on Thu Jul 25 13:51:38 EDT 2024.
I’m unsure what else could be giving me the following error. I have included the entire stacktrace.
13:51:37.812 [main] INFO net.fortuna.ical4j.util.Configurator -- ical4j.properties not found.
SLF4J(W): A number (1) of logging calls during the initialization phase have been intercepted and are
SLF4J(W): now being replayed. These are subject to the filtering rules of the underlying logging system.
SLF4J(W): See also https://www.slf4j.org/codes.html#replay
Jul 25, 2024 1:51:37 PM com.sun.javafx.application.PlatformImpl startup
WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @46793e11'
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.0)
2024-07-25T13:51:38.550-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] o.s.boot.SpringApplication : Starting application using Java 17.0.9 with PID 22496 (started by sh0184 in C:devGaitApp)
2024-07-25T13:51:38.550-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] o.s.boot.SpringApplication : No active profile set, falling back to 1 default profile: "default"
2024-07-25T13:51:38.580-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2024-07-25T13:51:38.886-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-07-25T13:51:38.895-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 3 ms. Found 0 JPA repository interfaces.
2024-07-25T13:51:38.896-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-07-25T13:51:38.898-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1 ms. Found 0 JPA repository interfaces.
2024-07-25T13:51:38.899-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-07-25T13:51:39.007-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 108 ms. Found 13 JPA repository interfaces.
2024-07-25T13:51:39.317-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2024-07-25T13:51:39.347-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.5.2.Final
2024-07-25T13:51:39.365-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] o.h.c.internal.RegionFactoryInitiator : HHH000026: Second-level cache disabled
2024-07-25T13:51:39.541-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer
2024-07-25T13:51:39.563-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2024-07-25T13:51:39.745-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection ConnectionID:1 ClientConnectionId: 6c877b03-6e5b-4682-b737-29afe33e62a1
2024-07-25T13:51:39.747-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2024-07-25T13:51:40.708-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
2024-07-25T13:51:40.882-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2024-07-25T13:51:41.312-04:00 INFO 22496 --- [GaitLabApp] [JavaFX-Launcher] o.s.boot.SpringApplication : Started application in 3.079 seconds (process running for 3.868)
org.springframework.context.annotation.AnnotationConfigApplicationContext@6d9e511, started on Thu Jul 25 13:51:38 EDT 2024
2024-07-25T13:51:41.469-04:00 WARN 22496 --- [GaitLabApp] [lication Thread] javafx : Loading FXML document with JavaFX API of version 20.0.1 by JavaFX runtime of version 19
2024-07-25T13:51:41.976-04:00 INFO 22496 --- [GaitLabApp] [lication Thread] c.e.gaitlabapp.FxApplicationLauncher : Javafx SpringBoot Application has successfully started.
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8923)
at javafx.scene.control.Button.fire(Button.java:203)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3894)
at javafx.scene.Scene.processMouseEvent(Scene.java:1887)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2620)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:842)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
at jdk.internal.reflect.GeneratedMethodAccessor42.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
... 50 more
Caused by: javafx.fxml.LoadException:
/C:/dev/GaitApp/target/classes/NewPersonModule.fxml:13
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2714)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:944)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:981)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:230)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:755)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2845)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2641)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2555)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2523)
at com.example.controllers.PersonController.onAdd(PersonController.java:872)
... 61 more
Caused by: java.lang.NoSuchMethodException: com.example.controllers.NewPersonController.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3585)
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2754)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:939)
... 69 more
2024-07-25T13:51:48.598-04:00 INFO 22496 --- [GaitLabApp] [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2024-07-25T13:51:48.616-04:00 INFO 22496 --- [GaitLabApp] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2024-07-25T13:51:48.622-04:00 INFO 22496 --- [GaitLabApp] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
Process finished with exit code 130