I'm making a Spring Boot + JavaFX application for a college project. Since we're studying the BCE (Boundary/Control/Entitity) Pattern from the Unified Process, we have to implement a use case with that in mind. I'm programming the use case in Java, using JavaFX as UI and Spring Boot as a framework. In the JavaFX fxml file, when you define the controller class attribute, I think that it should be the boundary class, rather than the controller itself because the first method of the app goes to the boundary object. The problem is that later I don't know how to link the boundary class to the controller in the Spring Context because if I define a Controller controller attribute in the boundary class, the program won't compile. This is my Spring/JavaFX application class:
@SpringBootApplication
public class SpringApp extends Application {
public static ConfigurableApplicationContext applicationContext;
public static Parent root;
public static Stage stage;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
applicationContext = SpringApplication.run(SpringApp.class);
FXMLLoader loader = new FXMLLoader(SpringApp.class.getResource("/app.fxml"));
loader.setControllerFactory(applicationContext::getBean);
Scene scene = new Scene(loader.load(), 640, 360, false, SceneAntialiasing.BALANCED);
primaryStage.setScene(scene);
primaryStage.show();
}
}
And this is a portion of the boundary class (defined as a controller in the fxml file):
public class SaveNewClientBoundary implements Initializable {
@FXML
public ComboBox<Client> cboClients;
@Autowired
private ClientRepo clientRepo;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
saveNewClientOption();
}
public void saveNewClientOption(){
showClients();
}
public void showClients() {
cboClients.setItems(FXCollections.observableArrayList(clientRepo.findAll()));
}
Now, this works and all, but it's not using the pattern properly (because of the findAll()
method). What I'd like to do is to delegate the communication with entity objects to a controller, so a controller would look like this:
@Component
public class SaveNewClientController {
private List<Client> clients;
@Autowired
private ClientRepo clientRepo;
public void saveNewClientOption() {
this.searchAllClients();
}
public void searchAllClients() {
this.clients = clientRepo.findAll();
}
public List<Client> getClients() {
return clients;
}
If I do this, however, I don't get how to let the boundary object access the clients that the controller looks for. Basically, I don't understand how to instantiate the controller and link it to the Boundary class (and link the boundary class to the controller) once the program starts.
I ended up fixing it by implementing a circular dependency between the boundary class and the controller: Setter/Field Injection
Before that, I was trying to create an instance of the controller in the Boundary class initialize()
method, but it was throwing a Controller NullPointer exception because that's not how Spring Boot works. Thanks for the other replies.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.