【问题标题】:Spring boot jpa integreated with javafx service nullSpring boot jpa 与 javafx 服务集成 null
【发布时间】:2016-08-14 17:30:24
【问题描述】:

我已将 spring boot jpa 与 javafx 应用程序集成。集成成功并且 fx 屏幕加载了场景,问题是服务在 @Autowired 注释字段上变为空。 你可以在 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"); 
           }
    }

}

【问题讨论】:

  • 你为什么注释掉loader.setController(...)这一行?你不只需要loader.setController(this);吗?
  • 因为我已经在 fxml 页面上指定了控制器。
  • 但这是创建控制器类的新实例的指令(即不由 spring 管理的实例)。删除fx:controller 属性并使用loader.setController(this)
  • 感谢@James_D 这就是问题所在..

标签: java javafx spring-boot spring-data-jpa


【解决方案1】:

@James_D 在我的代码中找到问题。 我已经在 fxml 页面中指定了控制器。根据 james 的建议,将控制器从 fxml 删除到 fxml 控制器,例如 loader.setController(this)

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2018-11-28
    • 1970-01-01
    • 2016-08-27
    • 2018-12-21
    相关资源
    最近更新 更多