mercredi 3 juillet 2019

Binding together two objects automatically

I am following MVC pattern for my Java application. So I have one class (normally extends any layouts like Gridpane, HBox etc) for View and another for Controller.

Some Background: Since application I am building will be running on older machine, running time is very crucial. During profiling I found that FXML parsing is taking longer time, So I switched to classes.

Now back to the problem: Since I am using classes for Views, I want some automatic mechanism that will bind my view class with controller (just like FXML loader does).

So I was thinking like having some annotation like


@Controller(MainController.class)
public class MainView extends GridPane {
    private Button btn = new Button();
    ...

}

And controller

public class MainController {
    @FXML
    private Button btn;    // <--- This will be injected automatically from View class
}

So my question here,

How can I intercept creation of MainView class object so that I can instantiate controller object for the same and inject field using reflection?

P.S: As i mentioned, application is going to be run on legacy systems, so I do not want to use any third party frameworks (though I can go for some lightweight libraries)





Aucun commentaire:

Enregistrer un commentaire