SpringBoot not automatically configuring jdbctemplate

24 Views Asked by At

I have a SpringBoot application using JdbcTemplate for accessing a relational database. I have a dependency, called DbManager, which have the methods regarding the db operations and requires a JdbcTemplate object as a constructor parameter. This is configured in the AppConfiguration class as a Bean:


@Configuration
public class AppConfiguration implements WebMvcConfigurer {

    @Bean
    DbManager dbmanager(JdbcTemplate jdbcTemplate) {
        return new DbManager(jdbcTemplate);
    } 

}

In application.yaml I have declared a datasource, which will be used by jdbctemplate

spring:
 
  thymeleaf:  
    suffix: .html
    prefix: classpath:/templates/
    mode: HTML

  datasource:
    url: <jdbc_url>
    username:  <my_username>
    password: <my_pass>
    hikari:
        minimumIdle: 1
        maximumPoolSize: 2
        idleTimeout: 10000
        connectionTimeout: 30000

The relevant dependencies in pom.xml are the following:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc11</artifactId>
    <scope>runtime</scope>
</dependency>

When I try to launch the application, I get:

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

Description:

Parameter 0 of method dbmanager in com.example.myapp.configuration.AppConfiguration required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.jdbc.core.JdbcTemplate' in your configuration.

However, when I manually configure a Bean for the datasource, the application initializes correctly:

@Bean
public DataSource dataSource() {
      return DataSourceBuilder.create()
                .url("<jdbc_url>")
                .username("<my_username>")
                .password("<my_pass>")
                .build();
}

Shouldn´t Spring Boot automatically configure a JdbcTemplate bean when it detects a DataSource bean in the application context?

I am using springbootframework version 3.2.2

0

There are 0 best solutions below