亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Boot + JavaFx 自動裝配不起作用

Spring Boot + JavaFx 自動裝配不起作用

九州編程 2023-08-09 16:24:02
昨天我嘗試創建連接 Spring Boot 和 JavaFX 的項目。因此,我創建了一個項目,當我運行應用程序時,將創建 spring 上下文并運行 JavaFx 應用程序。但問題是當我嘗試創建一些bean時,例如使用@Repository注釋。當我自動裝配時,值為空。CarGarageApplication.javapackage com.car.garage;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.ComponentScan;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import com.car.garage.dao.UsersRepository;import javafx.application.Application;import javafx.fxml.FXMLLoader;import javafx.scene.Parent;import javafx.scene.Scene;import javafx.stage.Stage;@SpringBootApplication@ComponentScan@EnableJpaRepositories("com.car.garage.dao")public class CarGarageApplication extends Application {? ? private ConfigurableApplicationContext mainContext;? ? private Parent rootNode;? ? @Autowired? ? UsersRepository usersRepository;? ? public static void main(String[] args) {? ? ? ? Application.launch(args);? ? }? ? @Override? ? public void init() throws Exception {? ? ? ? mainContext = SpringApplication.run(CarGarageApplication.class);? ? ? ? FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/WelcomePage.fxml"));? ? ? ? loader.setControllerFactory(mainContext::getBean);? ? ? ? rootNode = loader.load();? ? }? ? @Override? ? public void start(Stage primaryStage) throws Exception {? ? ? ? primaryStage.setScene(new Scene(rootNode));? ? ? ? primaryStage.setResizable(false);? ? ? ? primaryStage.show();? ? ? ? System.out.println(usersRepository);? ? }? ? @Override? ? public void stop() throws Exception {? ? ? ? mainContext.close();? ? }}
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

我們應該遵循以下方法來自動裝配存儲庫類。因為從自動裝配另一個類的地方必須有 SPRING STEREOTYPES 類(@Component,@Service 這樣的)


@Component

 public class Anotherclass{


     @Autowired

     private UsersRepository usersRepository;

}


查看完整回答
反對 回復 2023-08-09
  • 1 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號