Apply the same annotation to multiple fields

Is it possible to apply the same annotation to several fields (if there are many private fields, and it’s just awkward to annotate them).

So, I have how

@Autowired private BlahService1 blahService1;
@Autowired private BlahService2 blahService2;
@Autowired private BlahService3 blahService3;

etc.

I tried the following but it will not work

@Autowired{     
   private BlahService1 blahService1;       
   private BalhService2 blahService2;   
}

Perhaps something similar to user annotations?

+5
source share
3 answers

No, but you can annotate your constructor, not your fields. This will have the added benefit of making your class more easily verifiable by injecting mock dependencies when building an instance for testing (which is the main reason for the usefulness of the injection):

@Autowired
public MyClass(BlahService1 blahService1, BlahService2 blahService2, BlahService3 blahService3) {
    this.blahService1 = blahService1;
    this.blahService2 = blahService2;
    this.blahService3 = blahService3;
}
+6
source

, .

, , " " .

, , @Autowired , . , .

+1

AutoWired, , , , , , .

0

All Articles