I have the following 2 classes:

@SpringBootApplication
public class MySpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringApplication.class, args);
    }

    @Bean
    public NamedParameterJdbcTemplate jdbcTemplate(DataSource ds) {
        return new NamedParameterJdbcTemplate(ds);
    }

    @Bean
    public DataSource datasource() {
           // ...
        }

}

@Repository
public class MyRepository {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

}

Expected behaviour: the application starts correctly Actual behaviour: the application doesn't start with the following error:

Field jdbcTemplate in test.MyRepository required a bean of type 'org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate' that could not be found.
- Bean method 'namedParameterJdbcTemplate' in 'JdbcTemplateAutoConfiguration.NamedParameterJdbcTemplateConfiguration' not loaded because @ConditionalOnSingleCandidate (types: org.springframework.jdbc.core.JdbcTemplate; SearchStrategy: all) did not find any beans

It seems that the NamedParameterJdbcTemplate is getting replaced by a JdbcTemplate by the auto-configuration. Is this intended? The 2 classes don't even share a common ancestor. It gets fixed by excluding the JdbcTemplateAutoConfiguration.

Thanks.

Comment From: snicoll

@aurasphere you're naming your NamedParameterJdbcTemplate with the same name as the JdbcTemplate (notice the type difference) that we auto-configure.

If you're using Spring Boot 2.1 with default settings, you don't get that error but rather:

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'jdbcTemplate', defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.class], could not be registered. A bean with that name has already been defined in com.example.demo.DemoApplication and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

It seems that the NamedParameterJdbcTemplate is getting replaced by a JdbcTemplate by the auto-configuration. Is this intended?

You don't have a JdbcTemplate in this app. Yet, you've named a bean with a different type with its name so the auto-configuration kicks in and replace your bean. That's why we prevent bean overriding by default as the error above gives you more information about the error.

I don't know why you have to create the template yourself but if you do, please use a different bean name