After an entity is persisted, I want to execute a Bean, which sends a registration Mail to the newly registered user. I want to do this with a Listener class. I did the following:
- Annotated the Entity with
@EntityListener
(UserListener.class) - Created the Listener and annotated it with
@Stateless
Here is the code of the listener: ( the system.out.println part is just for testing purposes)
@Stateless
public class UserListener {
@Inject
private MailSenderController mailSenderController;
@PostPersist
void onPostPersist(User user) throws AddressException{
System.out.println("PostPersist");
System.out.println("Username: " + user.getUsername());
mailSenderController.sendRegistrationMail(user);
}
}
The MailSenderController is a @RequestScoped
annotated Bean.
If I execute the code, I get a NullPointerException
.
If I remove mailSenderController.sendRegistrationMail(user)
, the code works fine.
I think the onPostPersist
gets executed before the MailSenderController
gets injected and this causes the NullPointerException
.
Can someone help me out with this problem?