Spring Data Autowire в FXML Controller (JavaFX)
Привет,
опитвам се да използвам Spring Data заедно с JavaFX. Искам като натисна бутон на апликацията да се запаметява в базата данни но не мога да свържа Bean-овете правилно.
Ето и кода:
Repository
package com.unibul.repositories;
import com.unibul.entities.Persona;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PersonaRepository extends JpaRepository<Persona, Long> {
}
Service Interface
package com.unibul.services.personas;
import com.unibul.entities.Persona;
public interface PersonaService {
Persona add(Persona persona);
}
Service Implementation
package com.unibul.services.personas;
import com.unibul.entities.Persona;
import com.unibul.repositories.PersonaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@Service
@Transactional
public class PersonaServiceImpl implements PersonaService {
private final PersonaRepository personaRepository;
@Autowired
public PersonaServiceImpl(PersonaRepository personaRepository) {
this.personaRepository = personaRepository;
}
@Override
public Persona add(Persona persona) {
Persona persistedPersona;
persistedPersona = personaRepository.save(persona);
return persistedPersona;
}
}
FXML Controller
@Component
public class PersonasController implements Initializable {
@Autowired
private PersonaService personaService;
@FXML
private JFXButton btn_submit;
@Override
public void initialize(URL url, ResourceBundle rb) {
//Handles all events on mouse clicks and actions
eventHandler();
}
//Handles all events on mouse clicks and actions
private void eventHandler() {
btn_submit.setOnMouseClicked((MouseEvent event) -> {
Persona persona = new Persona("some data");
personaService.add(persona);
}
}
}
Initializer
@SpringBootApplication
public class MarketingApplication extends Application {
private ConfigurableApplicationContext springContext;
private Parent root;
@Override
public void init() throws Exception {
springContext = SpringApplication.run(MarketingApplication.class);
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/layouts/Main.fxml"));
fxmlLoader.setControllerFactory(springContext::getBean);
root = fxmlLoader.load();
}
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(root, 800, 600);
primaryStage.setMinWidth(380);
primaryStage.setMinHeight(500);
Image image = new Image("/images/icon.png");
primaryStage.getIcons().add(image);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void stop() throws Exception {
springContext.stop();
}
public static void main(String[] args) {
launch(MarketingApplication.class, args);
}
}
При пускането на този код ми гърми със NullPointerException на personaService.add(persona);
Проверих и самия personaService е null което не трябва да е така. Това явно значи че не съм вързал Bean-овете правилно. Някакви идеи как да го направя?
Благодаря предварително