package actors;
import akka.actor.UntypedActor;import dao.notification.NotificationDAO;import java.util.List;import javax.inject.Inject;import models.notification.Notification;import play.db.jpa.JPAApi;
public class NotificationsActor extends UntypedActor { @Inject JPAApi jpaApi;
@Inject NotificationDAO notificationDAO;
@Override public void onReceive(Object message) throws Exception { jpaApi.withTransaction(() -> { // Check if any new notifications have been received List<Notification> notifications = notificationDAO.findAllUnsentNotifications();
try { for (Notification notification : notifications) { notification.sendMail(); } } catch (Exception ex) { ex.printStackTrace(); } }); }}
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/678d943a-43c7-437c-8ca4-60ff17e44540%40googlegroups.com.
1. I would like to use DI in the proper way.This solution is for a class, that is not injected.
2. You are write that constructor injection is better practice, I meant that the result is the same: if a class is has not injection binding, using of constructor injection will not help.
3. The link you have mentioned is for Scala. The original question was about java. I found the relevant link. It is good to know, that the proper DI may be done.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/8f8b1e1a-26e6-47f8-ab45-2ca3b6a92d87%40googlegroups.com.