Search code examples
javaspringdependency-injectionopenjpa

javax.inject.Inject does not work


In our project we did some Maven pom.xml cleanup and dependency injection stopped working. For example, this used to work but works no longer:

@Inject
private ItemService itemService;

public void whatever() {
    itemService.whatever();
}

The itemService is null when we run whatever(), no error/warning message present in log. There's only one bean implementing the interface:

public interface ItemService { ... }

@ContainerManaged
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ItemServiceBean implements ItemService { ... }

We tried putting all the dependencies back in the project where this error occurs, but that didn't help. I don't know where to start looking. Any of these would be helpful:

  • Where is the @Inject used? At runtime, by Spring? Is there some source code that would help? Perhaps the jar for that code is what we're missing?
  • Is there some way to get some hint printed in the log? Even better would of course be some error at compile time, but that is probably pushing my luck...
  • Best would of course be if you knew what dependency we're missing. :) We're running Spring, OpenJPA and some more, but don't know what is relevant to list. If you know what to ask, I'll try my best to find the answer.

Solution

  • Include the following dependency in your pom.xml

    <dependency>
      <groupId>javax.inject</groupId>
      <artifactId>javax.inject</artifactId>
      <version>1</version>
    </dependency>