Spring autowire=byType with Java Config?

Spring autowire=byType with Java Config?

When you just autowire via @Autowired annotation – it means autowiring by type.

If we want to autowire by name we need to use @Autowired and @Qualifier annotation together.

Example:

@Configuration
public class JavaConfig {

    @Bean
    @Qualifier(stackoverflow)
    public Company company(){
    }

    @Bean
    public Employee employee1(@Autowired Company company){
    }

    @Bean
    public Employee employee2(@Autowired @Qualifier(stackoverflow) Company company){
    }

}

Updated: Also you can use parameter of @Bean annotation:

@Bean
public Company company(){
    return new Company();
}

@Bean(autowire = Autowire.BY_NAME)
public Employee employee1(@Autowired Company company){
    return new Employee();
}

Please see additional information here

Spring autowire=byType with Java Config?

Leave a Reply

Your email address will not be published. Required fields are marked *