[英]Spring boot jpa integreated with javafx service null
我已经将spring boot jpa与javafx app集成在一起。 集成成功并且fx屏幕加载了场景,问题是服务在@Autowired注释字段上变为null。 你可以在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页面中指定了控制器。 根据james建议将控制器从fxml移除到fxml控制器,如loader.setController(this)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.