Spring Boot切换PostgreSQL后启动报错:无法加载SQLServer驱动
问题:Spring Boot切换PostgreSQL后仍报SQL Server驱动错误
原本使用SQL Server的Spring Boot应用,切换到Docker Compose部署的PostgreSQL容器并集成Flyway后,执行docker-compose up时仍出现找不到SQL Server驱动的错误。
错误信息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.microsoft.sqlserver.jdbc.SQLServerDriver
相关配置
application.properties
spring.datasource.username=${db_user} spring.datasource.password=${db_pass} # Hibernate ddl auto (create, create-drop, validate, update) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect spring.datasource.url=jdbc:postgresql://${db_host}:5432/test spring.jpa.hibernate.ddl-auto = validate
docker-compose.yml
version: '2' services: app: image: 'app:latest' build: context: . container_name: app depends_on: - db environment: - ENV=local - db_user=test - db_pass=test - dh_host=db ports: - "5050:8080" db: image: 'postgres:13.1-alpine' container_name: db environment: - POSTGRES_USER=test - POSTGRES_PASSWORD=test
pom.xml
<description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
解决方案
问题出在以下几个地方,对应修复步骤:
修正环境变量拼写错误
docker-compose.yml里的dh_host=db是笔误,应该改为db_host=db。这个错误会导致application.properties中的${db_host}无法正确取值,Spring Boot可能 fallback 到旧的SQL Server残留配置,或无法识别PostgreSQL的JDBC URL。显式指定PostgreSQL驱动类
在application.properties中添加驱动类配置,避免Spring Boot自动识别出错:spring.datasource.driver-class-name=org.postgresql.Driver清理旧镜像与残留配置
- 检查项目中是否存在其他配置文件(如
application-local.properties)、系统属性或环境变量残留SQL Server相关配置 - 执行
docker-compose down --rmi all清理旧镜像,再用docker-compose up --build重新构建并启动容器,确保应用使用最新配置
- 检查项目中是否存在其他配置文件(如
内容的提问来源于stack exchange,提问作者Manish
相关产品推荐
相关产品推荐

