title: Generics and Dependency Injection in Java
body: <p>I am developing a Java EE application that uses JAX-RS for the WebLayer.</p>
  
  <p>I tried to break down the problem. So the code you’ll see is just a cutout. For now everything works fine. As you can see I have an <code>UserEndpoint</code> where a client can create a new user. After everything is done a verification mail is sent by the <code>VerificationService</code>.</p>
  
  <p><strong>UserEndpoint</strong></p>
  
  <pre><code>@Path("/users")
  @Stateless
  public class UserEndpoint {
  
      @Inject
      @AuthenticatedUser
      private User authenticatedUser;
  
      @EJB
      private UserDAO userDAO;
  
      @EJB
      private TokenDAO tokenDAO;
  
      @Inject
      private PasswordValidator passwordValidator;
  
      @Inject
      private UserValidator validator;
  
      @Inject
      private VerificationService verificationService;
  
      @POST
      @Produces(MediaType.APPLICATION_JSON)
      @Consumes(MediaType.APPLICATION_JSON)
      public User createUser(User user) {
  
          // Validation and Creation
  
          // Verification
          this.verificationService.sendVerificationMail(user);
  
          return user;
      }
  }
  </code></pre>
  
  <p><strong>VerificationService</strong></p>
  
  <pre><code>@Singleton
  public class VerificationService {
  
      @Inject
      private MailService mailSender;
  
      @Inject
      private TemplateService templateService;
  
      public void sendVerificationMail(User user) {
          this.templateService.setUserTemplate(TemplateCategory.VerificationTemplate, user);
          mailSender.sendMail(new Message(
              MessageDAO.systemEmailAddresses.get(0),
              user.getEmail(),
              templateService.getSubject(),
              templateService.getContent()));
      }
  }
  </code></pre>
  
  <p>Because I want to have a scalable project I decided to encapsulate parts of the endpoint, so I can make use of it in other projects. Since every user has to be verified in one way or another I thought that the encapsulation of the verification process would be a good idea. The problem is that I have no clue how to realize that. My first approach was as followed:</p>
  
  <p><strong>CoreUserEndpoint</strong></p>
  
  <pre><code>@Path("/users")
  @Stateless
  public abstract class CoreUserEndpoint&lt;D extends BaseUserDAO&lt;T&gt;, T extends BaseUser, V extends CoreUserValidator&lt;D, T&gt;&gt; extends EntityEndpoint&lt;D, T, V&gt; {
  
      @EJB
      private TokenDAO tokenDAO;
  
      @Inject
      private PasswordValidator passwordValidator;
  
      @Inject
      private VerificationService&lt;T&gt; verificationService;
  
      @POST
      @Produces(MediaType.APPLICATION_JSON)
      @Consumes(MediaType.APPLICATION_JSON)
      public User createUser(User user) {
  
          // Validation and Creation
  
          // Verification
          this.verificationService.sendVerificationMail(user);
  
          return user;
      }
  }
  </code></pre>
  
  <p><strong>EntityEndpoint</strong></p>
  
  <pre><code>@Stateless
  public class EntityEndpoint&lt;D extends CoreDAO&lt;T&gt;, T extends CoreEntity, V extends CoreValidator&gt; {
  
      protected D entityDAO;
  
      @Inject
      @AuthenticatedUser
      protected T authenticatedUser;
  
      protected V entityValidator;
  
  }
  </code></pre>
  
  <p><strong>VerificationService</strong></p>
  
  <pre><code>@Singleton
  public class VerificationService&lt;T extends BaseUser&gt; {
  
      @Inject
      private MailService mailSender;
  
      @Inject
      private TemplateService&lt;T&gt; templateService;
  
      public void sendVerificationMail(User user) {
          this.templateService.setUserTemplate(TemplateCategory.VerificationTemplate, user);
          mailSender.sendMail(new Message(
              MessageDAO.systemEmailAddresses.get(0),
              user.getEmail(),
              templateService.getSubject(),
              templateService.getContent()));
      }
  }
  </code></pre>
  
  <p>When I tried this I get the famous <code>WELD-001408: Unsatisfied dependencies</code> error. And I think I do understand why this is happening. As I read <a href="https://stackoverflow.com/questions/19234733/why-is-it-impossible-to-inject-generic-classes">here</a> I cannot make use of generics, neither for <code>@Inject</code> nor for <code>@EJB</code>. But if I am not able to use generics how can I encapsulate the core of my project? Is their any work around or a different approach? I am sure there already is a solution for that anywhere in the internet. But after 3 days of searching without making any progress I get really frustrated and thought It would be best to ask you guys. So I would really appreciate if you could point me in the right direction.</p>