I am learning javaEE and I read somewhere about the main usage of cdi's was back then first in jsf-managed beans with annotations like @requestscope, @applicationscope, etc.. now in newer javaEE versions the cdi got available everywhere (even in ejb beans) so the question is, how do I have to annotate a class which shall be injected inside my local stateless ejb? I am asking this because the annotations like @RequestScope and all those are from jsf but I am not using jsf. Is @Default enough since its marked as default anyway? Is @Dependent better choice?
@Stateless
public class FooEjb{
@Inject Bar b;
}
// what annotation to put here?
public class Bar {
...
}
Yes you don't need JSF to use CDI in JavaEE.
If you are using CDI without using JSF, use the scope annotations from the javax.enterprise.context
package.
@Default
is a qualifier which as the name suggests the default qualifier. If you have multiple implementations/instances of the same class in your container, then you can use qualifiers to distinguish.
@Dependent
is a scope which is the default scope. This means it will depend on the scope of the class it's injected in. A new instance of a @Dependent
class will be injected every time a new instance of the class in which it is injected is created.
To enable CDI you need to put a beans.xml
file in WEB-INF
directory of your web project or META-INF
directory of your EAR or EJB project.