我正在尝试通过
自动装配数据库@Autowired
private DataSource dataSource;
我的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
但我收到一条错误信息。
Could not autowire. There is more than one bean of 'DataSource' type.
Beans:dataSource (DataSourceConfiguration.class)
dataSource (DataSourceConfiguration.class)
我觉得很奇怪,因为我只在application.yml
中定义了一个数据源,据我所知,我没有定义任何其他数据源。
我尝试了配置,但我仍然遇到同样的问题。
@Configuration
@EnableConfigurationProperties
public class AppConfig {
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
这是我的'pom'文件
<?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>
我正在使用Spring Boot 1.5.2和IntelliJ 2017.1
答案 0 :(得分:19)
试试这个对我有用,像这样使用@Primary
@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);
}
然后像这样使用@autowire
@Autowired
@Qualifier("prodJdbc")
private JdbcTemplate prodJdbcTemplate;
我希望这可以帮助你或其他人:)
答案 1 :(得分:4)
请注意,尚未100%支持Spring Boot自动配置的bean,有关进度和可能的工作内容,请参阅https://youtrack.jetbrains.com/issue/IDEA-139669。
答案 2 :(得分:4)
我通过在属性上添加限定符来解决:
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;