mercredi 17 mars 2021

Why does @SpringBootTest need @Autowired in constructor injection

A more general question. If one uses constructor injection in a regular Spring managed class the classes get autowired without requiring the @Autowired annotation, i. e.:

@Service
class MailService(
  private val projectService: ProjectService,
  private val mailer: Mailer
) { ... }

Following the same constructor injection principle in a @SpringBootTest class, you need to have the @Autowired annotation set to the constructor parameter otherwise it will fail to inject the class, i. e.:

@SpringBootTest
internal class MailerTest(
  @Autowired private val mailer: Mailer
) { ... }

Why does this difference occur?

Aucun commentaire:

Enregistrer un commentaire