Ich versuche, Benutzerdaten in der Datenbank durch Spring-Daten JPA zu speichern. Leider, wenn ich in meiner Controller-Klasse ich RegistrazioneService aufrufen, kommt es Null.Spring Daten JPA - Service immer Null
registrazioneController.java: es ist ein normaler JSF-Controller ...
@ManagedBean(name="registrazioneController")
@ViewScoped
public class RegistrazioneController {
private String username = "";
private String password = "";
private String repassword = "";
private String email;
@ManagedProperty(value = "#{registrazioneService}")
private RegistrazioneService registrazioneService;
public void registraUser() {
System.out.println("inizio registrazione utente...");
// si salva nel db il nuovo utente
User utente = new User();
utente.setUsername(username);
utente.setPassword(password);
utente.setEmail(email);
utente.setDataCreazione(new Date());
// di default si assegna all'utente il ruolo ROLE_USER
utente.setRole(Role.ROLE_USER);
registrazioneService.registraUtente(utente);
}
RegistrazioneService:
@Service
public class RegistrazioneService {
@Autowired
UserRepository userRespository;
@Transactional
public void registraUtente(User user) {
System.out.println(("sono in registraUtente()"));
userRespository.save(user);
}
}
und das ist UserRepository:
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
}
Kann mir bitte jemand helfen?
fügen Sie einen 'setRegistrazioneService' zu Ihrem Controller hinzu. –
Können Sie die importierte "@ Service" -Anmerkung noch einmal überprüfen? Geht es um 'org.jvnet.hk2.annotations' oder' org.springframework.stereotype' Paket? – harshavmb