Spring boot jpa integreated with javafx service null

别来无恙 提交于 2021-01-29 03:18:45

问题


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"); 
           }
    }

}

回答1:


@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)



来源:https://stackoverflow.com/questions/38944767/spring-boot-jpa-integreated-with-javafx-service-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!