More Than Five
More Than Five

Reputation: 10419

Failed to load JDBC Template that could not be found

In my SpringBoot application, I have a service defined as:

@Service
public class JdbcService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    public DataSource myDatabase;

    public void run() {
        System.out.println(">>run()");
        jdbcTemplate.setDataSource(stagingDatabase);
        jdbcTemplate.execute("INSERT SAMPLETABLE1 (STRING1, STRING2) VALUES ('TEST1', 'TEST2)");
        System.out.println("<<run()");
    }
}

I get:

Description:

Field jdbcTemplate in com.myproject.services.JdbcService 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.

When I add @ComponentScan(basePackages="com.myproject") to my SpringBootApplication defined in this package, I also get it.

build.gradle includes:

compile("org.springframework.boot:spring-boot-starter-jdbc")

Any ideas?

Upvotes: 1

Views: 3343

Answers (1)

More Than Five
More Than Five

Reputation: 10419

I had to define it as a Bean. I also renamed it to make it clearer.

@Configuration
public class DatasourceConfiguration {

    @Bean
    JdbcTemplate dwJdbcTemplate() {
            JdbcTemplate dwDatasource = new JdbcTemplate(dwDatasource());
            return dwDatasource;
    }
}

Upvotes: 2

Related Questions