2
I am learning Spring, and several times I have come across instructors solving the "problem" of instantiation of objects being made by keyword new
, as in:
private SpeakerRepository repository = new HibernateSpeakerRepositoryImpl();
Normally, to solve this, they use the Autowired (dependency injection) annotation, with the justification that use instantiation by new
, leaves hard coded code.
I can’t understand how the keyword new
leaves hard-coded code.
Thank you so much for your help!
The question of being considered "Hard Coded" is that preference should be given to using the Spring dependency management mechanism. Create
@Bean
and inject using the@Autowired
, it is preferable to use a constructor to receive the dependencies than to use the@Autowired
on the estates.– Giovane