Reputation: 3887
My question is quick, we use @Autowire to wire beans by type, and @Resource by name, but I have always seen these annotations used to wire variables inside a class, can they be used at a classlevel as well to wire all the properties of a object?
Thank you
Upvotes: 1
Views: 134
Reputation: 10709
Yes, just look at code or javadoc
Autowired: ctor (but only one), field, method, annotation
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
Resource: type, field, method
@Target({TYPE, FIELD, METHOD})
Upvotes: 0
Reputation: 19543
Let me try to answer your question with the API of the annotations.
@Retention(value=RUNTIME)
@Target(value={CONSTRUCTOR,FIELD,METHOD})
public @interface Autowired
@Target(value={TYPE,FIELD,METHOD})
@Retention(value=RUNTIME)
public @interface Resource
If you want to know when you can use the annotations go to the API and take special attention to the @Target values.
ANNOTATION_TYPE Annotation type declaration
CONSTRUCTOR Constructor declaration
FIELD Field declaration (includes enum constants)
LOCAL_VARIABLE Local variable declaration
METHOD Method declaration
PACKAGE Package declaration
PARAMETER Parameter declaration
TYPE Class, interface (including annotation type), or enum declaration
In general annotation only could be used in the places for which they are defined in the @Target values.
Upvotes: 1