Class FxWeaver


  • public class FxWeaver
    extends Object
    FxWeaver is the core weaving facility, enabling Controllers and Views to be instantiated by a dependency injection framework such as (but not limited to) Spring.

    The following example requires a Spring ConfigurableApplicationContext to be instantiated. If now MainController is declared as Spring managed bean, it will get created and injected by Spring.

    If a managed Controller class contains an FxmlView annotation, attached FXML views are injected as well.

         ConfigurableApplicationContext applicationContext = ...
         FxWeaver fxWeaver = new FxWeaver(applicationContext::getBean, applicationContext::close);
         Scene scene = new Scene(fxWeaver.loadView(MainController.class), 400, 300);
         ...
         @FxmlView
         public class MainController {
            ...
         }
     
    Author:
    Rene Gielen
    See Also:
    FxmlView
    • Constructor Detail

      • FxWeaver

        public FxWeaver​(javafx.util.Callback<Class<?>,​Object> beanFactory,
                        Runnable closeCommand)
        Create a FxWeaver instance.

        Example:

             ConfigurableApplicationContext applicationContext = ...
             FxWeaver fxWeaver = new FxWeaver(applicationContext::getBean, applicationContext::close);
         
        Parameters:
        beanFactory - The beanFactory callback to be called for requesting a bean of given class when e.g. loadView(Class) is called.
        closeCommand - The function to close a bean factory attached to FxWeaver
        See Also:
        loadView(Class), loadView(Class, ResourceBundle)
    • Method Detail

      • loadView

        public <C,​V extends javafx.scene.Node> V loadView​(Class<C> controllerClass)
        Load FXML-defined view instance, weaved with its controller declared in fx:controller as a bean produced by the bean factory provided in FxWeaver(Callback, Runnable).

        The possible FXML resource is inferred from a FxmlView annotation at the controller class or the simple classname and package of said class if it was not annotated like this. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If you are interested in the controller instance, you might instead use the loadController methods, e.g. loadController(Class)

        Type Parameters:
        C - The controller type
        V - The view type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        Returns:
        An instance of the requested view, weaved with its managed controller as defined in FXMLLoader.getController().
        See Also:
        loadController(Class), loadController(Class, ResourceBundle), loadController(Class, String), loadController(Class, String, ResourceBundle), FxWeaver(Callback, Runnable), FXMLLoader
      • loadView

        public <C,​V extends javafx.scene.Node> V loadView​(Class<C> controllerClass,
                                                                ResourceBundle resourceBundle)
        Load FXML-defined view instance, weaved with its controller declared in fx:controller as a bean produced by the bean factory provided in FxWeaver(Callback, Runnable).

        The possible FXML resource is inferred from a FxmlView annotation at the controller class or the simple classname and package of said class if it was not annotated like this. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If you are interested in the controller instance, you might instead use the loadController methods, e.g. loadController(Class)

        Type Parameters:
        C - The controller type
        V - The view type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        resourceBundle - The optional ResourceBundle to use for view creation. May be null
        Returns:
        An instance of the requested view, weaved with its managed controller as defined in FXMLLoader.getController().
        See Also:
        loadController(Class), loadController(Class, ResourceBundle), loadController(Class, String), loadController(Class, String, ResourceBundle), FxWeaver(Callback, Runnable), FXMLLoader
      • loadView

        public <C,​V extends javafx.scene.Node> V loadView​(Class<C> controllerClass,
                                                                String location)
        Load FXML-defined view instance, weaved with its controller declared in fx:controller as a bean produced by the bean factory provided in FxWeaver(Callback, Runnable).

        The possible FXML resource may be given as a location in the classpath. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If you are interested in the controller instance, you might instead use the loadController methods, e.g. loadController(Class)

        Type Parameters:
        C - The controller type
        V - The view type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        location - The location of the FXML view to load as a classloader resource.
        Returns:
        An instance of the requested view, weaved with its managed controller as defined in FXMLLoader.getController().
        See Also:
        load(Class), load(Class, ResourceBundle), loadController(Class), loadController(Class, ResourceBundle), FxWeaver(Callback, Runnable), FXMLLoader
      • loadView

        public <C,​V extends javafx.scene.Node> V loadView​(Class<C> controllerClass,
                                                                String location,
                                                                ResourceBundle resourceBundle)
        Load FXML-defined view instance, weaved with its controller declared in fx:controller as a bean produced by the bean factory provided in FxWeaver(Callback, Runnable).

        The possible FXML resource may be given as a location in the classpath. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If you are interested in the controller instance, you might instead use the loadController methods, e.g. loadController(Class)

        Type Parameters:
        C - The controller type
        V - The view type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        location - The location of the FXML view to load as a classloader resource.
        resourceBundle - The optional ResourceBundle to use for view creation. May be null
        Returns:
        An instance of the requested view, weaved with its managed controller as defined in FXMLLoader.getController().
        See Also:
        loadController(Class), loadController(Class, ResourceBundle), FxWeaver(Callback, Runnable), FXMLLoader
      • loadController

        public <C> C loadController​(Class<C> controllerClass,
                                    String location)
        Load controller instance, potentially weaved with a FXML view declaring the given class as fx:controller.

        The possible FXML resource may be given as a location in the classpath. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If the controller class does not come with a resolvable FXML view resource, the controller will be instantiated by the given bean factory directly.

        Type Parameters:
        C - The controller type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        location - The location of the FXML view to load as a classloader resource. May be null or not resolvable, in which case the controller will be directly instantiated by the given bean factory.
        Returns:
        A managed instance of the requested controller, potentially weaved with its view
        See Also:
        FxWeaver(Callback, Runnable), FXMLLoader
      • loadController

        public <C> C loadController​(Class<C> controllerClass,
                                    String location,
                                    ResourceBundle resourceBundle)
        Load controller instance, potentially weaved with a FXML view declaring the given class as fx:controller.

        The possible FXML resource may be given as a location in the classpath. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If the controller class does not come with a resolvable FXML view resource, the controller will be instantiated by the given bean factory directly.

        Type Parameters:
        C - The controller type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        location - The location of the FXML view to load as a classloader resource. May be null or not resolvable, in which case the controller will be directly instantiated by the given bean factory.
        resourceBundle - The optional ResourceBundle to use for view creation. May be null
        Returns:
        A managed instance of the requested controller, potentially weaved with its view
        See Also:
        FxWeaver(Callback, Runnable), FXMLLoader
      • loadController

        public <C> C loadController​(Class<C> controllerClass)
        Load controller instance, potentially weaved with a FXML view declaring the given class as fx:controller.

        The possible FXML resource is inferred from a FxmlView annotation at the controller class or the simple classname and package of said class if it was not annotated like this. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If the controller class does not come with a resolvable FXML view resource, the controller will be instantiated by the given bean factory directly.

        Type Parameters:
        C - The controller type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        Returns:
        A managed instance of the requested controller, potentially weaved with its view
        See Also:
        FxWeaver(Callback, Runnable), FXMLLoader
      • loadController

        public <C> C loadController​(Class<C> controllerClass,
                                    ResourceBundle resourceBundle)
        Load controller instance, potentially weaved with a FXML view declaring the given class as fx:controller.

        The possible FXML resource is inferred from a FxmlView annotation at the controller class or the simple classname and package of said class if it was not annotated like this. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If the controller class does not come with a resolvable FXML view resource, the controller will be instantiated by the given bean factory directly.

        Type Parameters:
        C - The controller type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        resourceBundle - The optional ResourceBundle to use for view creation. May be null
        Returns:
        A managed instance of the requested controller, potentially weaved with its view
        See Also:
        FxWeaver(Callback, Runnable), FXMLLoader
      • getBean

        public <C> C getBean​(Class<C> beanType)
        Get managed bean instance from bean factory provided in FxWeaver(Callback, Runnable).
        Type Parameters:
        C - The bean type.
        Parameters:
        beanType - The type of the bean to be instantiated.
        Returns:
        The bean as defined in and returned from the bean factory
      • load

        public <C,​V extends javafx.scene.Node> FxControllerAndView<C,​V> load​(Class<C> controllerClass)
        Load controller instance, potentially weaved with a FXML view declaring the given class as fx:controller.

        The possible FXML resource is inferred from a FxmlView annotation at the controller class or the simple classname and package of said class if it was not annotated like this. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If the controller class does not come with a resolvable FXML view resource, the controller will be instantiated by the given bean factory directly.

        Type Parameters:
        V - The view type
        C - The controller type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        Returns:
        A SimpleFxControllerAndView container with the managed instance of the requested controller and the corresponding view, if applicable
        See Also:
        FxWeaver(Callback, Runnable), FXMLLoader
      • load

        public <C,​V extends javafx.scene.Node> FxControllerAndView<C,​V> load​(Class<C> controllerClass,
                                                                                         ResourceBundle resourceBundle)
        Load controller instance, potentially weaved with a FXML view declaring the given class as fx:controller.

        The possible FXML resource is inferred from a FxmlView annotation at the controller class or the simple classname and package of said class if it was not annotated like this. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If the controller class does not come with a resolvable FXML view resource, the controller will be instantiated by the given bean factory directly.

        Type Parameters:
        V - The view type
        C - The controller type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        resourceBundle - The optional ResourceBundle to use for view creation. May be null
        Returns:
        A SimpleFxControllerAndView container with the managed instance of the requested controller and the corresponding view, if applicable
        See Also:
        FxWeaver(Callback, Runnable), FXMLLoader
      • load

        protected <C,​V extends javafx.scene.Node> FxControllerAndView<C,​V> load​(Class<C> controllerClass,
                                                                                            String location,
                                                                                            ResourceBundle resourceBundle)
        Load controller instance, potentially weaved with a FXML view declaring the given class as fx:controller.

        The possible FXML resource is inferred from a FxmlView annotation at the controller class or the simple classname and package of said class if it was not annotated like this. If the FXML file is resolvable, the defined view within will be loaded by FXMLLoader. The controller will then be instantiated based on the fx:controller attribute, using the bean factory from FxWeaver(Callback, Runnable). If the bean factory is based on a dependency management framework such as Spring, Guice or CDI, this means that the instance will be fully managed and injected as declared.

        If the controller class does not come with a resolvable FXML view resource, the controller will be instantiated by the given bean factory directly.

        Type Parameters:
        V - The view type
        C - The controller type
        Parameters:
        controllerClass - The controller class of which a weaved instance should be provided
        location - The location of the FXML view to load as a classloader resource. May be null or not resolvable, in which case the controller will be directly instantiated by the given bean factory.
        resourceBundle - The optional ResourceBundle to use for view creation. May be null
        Returns:
        A SimpleFxControllerAndView container with the managed instance of the requested controller and the corresponding view, if applicable
        See Also:
        FxWeaver(Callback, Runnable), FXMLLoader
      • buildFxmlReference

        protected String buildFxmlReference​(Class<?> c)
        Build a FXML view location reference for controller classes, based on FxmlView annotation or simple classname.
        Parameters:
        c - The class to build a FXML location for. If it does not contain a FxmlView annotation to specify resource to load, it is assumed that the view resides in the same package, named {c.getSimpleName()}.fxml
        Returns:
        a resource location suitable for loading by Class.getResource(String)
      • shutdown

        public void shutdown()
        Perform the provided close method and call Platform.exit().