![](/img/trans.png)
[英]Gradle, Spring Boot, spring-boot-starter-data-jpa upgrade
[英]Spring boot application with Spring-boot-starter-data-jpa and spring-boot-starter-test makes spring does not find application properties
我創建了一個帶有 spring 引導的小型應用程序,該應用程序使用 spring 數據訪問數據庫(mySql):
- spring-boot-starter-parent:2.4.2
- spring-boot-starter-web:2.4.2
- spring-boot-starter-data-jpa: 2.4.2
- mysql-connector-java:8.0.22
- 彈簧引導啟動器測試:2.4.2
- org.junit.jupiter: 5.7.0
- junit-jupiter-engine: 5.7.0
我已經在 src/main/resources 中配置了我的 application.properties,並且我已經配置了 url 的屬性,用戶名、密碼、驅動程序……,如下所示:
spring.datasource.url=jdbc:mysql://localhost:3306/BBDD
spring.datasource.username=XXX
spring.datasource.password=XXXX
spring.datasource.driver=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect:org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
我有一個用於訪問我的數據庫的存儲庫接口:
@Repository
public interface ArticleRepository extends PagingAndSortingRepository<ArticuloEntity, Integer> {
}
另外,我已經定義了我的實體。
問題在於當我啟動我的 webapp 時,我收到一個錯誤,因為它沒有從 application.properties 讀取配置數據源的參數:
描述:
無法配置數據源:未指定“url”屬性,並且無法配置嵌入式數據源。
原因:無法確定合適的驅動程序 class
行動:
考慮以下幾點:如果您想要一個嵌入式數據庫(H2、HSQL 或 Derby),請將其放在類路徑中。 如果您有要從特定配置文件加載的數據庫設置,您可能需要激活它(當前沒有配置文件處於活動狀態)。
如果我刪除 spring-boot-starter-test 和 junit 的依賴項,應用程序加載正常,並且可以訪問數據庫。 但是,如果我添加依賴項來添加我的 junit,它不會讀取我的 application.properties 文件,並且我會收到之前的錯誤。
如何使用 junits 配置我的應用程序並使用 application.properties 文件讀取我的應用程序的配置?
先感謝您!
您的 test/application.properties 也需要配置。 嘗試將此添加到您的測試應用程序屬性文件中
spring.datasource.url=jdbc:h2:mem:BBDD;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
如果你還不支持 h2,這給你的 pom。
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
檢查您的 application.properties
改變
spring.datasource.driver=com.mysql.jdbc.Driver
至
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.