本文介绍了Spring Boot JPA与JavaFX Service集成为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将spring boot jpa与javafx应用程序集成在一起.集成成功,并且FX屏幕加载了场景,问题在于该服务在@Autowired带注释的字段上为空.您可以在github boot-fx 上找到我的整个项目.

I have integrated spring boot jpa with javafx app. The integration was successful and fx screen loaded the scene, the problem was the the service is getting null on an @Autowired annotated field.You can find my whole project on github boot-fx.

pom details

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
    </dependencies>

Entity

@Entity
@Table(name = "LW_BLOOD_GROUP")
public class BloodGroup implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", nullable = false, length = 11)
    private int id;
    @Column(name = "GROUP_NAME", nullable = false)
    private String groupName;
    @Column(name = "CREATED_DATE", nullable = false)
    private Date createdDate;
    @Column(name = "IS_DELETE", nullable = false)
    private boolean isDelete;

    public BloodGroup() {
        super();
    }
//getters and setters
}

Repository

@Repository
public interface BloodRepository extends JpaRepository<BloodGroup, Integer>{
}

serviceimpl

@Service
public class BloodGroupServiceImpl implements BloodGroupService{
    @Autowired
    private BloodRepository  bloodRepository;
    @Override
    public Collection<BloodGroup> findAllBloodGroup() {
        return bloodRepository.findAll();
    }
}

fx controller

@Component
public class HomeController implements BootInitializable {
    private ApplicationContext springContext;
    @Autowired
    private BloodGroupService bloodGroupService;
    @Autowired
    private BloodRepository  bloodRepository;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {}

    @Override
    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        this.springContext = springContext;
    }

    @Override
    public void initConstruct() {}

    @Override
    public void stage(Stage primaryStage) {}

    @Override
    public Node initView() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource(Screens.HOME));
            //loader.setController(springContext.getBean(this.getClass()));
            return loader.load();
        } catch (IOException e) {
            System.err.println("can't load scene");
            e.printStackTrace();
            return null;
        }
    }

    @FXML
    private void addMemberSelect(){
         if(bloodRepository!=null){
               System.out.println("service initialized");
           }else{
               System.out.println("service null");
           }
    }

}

推荐答案

@James_D在我的代码中找到了问题. 我已经在fxml页面中指定了控制器.根据詹姆斯的建议,将控制器从fxml移至了loader.setController(this)

@James_D find the problem in my code. I have specified the controller in fxml page. As per james suggestion removed the controller from fxml to fxml controller like loader.setController(this)

这篇关于Spring Boot JPA与JavaFX Service集成为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:16