昨天我嘗試創建連接 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;
}
添加回答
舉報
0/150
提交
取消