concept @inject in category ejb

appears as: @Inject, @Inject
EJB 3 in Action, Second Edition

This is an excerpt from Manning's book EJB 3 in Action, Second Edition.

We’re going to analyze a lot of code in this book—some just as easy as this. You could trigger the hello as a web service by simply adding the @WebService annotation. You could inject a resource, like a helper bean that will translate hello into foreign languages, with @Inject. What do you want to do with EJB? If you keep reading, we’ll probably tell you how to do it.

CDI and EJB are fully integrated. This means that EJBs can use all of the services we just discussed including dependency injection, event notification, interceptors, and decorators. These services are available to all EJB types: singleton, stateless, stateful, and message-driven beans. Because there’s an overlap in services, you might be wondering when you should use the EJB services (injection, interceptors, and so on) versus their CDI counterparts. The simple answer is to use the CDI variant because they’re more powerful, generic, and not limited to just EJBs. As you’ll see, you can safely use the @Inject instead of the @EJB.

12.3.1. Injection with @Inject

The @Inject annotation is the heart of CDI. This annotation marks a point where an instance of a bean needs to be injected. It can be placed either on an instance variable or on a constructor. When the CDI container goes to instantiate a class containing fields marked with @Inject, it checks to see if an instance already exists; if not, it creates a new instance and sets the value. It’s important to note that if a bean isn’t annotated with a scoping annotation (@Conversation, @RequestScoped, or @SessionScoped, among others) then it falls under the dependent scope, which can be explicitly marked using the @Dependent annotation. CDI looks at the type on the injection point, as well as any qualifier annotations, to figure out what object needs to be injected or created and then injected. All of this resolution is performed when a bean is first instantiated.