Spring framework 4.3.0 - when do I need @Autowired?

I'm just starting to learn the Spring framework (I'm using version 4.3.0), and I thought we needed @Autowired to talk about the structure when a class needs an injection.

However, today I try:

@Component
public class CDPlayer implements MediaPlayer{

    private CompactDisc cd;

    //there are no @Autowired here
    public CDPlayer(CompactDisc cd) {
        this.cd = cd;
    }

    public void play() {
        cd.play();
    }

}

and it works great with automatic wiring configuration:

@Configuration
@ComponentScan
public class CDPlayerConfigAuto {

}

So, when do I really need to use @Autowired?

+4
source share
2 answers

This is a new feature in Spring Boot 4.3. If you have only one constructor in a class, this constructor will be used to automatically match arguments. If you have more constructors or want to use a setup or field injection, you still need an annotation @Autowired.

+5

All Articles