Reputation: 2125
In Spring is the @Autowired annotation referred to only one object?
@Autowired
A object_a;
@Autowired
B object_b;
@Autowired
C object_c;
and
@Autowired
A object_a;
B object_b;
C object_c;
Are they the same thing?Thanks
Upvotes: 2
Views: 955
Reputation: 4106
@Autowire (like any annotation) is specific to the next statement. As a consequence, your examples are not the same.
The Autowired
interface may be applied to a constructor, a field or a method :
@Target(value={CONSTRUCTOR,FIELD,METHOD})
Regarding the example you gave in comment :
@Autowired A a, B b, C c;
If a
, b
and c
are fields, it won't compile, since you only have one field declaration per statement.
If they are not, it won't work either since they won't belong to the authorized types.
Upvotes: 2
Reputation: 16060
No they're not the same. The FieldDeclaration is terminated with the semicolon, as per JLS section 8.3. You could write
@AutoWired A a, b, c;
and have autowiring apply to all three, but that would probably not make much sense, since you can have only on type per field declaration...
Cheers,
Upvotes: 0
Reputation: 28569
In the context of your question the @Autowired refers to only one object. Your later statement will only autowire object_a;
Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.
Used in the way you explained, its a field-level annotation, applying to one field only
Note that if you use the @Autowire on a constructor in spring, you can inject multiple dependencies, as an example
@Autowired
public YourClass(A object_a, B object_b, C object_c) {
this.object_a = object_a;
this.object_b = object_b;
this.object_c = object_c;
}
Upvotes: 4