1
我已經集成春天啓動JPA使用JavaFX應用程序。整合是成功的,FX屏幕加載場景,問題是服務在@Autowired註釋字段上變爲空。 你可以在github上找到我的整個項目boot-fx。春季啓動JPA integreated使用JavaFX服務空
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頁面上指定了控制器。 – boycod3
但是這是一個創建控制器類的新實例的指令(即一個不是由Spring管理的實例)。刪除'fx:controller'屬性並使用'loader.setController(this)' –