Failed to configure a DataSource

在用spring cloud的時候,先建立了一個parent項目,而後分別加入eureka-server模塊、短信模塊,運行正常。最後再加入支付模塊(需整合jpa(或者myBatis)、mySql)以後,eureka-server模塊和短信模塊報錯:spring

Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class  Action: Consider the following:	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

經查是由於引入了jpa的包ide

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

spring boot 會默認加載org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration這個類,而DataSourceAutoConfiguration類使用了@Configuration註解向spring注入了dataSource bean,又由於項目(eureka-server模塊和短信模塊)中並無關於dataSource相關的配置信息,因此當spring建立dataSource bean時因缺乏相關的信息就會報錯。spring-boot

解決辦法:ui

一、在@SpringBootApplication註解上加上exclude,解除自動加載DataSourceAutoConfiguration。url

@EnableEurekaServer@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)public class EurekaServerApplication { 	public static void main(String[] args) {		SpringApplication.run(EurekaServerApplication.class, args);	}}

二、在parent項目的pom.xml文件中保存全部子模塊的共有jar依賴,非共有的依賴則在各模塊自身的pom.xml文件中進行申明。建議採用此方法,好處在於各模塊的依賴不會相互產生干擾。spa

相關文章
相關標籤/搜索