I have created new project via Java Web
->WebApplication
->Enabled CDI
My First question is: Why @Inject annotation showing error? even though bean.xml
added.
I have created new project via Java Web
->WebApplication
->Enabled CDI
My First question is: Why @Inject annotation showing error? even though bean.xml
added.
As mentioned in injects documentation, it is applicable to constructors, methods and fields. Not classes.
For question 2, please refer to this other question
@Inject
is not applicable to class. For one this is defined in inject documentation/javadoc.
But most of all, you can open up the Inject
class in your IDE and see the following line:
@Target({ METHOD, CONSTRUCTOR, FIELD })
Such line is present on all Java annotations and it dictates where you can put such annotation. In this case it is on a method, constructor and field.
If an annotation was to be used on class level as well, the @Target
would also have the TYPE
there. Hence it would look like this:
@Target({ METHOD, CONSTRUCTOR, FIELD, TYPE })