2

I have a Java 4 and Spring Boot 2.4.0-SNAPSHOT application.

It needs to access two separate datasources. I am also using Spring jdbc to query the databases.

I have tried an implementation (see below), but I get errors.

I have the following:

application.properties

# pims datasource
spring.datasource1.driver-class-name=org.postgresql.Driver
spring.datasource1.url=jdbc:postgresql://localhost:5432/pims
spring.datasource1.username=postgres
spring.datasource1.password=
spring.jpa.database-platform=postgres
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
# approval datasource
spring.datasource2.driver-class-name=org.postgresql.Driver
spring.datasource2.url=jdbc:postgresql://localhost:5432/approval
spring.datasource2.username=postgres
spring.datasource2.password=

and

MultipleDBConfig.java

@Configuration
@ComponentScan(basePackages = "com.nexct")
public class MultipleDBConfig {

    @Bean(name = "datasource1")
    @ConfigurationProperties("spring.datasource1")
    @Primary
    public DataSource dataSource1(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "datasource2")
    @ConfigurationProperties("spring.datasource2")
    public DataSource dataSource2(){
        return DataSourceBuilder.create().build();
    }
}

Then in the DAO, I define the jdbcTemplate.

CompanyContactDAOImpl.java

@Repository
public class CompanyContactDAOImpl implements CompanyContactDAO {

    @Autowired
    @Qualifier("datasource1") // pims datasource
    private JdbcTemplate jdbcTemplate;

ApprovalRequestDAOImpl.java

@Repository
public class ApprovalRequestDAOImpl implements ApprovalRequestDAO {

    @Autowired
    @Qualifier("datasource2") // approval datasource
    private JdbcTemplate jdbcTemplate;

Now when I start Spring Boot, I get the following error:

Could not autowire. Qualified bean must be of 'JdbcTemplate' type.

and

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'approvalRequestDAOImpl': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 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), @org.springframework.beans.factory.annotation.Qualifier("datasource2")}

and

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'approvalRequestDAOImpl': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 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), @org.springframework.beans.factory.annotation.Qualifier("datasource2")} at Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 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), @org.springframework.beans.factory.annotation.Qualifier("datasource2")}

2

1 Answer 1

1

The stack trace is related to something else. Your JdbcTemplates require a datasource, they're not datasources by themselves. Create a JdbcTemplate and inject the datasource rather than setting the qualifiers on the JdbcTemplates, for instance:

@Repository
public class ApprovalRequestDAOImpl implements ApprovalRequestDAO {

    private JdbcTemplate jdbcTemplate;

    public ApprovalRequestDAOImple(@Qualifier("datasource2") DataSource ds) {
        this.jdbcTemplate = new JdbcTemplate(ds);
    }
}

More info here

Sign up to request clarification or add additional context in comments.

3 Comments

thank you, that fixed my issue, I get no errors now in start up. I am just going to test it.
I just get the following error now, which I think is related to my properties file: HikariPool-1 - jdbcUrl is required with driverClassName.. I am just looking to fix it
All works now, thank you. Fix for HikariPool-1 - jdbcUrl is required with driverClassName. stackoverflow.com/questions/49352800/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.