After reading some articles about the CDI and Java FX integration, and the source codes of javafx-weaver spring integration.
I decided to add CDI integration to Java FX via the existing javafx-weaver work to simplify the integration work.
The source code can be found here.
I added a simple producer to expose FxWeaver
to the CDI context.
@ApplicationScoped
public class FxWeaverProducer {
private static final Logger LOGGER = Logger.getLogger(FxWeaverProducer.class.getName());
@Produces
FxWeaver fxWeaver(CDIControllerFactory callback) {
var fxWeaver = new FxWeaver((Callback<Class<?>, Object>) callback,
() -> LOGGER.log(Level.INFO, "calling FxWeaver shutdown hook")
);
return fxWeaver;
}
public void destroyFxWeaver(@Disposes FxWeaver fxWeaver) {
LOGGER.log(Level.INFO, "destroying FxWeaver bean...");
fxWeaver.shutdown();
}
}
The problem is when using fxWeaver.loadView
to load view, the controller did not work as expected.
@ApplicationScoped
@FxmlView("HandlingReport.fxml")
public class HandlingReportController {
private final static Logger LOGGER = Logger.getLogger(HandlingReportController.class.getName());
@Inject
private HandlingReportService handlingReportService;
//fxml properties...
@FXML
private void onSubmit(){...}
}
As above codes, the dependent service handlingReportService
in the controller class is null(not injected) when performing an action like onSubmit
, it seems when JavaFX handles the @FXML
properties binding it always used java reflection API.
If I change the method to public void onSubmit()
( use public
modifier), all FX fields become null when pressing the onSubmit
button.
Any idea to fix this issue?
Marked the controller class as @Dependent
scope to resolve the problem.
@Dependent
public class HandlingReportController { ...}