Sobes.tech
Junior — Middle

Do you use explicit @Autowired annotation in your code?

sobes.tech AI

Answer from AI

In modern versions of the Spring Framework (starting from Spring 4.3), explicitly annotating with @Autowired is not always necessary, especially if there is a single constructor in the class — Spring will automatically inject dependencies through that constructor.

However, if the class has multiple constructors or dependencies need to be injected into fields or setters, then @Autowired is still required to explicitly specify the injection point.

Example without @Autowired on the constructor:

@Component
public class MyService {
    private final Dependency dep;

    public MyService(Dependency dep) {
        this.dep = dep;
    }
}

If field injection is used, then @Autowired must be explicitly added:

@Component
public class MyService {
    @Autowired
    private Dependency dep;
}

Thus, the explicit use of @Autowired depends on the context and coding style, but in modern projects, constructor injection without annotation is preferred.

Do you use explicit @Autowired annotation in your… - sobes.tech