Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available error

I am running through the tutorial.example on https://developer.ibm.com/tutorials/spring-with-db2-via-jdbc/ but cannot get it to work, i keep getting the below error and am unsure how to fix.

No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}'

Nothing to do with setting up a bean is mentioned in the tutorial so am unsure if i should be breaking off it to fix it or i've just made a mistake.

my application class -

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

Example rest controller -

package application.rest.v1;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
import main.java.application.jdbc.*;

@RestController
public class Example {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping("test")
    public @ResponseBody ResponseEntity<String> example() {
        List<String> list = new ArrayList<>();
        list.add("Table data...");
        jdbcTemplate.query(
                "SELECT * FROM things", new Object[]{},
                (rs,rowNum) -> new Things(rs.getLong("id"), rs.getString("name")))
                .forEach(thing -> list.add(thing.toString()));
        return new ResponseEntity<String>(list.toString(), HttpStatus.OK);
    }
}

application.properties -

spring.datasource.url=jdbc:imdb://xxxx.xxx.xxxx/xxxx
spring.datasource.username=xxxxxxx
spring.datasource.password=xxxx
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Also, i am not connecting to the suggested DB2 instance in the tutorial but my own instance.

like image 941
SaskatoonDiver Avatar asked Mar 04 '23 11:03

SaskatoonDiver


1 Answers

I believe you are missing the part where you are supposed to configure JdbcTemplate in your configuration. As you are using spring boot, you can achieve it through @Configuration annotation on a class. You typical configuration will look something like below

@Configuration
public class WebAppConfig {

    @Bean(name = "appDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "applicationJdbcTemplate")
    public JdbcTemplate applicationDataConnection(){
        return new JdbcTemplate(dataSource());
    }
}
like image 94
Coder Avatar answered Mar 06 '23 12:03

Coder