I'm trying to Autowire a database by
@Autowired private DataSource dataSource;
I have one datasource in my application.yml
spring: profiles: active: dev --- spring: profiles: dev datasource: driverClassName: org.mariadb.jdbc.Driver url: jdbc:mariadb://localhost:3306/dbname username: user password: password name: dev logging: level: org.springframework: INFO --- spring: profiles: prod name: prod logging: level: org.springframework: INFO
But I get an error message saying.
Could not autowire. There is more than one bean of 'DataSource' type. Beans:dataSource (DataSourceConfiguration.class) dataSource (DataSourceConfiguration.class)
Which I find strange since I only have one datasource defined in my application.yml
and to my knowledge I don't have any other datasource defined.
I gave a try with a config but I still get the same issue.
@Configuration @EnableConfigurationProperties public class AppConfig { @Bean @ConfigurationProperties(prefix="spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } }
This is my 'pom' file
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>id.group</groupId> <artifactId>ProjectName</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <!--Spring Boot dependencies--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!--Spring Boot dependencies--> <!--Spring Security dependencies--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!--Spring Security dependencies--> <!--JWT dependencies--> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.7.0</version> </dependency> <!--JWT dependencies--> <!--Actuator and HAL browser dependencies--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-hal-browser</artifactId> </dependency> <!--Actuator and HAL browser dependencies--> <!--Database dependencies--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>1.5.7</version> </dependency> <!--Database dependencies--> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
I'm using Spring Boot 1.5.2 and IntelliJ 2017.1
When a bean implementation exists both in the production and test codes, IntelliJ will mark @Autowired instances of this bean as "cannot autowire, more than one bean...". This is of course incorrect, as the test implementation will never be deployed in a production environment.
When there is more than one matching bean, the ambiguity doesn't allow Spring to autowire the properties, constructor arguments or method parameters. Let us understand this with the help of an example below.
Try this it worked for me, use @Primary like this
@Primary @Bean(name ="prodDataSource") @ConfigurationProperties(prefix="spring.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "prodJdbc") public JdbcTemplate prodJdbcTemplate(@Qualifier("prodDataSource") DataSource prodDataSource){ return new JdbcTemplate(prodJdbcTemplate); } @Bean(name = "devDataSource") @ConfigurationProperties(prefix = "spring.datasource.dev") public DataSource devDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "devJdbc") public JdbcTemplate devJdbcTemplate(@Qualifier("devDataSource") DataSource devDataSource) { return new JdbcTemplate(devDataSource); }
And then use @autowire like this
@Autowired @Qualifier("prodJdbc") private JdbcTemplate prodJdbcTemplate;
I hope this can help you or someone else :)
Please note that Spring Boot autoconfigured beans are not supported 100% yet, see https://youtrack.jetbrains.com/issue/IDEA-139669 for progress and possible workaorunds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With