Integrating Spring with FXML nested controllers

Adam Granger adam at
Fri Feb 27 06:28:16 UTC 2015

I'd be interested in the groups opinions...


I'm implementing a large application using JavaFX but unsure how to deal
with nested controllers and Spring.

 * The FXML has already been provided by the design team and has up to 3
levels of nested FXML using the include mechanism.

 * Models will be defined in Spring
 * I need to inject the models into all the nested controllers
 * There are multiple instances of controllers of the same type. i.e.
parts of the screen are identical but powered by different instances of
the same model type.

My work so far

 * I've read [Stephen Chin's blog - JavaFX in Spring Day 2 – Configuration
and FXML][1] and other SO questions however these only deals with top
level controllers.

 * I experimented with FXMLLoader.setControllerFactory() mechanism and
defining the controllers in the application context, however this only
gives the class of the controller to create, which means there is no way
to differentiate the two controllers of the same type but with different

Problem with using controller factory:

    loader.setControllerFactory(new Callback<Class<?>, Object>() {
        public Object call(Class<?> param) {
            // OK but doesn't work when multiple instances controller of
same type
            return context.getBean(param);

 * My best approach so far is for the top level controller to be wired up
using Stephen Chin approach.
   * For the case with multiple bean instances the parent controller will
get references to the specific beans via @Autowire/@Qualifer and then
set on the corresponding controller.
  * The next level of controllers can be wired up too by exposing them on
the top level controller and calling autowire()

Specific questions
 * Is there anyway of using controller factory mechanism so I can define
the controllers in the spring context instead so that it is easier to
wire them up ?
 * Is there some other method I can use?


Spring context

	<context:annotation-config />
	<bean id="modelA" class="org.example.Model">
		<property name="value" value="a value" />
	<bean id="modelB" class="org.example.Model">
		<property name="value" value="b value" />

Top level

    <HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" xmlns=""
          <TabPane tabClosingPolicy="UNAVAILABLE">
              <Tab text="A">
                      <fx:include fx:id="a" source="nested.fxml" />
              <Tab text="B">
                      <fx:include fx:id="b" source="nested.fxml" />

Nested level

    <HBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0"
prefWidth="200.0" xmlns=""
          <TextField fx:id="value" />

Application main

    public class NestedControllersSpring extends Application {
        public static void main(String[] args) {
        public void start(Stage stage) throws Exception {

            ClassPathXmlApplicationContext context =
                    new ClassPathXmlApplicationContext("/app-context.xml");

            FXMLLoader loader = new
            stage.setScene(new Scene(loader.load()));
            TopLevelController top = loader.getController();
            top.init(); // needed because autowire doesn't call

Top level controller

    public class TopLevelController {

        private NestedController aController;

        private NestedController bController;

        private Model a;

        private Model b;

        public void init() {
        public NestedController getAController() {
            return aController;
        public NestedController getBController() {
            return bController;


More information about the openjfx-dev mailing list