Reputation: 3567
I have a POJO which I want to inject into a CDI Bean. Now I get that I could change the discovery mode in beans.xml from 'annotated' to 'all'. But I also could just give my POJO a bean defining annotation. I guess any annotation would work, but I was wondering, what is the correct annotation just for the single purpose to make my POJO injectable?
Upvotes: 2
Views: 2286
Reputation: 6753
Assuming you want to stay with bean discovery mode annotated
, you need to have a bean-defining annotation on your POJO which will make it so called implicit bean
. Here is a relevant quotation from CDI spec:
The set of bean defining annotations contains:
@ApplicationScoped, @SessionScoped, @ConversationScoped and @RequestScoped annotations,
all other normal scope types,
@Interceptor and @Decorator annotations,
all stereotype annotations (i.e. annotations annotated with @Stereotype),
and the @Dependent scope annotation.
As for which annotation is correct - that depends on your use case. Typically you need to take into consideration the lifecycle of beans (requests, conversations, application scoped which lives as long as the app does). But if you really don't care, I would say just go for @Dependent
.
In fact, if you have bean discovery mode all
and had no annotation on that POJO class, it would be picked up by CDI as @Dependent
anyway.
Hope that answers the question.
Upvotes: 2
Reputation: 1955
You may have no annotation at all, no annotation is required to make your bean injectable (i.e. to make it managed or cdi bean).
In order to inject class it should be:
@Decorator
@Inject
ejb-jar.xml
.So you can inject, pretty much, all 'normal' classes. Also, the class should be located inside bean archive. Starting from CDI 1.1 there are two types of bean archives: implicit and explicit. From Oracle documentation:
An explicit bean archive is an archive that contains a beans.xml deployment descriptor, which can be an empty file, contain no version number, or contain the version number 1.1 with the bean-discovery-mode attribute set to all. For example:
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1" bean-discovery-mode="all"> ... CDI can manage and inject any bean in an explicit archive, except those annotated with @Vetoed.
An implicit bean archive is an archive that contains some beans annotated with a scope type, contains no beans.xml deployment descriptor, or contains a beans.xml deployment descriptor with the bean-discovery-mode attribute set to annotated.
In an implicit archive, CDI can only manage and inject beans annotated with a scope type.
For a web application, the beans.xml deployment descriptor, if present, must be in the WEB-INF directory. For EJB modules or JAR files, the beans.xml deployment descriptor, if present, must be in the META-INF directory.
Upvotes: 2
Reputation: 5568
@Dependent will create a new instance for that bean on every injection point. Usually the least intrusive.
http://docs.oracle.com/javaee/7/api/javax/enterprise/context/Dependent.html
Upvotes: 3