Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't I use @EJB with @Named?
    text
    copied!<p>I basically am trying to inject a @Stateless bean with a local interface into a class annotated with @Named! My understanding is that injection is only possible when the injection point is managed (makes perfect sense), so for example it wouldn't be possible to inject into a POJO but you could inject into a Servlet, a JSF managed or another EJB.</p> <p>I would have thought that it would have been possible to subsequently use it with @Named! However I get a NullPointerException that specifically seems to imply that this in fact doesn't seem possible!?</p> <p>My classes look like this (stripped for clarity);</p> <pre><code>@Named public class EmailUtil { // Logger------------------------------------------------------------------- private static final Logger LOG = Logger.getLogger(EmailUtil.class.getName()); // Constructor-------------------------------------------------------------- public EmailUtil() { } // EJB---------------------------------------------------------------------- @EJB AuditDAO audit; // Methods------------------------------------------------------------------ public void sendEmail( String emailSender, String emailRecipient, String emailSubject, String emailHtmlBody, String emailTextBody) throws FailedEmailException { ... code removed for clarity ... // Call Amazon SES to send the message try { new SES().getClient().sendEmail(request); // Create an audit log of the event audit.create("Email sent to " + emailSender); } catch (AmazonClientException ace) { LOG.log(Level.SEVERE, ace.getMessage(), ace); throw new FailedEmailException(); } catch (Exception e) { LOG.log(Level.SEVERE, e.getMessage(), e); } } } @Stateless public class AuditDAOImpl implements AuditDAO { // Logger------------------------------------------------------------------- private static final Logger LOG = Logger.getLogger(AuditDAOImpl.class.getName()); // EntityManager------------------------------------------------------------ @PersistenceContext(unitName = "iConsultPU") private EntityManager em; @Override public void create(String event) { String subject; try { /* * If the current subject has authenticated and created a session we * want to register their ID. However it is possible that a subject * does not have an ID so we want to set it to unknown. */ subject = SecurityUtils .getSubject() .getPrincipals() .asList() .get(1) .toString(); } catch (Exception e) { subject = "UNKNOWN"; } Audit audit = new Audit(); audit.setUserId(subject); audit.setEventTime(Calendar.getInstance()); audit.setEvent(event); em.persist(audit); } } @Local public interface AuditDAO { public void create(String event); } </code></pre> <p>I've tried using @Inject as well but that doesn't seem to work either. Have I misunderstood the specification or just poorly implemented it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload