Spring Boot + JavaFx 自动装配不起作用

问题描述 投票:0回答:2

昨天我尝试创建连接 Spring Boot 和 JavaFX 的项目。我基于 http://www.greggbolinger.com/let-spring-be-your-javafx-controller-factory/

所以我创建了一个项目,当我运行应用程序时,会创建 spring 上下文并运行 JavaFx 应用程序。但问题是当我尝试创建一些 bean 时,例如使用 @Repository 注释。当我自动装配时,值为空。

CarGarageApplication.java

package 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();
    }
}

UsersRepository.java

package com.car.garage.dao;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.car.garage.model.User;

@Repository
public interface UsersRepository extends CrudRepository<User, Long> {

}

用户.java

package com.car.garage.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {

    @Id
    @GeneratedValue
    private Long id;

    private String username;
    private String password;

    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

编辑:

mainContext.getBean(UsersRepository.class)
返回UsersRepository的正确bean实例,但为什么@Autowired不起作用

java spring spring-boot javafx
2个回答
1
投票

我们应该遵循以下方法来自动装配存储库类。因为从自动装配另一个类的地方必须有 SPRING STEREOTYPES 类(@Component、@Service 之类的)

@Component
 public class Anotherclass{

     @Autowired
     private UsersRepository usersRepository;
}

0
投票

补充:这里有两个现成的模板,包含 JavaFX、Spring Boot 和 Maven/Gradle,包括示例代码。

我将感谢 GitHub 上的明星支持它的免费开源,从而帮助您解决问题。 <3

https://github.com/davidweber411/javafx-JavaFxSpringBootGradleApp

https://github.com/davidweber411/javafx-JavaFxSpringBootMavenApp

© www.soinside.com 2019 - 2024. All rights reserved.