0

When trying to autowire DataSource by using Spring Boot 2.0.2 RELEASE, it gives the following error and cannot manage to inject it:

'Could not autowire. There is more than one bean of 'DataSource' type.'

The beans are created in DataSourceConfiguration class for HikariDataSource, BasicDataSource and for Tomcat.

Is there a way of qualifying one of them and avoid creation of the remaining ones?

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    public SecurityConfig() {
        this.usersByUsernameQuery = // definitions here
        this.authoritiesByUsernameQuery = //
        this.allowedAuthorities = //
    }

@Autowired
    public void configureGlobal(final AuthenticationManagerBuilder auth, final DataSource dataSource) throws Exception {
        auth.jdbcAuthentication()
            .dataSource(dataSource)
            .usersByUsernameQuery(usersByUsernameQuery)
            .authoritiesByUsernameQuery(authoritiesByUsernameQuery);
    }
}

pom.xml:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>
</dependencies>

application.properties:

spring.datasource.url=jdbc:mysql://localhost:9400/test
spring.datasource.username=root
spring.datasource.password=test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
vakses
  • 21
  • 9
  • 1
    Do you actually create multiple DataSource? Spring Boot itself will only create a single one. So it will only complain if you defined multiple yourself. However as you haven’t posted any configuration, nor a full stacktrace it is a bit of guessing. – M. Deinum Nov 13 '18 at 16:42
  • you need to provide more information on your question. Based on what I understand, take a look at [this](https://stackoverflow.com/questions/30337582/spring-boot-configure-and-use-two-datasources) – want2learn Nov 13 '18 at 16:50
  • I suspect you are using some annotation (like `@EnableAutoConfiguration` or something similar), which is creating `DataSource` object. See if you have multiple such annotations. – narendra-choudhary Nov 13 '18 at 17:58
  • 1
    Please add the POM file and Spring configuration class to the question. – Boris Nov 13 '18 at 18:28
  • 1
    Possible duplicate of [Spring Boot, Spring Data JPA with multiple DataSources](https://stackoverflow.com/questions/26308035/spring-boot-spring-data-jpa-with-multiple-datasources) – Jens Schauder Nov 14 '18 at 05:23
  • @Boris as you mentioned I checked the POM file and found out that even if I run mvn clean and removed the old com.zaxxer.HikariCP dependency, in project module settings I was seeing the dependency. Once I removed it from project dependencies, the error is gone away. – vakses Nov 14 '18 at 12:11
  • Without seeing the code I will not be able to help. – Boris Nov 14 '18 at 12:39

0 Answers0