昨天我嘗試創(chuàng)建連接 Spring Boot 和 JavaFX 的項(xiàng)目。因此,我創(chuàng)建了一個項(xiàng)目,當(dāng)我運(yùn)行應(yīng)用程序時(shí),將創(chuàng)建 spring 上下文并運(yùn)行 JavaFx 應(yīng)用程序。但問題是當(dāng)我嘗試創(chuàng)建一些bean時(shí),例如使用@Repository注釋。當(dāng)我自動裝配時(shí),值為空。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貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個贊
我們應(yīng)該遵循以下方法來自動裝配存儲庫類。因?yàn)閺淖詣友b配另一個類的地方必須有 SPRING STEREOTYPES 類(@Component,@Service 這樣的)
@Component
public class Anotherclass{
@Autowired
private UsersRepository usersRepository;
}
添加回答
舉報(bào)
0/150
提交
取消