我有問題。這是我的主要內容:
@SpringBootApplication
public class MyAppSpringApplication extends Application {
public static ConfigurableApplicationContext springContext;
private FXMLLoader fxmlLoader;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
fxmlLoader.setLocation(getClass().getResource("/sample.fxml"));
Parent root = fxmlLoader.load();
stage.setTitle("Sample app");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@Override
public void stop() throws Exception {
springContext.stop();
}
@Override
public void init() throws Exception {
springContext = SpringApplication.run(MyAppSpringApplication.class);
fxmlLoader = new FXMLLoader();
fxmlLoader.setControllerFactory(springContext::getBean);
}
}
我的第一個帶有 samplecontroller 和 sampleservice 的視窗(sample.fxml)作業正常。但是我dish-builder.fxml用他們的控制器和服務創建了另一個,但是當我嘗試在那里使用我的服務時,它不起作用,因為 disbuilderservice 中的 null (albo 在那個新控制器中不起作用 sampleservice)。我聽說我也應該使用它:
public static ConfigurableApplicationContext springContext;
但我不知道我應該如何使用它。對不起我的知識和英語薄弱。
@Controller
public class DishBuilderController implements Initializable {
@Autowired
DishBuilderService dishBuilderService;
@Autowired
SampleService sampleService;
private void somefun(){
sampleService.somefunInService(); //here sampleService and
every other service has null.
}
這是我打開新的 discBuilder 視窗(在 SampleController 中)的時刻:
@FXML
void addNoweOknoClicked(ActionEvent event) {
try {
Stage stage = (Stage)anchorPane.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("/dish-builder.fxml"));
AnchorPane root = fxmlLoader.load();
stage.setTitle("Sample app");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}catch (IOException e){
e.printStackTrace();
}
}
uj5u.com熱心網友回復:
加載時,dish-builder.fxml您沒有在FXMLLoader. 這意味著FXMLLoader它只是通過呼叫其無引數建構式來創建控制器。由于控制器不是 spring 管理的 bean,所以 spring 不能向其中注入任何組件。
您需要設定控制器工廠,就像您在加載時所做sample.fxml的那樣,以便FXMLLoader讓 Spring 從應用程式背景關系中檢索控制器。
與您的問題不嚴格相關的幾點:
- 無需將 公開
ApplicationContext為公共靜態欄位。您可以將其注入任何需要訪問它的 spring 管理的 bean - 不建議重復使用
FXMLLoaders.FXMLLoader因此,創建實體變數是沒有意義的。 - 該
@Controller注釋適用于 Spring MVC 應用程式中的Web控制器。這些與 JavaFX 意義上的控制器完全不同。您應該為 JavaFX 控制器使用通用@Component注解。 - 如果您要重新加載 FXML 檔案,您將需要一個新的控制器實體。這意味著如果控制器由 Spring 管理,它需要具有
PROTOTYPE范圍,而不是默認SINGLETON范圍。
所以你需要:
@SpringBootApplication
public class MyAppSpringApplication extends Application {
private ConfigurableApplicationContext springContext;
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setControllerFactory(springContext::getBean);
fxmlLoader.setLocation(getClass().getResource("/sample.fxml"));
Parent root = fxmlLoader.load();
stage.setTitle("Sample app");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@Override
public void stop() throws Exception {
springContext.stop();
}
@Override
public void init() throws Exception {
springContext = SpringApplication.run(MyAppSpringApplication.class);
}
}
那么你SampleController應該看起來像
@Component
@Scope("prototype")
public class SampleController {
@Autowired
private ConfigurableApplicationContext springContext ;
@FXML
void addNoweOknoClicked(ActionEvent event) {
try {
Stage stage = (Stage)anchorPane.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setControllerFactory(springContext::getBean);
fxmlLoader.setLocation(getClass().getResource("/dish-builder.fxml"));
AnchorPane root = fxmlLoader.load();
stage.setTitle("Sample app");
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}catch (IOException e){
e.printStackTrace();
}
}
}
同樣地
@Component
@Scope("prototype")
public class DishBuilderController implements Initializable {
@Autowired
DishBuilderService dishBuilderService;
@Autowired
SampleService sampleService;
private void somefun(){
// this should now work, since the controller is managed by Spring:
sampleService.somefunInService();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/486341.html
上一篇:如何使用鏈接文本/標題撰寫/列印href鏈接-javascript
下一篇:如何將Mono<ResponseEntity<Object>>轉換為Mono<ResponseEntity<Void>>
