D
D
Dmitry2017-02-16 06:25:09
Java
Dmitry, 2017-02-16 06:25:09

Why is passing Autowired field values ​​as parameters to a base class method an anti-pattern?

Let's say there are service classes (1) into which other service classes (2) are injected as fields. All service classes 1 perform the same action, using the corresponding (each has its own) service class 2. It was decided to separate this repetitive action into a method of the base class of service classes 1, and also highlight the corresponding interface that all classes 2 must implement The base class method takes this interface as input, i.e. object of class 2.
Colleagues consider this approach an antipattern, namely, passing injected objects to methods of the base class. Searching the internet, I didn't find anything about this. I would like to hear an opinion from the other side.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor Alenkov, 2017-02-16
@Borz

Correctly considered. You must inject through the object2 interface in the base service class and use it there. But when declaring a service class bean, specify what will be injected there.
this is usually done through the @Required annotation over the setter of the desired bean field

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question