ericj
ericj

Reputation: 2291

A method with @Bean autowires without @Autowired

In my Java Config file there is

@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc){
    return new CDPlayer(compactDisc);
}

My book says that the (singleton) CompactDisc is autowired into the method. Why? Because I do not see @Autowired. How can this work?

Upvotes: 0

Views: 131

Answers (1)

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

About @Bean in Spring

A @Bean annotated method can have an arbitrary number of parameters describing the dependencies required to build that bean

The resolution mechanism is pretty much identical to constructor-based dependency injection, see the relevant section for more details.

Every parameter in a @Bean method will be resolved (injected by Spring container).

Upvotes: 5

Related Questions