Spring Boot启动失败:找不到entityManagerFactory Bean求助
解决Spring Boot中找不到
entityManagerFactory Bean的问题 我之前也碰到过一模一样的问题,大概率是扫描范围、依赖配置或者数据库连接的问题,咱们一步步来排查解决:
1. 先简化并修正启动类的注解
你的启动类同时标注了@SpringBootApplication和@EnableAutoConfiguration,但@SpringBootApplication已经包含了@EnableAutoConfiguration和@ComponentScan,重复标注可能引发配置冲突。另外,@EntityScan如果不指定扫描范围,只会默认扫描启动类所在包及其子包下的实体类,要是你的实体类在其他路径,就会直接扫描不到,进而导致entityManagerFactory无法初始化。
修改后的启动类可以参考这个写法:
package com.servers; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @SpringBootApplication // 替换成你实际的实体类所在包路径 @EntityScan(basePackages = "com.servers.server1.model") // 指定Repository接口所在包,用basePackages更直观,保留basePackageClasses也可以 @EnableJpaRepositories(basePackages = "com.servers.server1.dao") public class ServersApplication { public static void main(String[] args) { SpringApplication.run(ServersApplication.class, args); } }
2. 检查依赖是否完整
确保你的pom.xml里已经正确引入了Spring Data JPA和PostgreSQL驱动的核心依赖,这俩是JPA正常工作的基础:
<dependencies> <!-- Spring Boot Data JPA Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- PostgreSQL驱动 --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <!-- 其他必要依赖比如web starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
3. 验证数据库配置的正确性
你的配置文件里有几个细节需要确认:
spring.datasource.username:PostgreSQL默认超级用户是postgres,你写的是postgre,大概率是拼写错误,这会直接导致数据库连接失败,进而无法创建entityManagerFactory。- 密码
22ALGORITMul!(($包含特殊字符,建议用引号包裹,避免配置解析出错。 - 确认本地PostgreSQL服务(
localhost:5432)已经启动,ServersDataBase数据库已经提前创建。
调整后的配置片段:
spring.jpa.database=POSTGRESQL spring.datasource.platform=postgres spring.jpa.show-sql=true spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=create-drop spring.database.driverClassName=org.postgresql.Driver spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect spring.datasource.url=jdbc:postgresql://localhost:5432/ServersDataBase spring.datasource.username=postgres spring.datasource.password="22ALGORITMul!(($"
4. 确认Repository接口的定义
检查你的LinieDao是否正确继承了JpaRepository,示例如下:
package com.servers.server1.dao; import com.servers.server1.model.Linie; import org.springframework.data.jpa.repository.JpaRepository; public interface LinieDao extends JpaRepository<Linie, Long> { // 自定义查询方法(如果有) }
同时,确保LinieService中对LinieDao的注入方式正确,推荐用构造函数注入(比字段注入更稳定):
package com.servers.server1.service; import com.servers.server1.dao.LinieDao; import org.springframework.stereotype.Service; @Service public class LinieService { private final LinieDao db; public LinieService(LinieDao db) { this.db = db; } // 业务方法 }
5. 最后一招:排除自动配置冲突(如果以上都无效)
要是前面的步骤都试过还是不行,可能是自动配置类冲突,可以尝试在@SpringBootApplication中排除JPA相关的自动配置类,再手动调整:
@SpringBootApplication(exclude = {HibernateJpaAutoConfiguration.class})
按照上面的步骤排查后,应该能解决entityManagerFactory找不到的问题。
内容的提问来源于stack exchange,提问作者Cojocariu Flavius
相关产品推荐
相关产品推荐

