Spring @ Multiple Autowire Objects

I like code that is visually easy to read (subjective, I know). Is there a way in Spring to take the first kind of code

@Autowired private O1 o1 @Autowired private O2 o2 @Autowired private O3 o3 

And do something like this:

 @Autowired private O1 o1 private O2 o2 private O3 o3 @Endautowire 

I would find the code less cluttered. I know that I'm trivial and picky, but ...

+4
source share
5 answers

Only possible with a preprocessor for java sources. But, in my opinion, this is not worth it.

0
source

You can use the constructor to enter all objects with one annotation:

 private O1 o1; private O2 o2; private O3 o3; @Autowired public ClassA(O1 o1, O2 o2, O3 o3) { this.o1 = o1; this.o2 = o2; this.o3 = o3; } 
+4
source

There were times when people used to declare a type variable,

 int i1, i2, i3; 

This is no longer considered best practice, but announces that each of them individually increases readability .

Coming to your question, like mrembisz said that this should happen, should happen in the source files. In other words, the java compiler must change to handle such a script .

Turning to readability, your solution will only increase code clutter and decrease readability , suppose I have 10 beans injected, each time I need to see the code above and below to know if it is automatically connected or not.

So, I doubt it is worth the time or not.

+2
source

Not the way you want with annotations.

You can create a uber type that accepts these three, and add this type here. Id do this only if you find yourself in this situation a lot of time or really reflect the concept in your application.

0
source

I had the same idea as the original poster, but after reading the answers, I completely agree with the consensus that it is better to annotate members individually. I put forward the initial question because it is a good question , despite its poor basic assumption , and because it has led to even better answers.

0
source

Source: https://habr.com/ru/post/1400736/


All Articles