-1

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.

Inject Exception

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Rence Abishek
  • 373
  • 1
  • 4
  • 16

2 Answers2

1

As mentioned in injects documentation, it is applicable to constructors, methods and fields. Not classes.

For question 2, please refer to this other question

1

@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 })
Siliarus
  • 6,393
  • 1
  • 14
  • 30