java - Spring Boot + JavaFx Autowiring 不起作用

标签 java spring spring-boot javafx

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

因此,我创建了一个项目,当我运行应用程序时,将创建 spring 上下文并运行 JavaFx 应用程序。但问题是当我尝试创建一些 bean 时,例如使用 @Repository 注释。当我 Autowiring 时,值为空。

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> {

}

User.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 不起作用

最佳答案

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

@Component
 public class Anotherclass{

     @Autowired
     private UsersRepository usersRepository;
}

关于java - Spring Boot + JavaFx Autowiring 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58006602/

相关文章:

java - 如何在不使用 system.exit() 的情况下,在输入 "quit"时使用一段时间不断要求用户输入并退出程序?

java - 将输入类型 ="image"提交给另一个 servlet 未能返回任何参数

Java 7 - JPanel PaintComponent 标题栏重叠

java - 找到 Spring 资源但未加载任何 bean

java - 使用 MappedSuperClass 对相关表进行 Hibernate 过滤器

java - Axon amqp 消息传递,未触发事件处理程序

java - 捕获而不尝试错误

java - 在 JpaRepository 中,如果一个参数对于许多查询方法来说是通用的,那么如何在 Repository 和 Service 中尽可能地做到最好

spring - 使用 OAuth2 资源所有者密码授权类型在 Spring Cloud Gateway 中创建路由

java - Spring 启动 : Optional parameter query in Query method