0

I'm trying to save User data on database through Spring data JPA.Unfortunately when in my controller class I call RegistrazioneService it comes null.

registrazioneController.java : it's a normal 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);

}
}

and that's UserRepository :

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

 }

Can someone help me please?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
alex
  • 65
  • 1
  • 8

1 Answers1

-2

Have you tried to put @Autowired instead of @ManagedProperty(value = "#{registrazioneService}") ?

mjayson
  • 7
  • 1
  • 4
  • yes it's null anyway – alex Jun 03 '17 at 10:55
  • Therefore put a constructor so that spring will be able to inject the bean. public RegistrazioneController (RegistrazioneService registrazioneService){ this.registrazioneService = registrazioneService; } – mjayson Jun 03 '17 at 10:57
  • nothing ... it's null anyway :( – alex Jun 03 '17 at 11:12
  • reason it's null because spring didn't inject the bean into that class – mjayson Jun 04 '17 at 00:46
  • @mjayson, a question should be posted as a comment to the question, not as an answer, since it implies that more clarification is required from the poster. – manish Jun 05 '17 at 06:32