Relaximus
Jan 14, 2024

--

In short - you're right, in more details - not.

@Autowired requires injection candidate by default. So in that sense no difference if it's constuctor or field injection. The tiny difference here is the moment of injection: constructor requires dependencies to exist at the moment of instance of the class creation. In case of field instance could be created but later dependency could be not found during graph resolving and fail overall.

So what it gives you may ask? It means that with the field injection theoretically your bean may exist in not consistent state for a very short moment, when not all depencies resolved and may throw NPE from internal initizalization logic if it exist before the whole APP fail.

--

--

Responses (2)