Springboot:需要一个找不到的“javax.persistence.EntityManagerFactory”类型的bean
Posted
技术标签:
【中文标题】Springboot:需要一个找不到的“javax.persistence.EntityManagerFactory”类型的bean【英文标题】:Springboot: Required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found 【发布时间】:2018-12-20 12:55:54 【问题描述】:我正在开发一个 Spring Boot 应用程序,在启动服务器时遇到了这个错误。我不确定我是否错误地定义了任何注释或缺少任何依赖项。任何帮助将不胜感激。
主类:
@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class DemoApplication
public static void main(String[] args)
System.out.println("Hello world");
SpringApplication.run(DemoApplication.class, args);
用户服务类:
@Service
public class UserService implements IUserService
@Autowired
private UserDAO userDAO;
@Override
public List<UserDTO> getAllUsers()
List<User> entities = userDAO.getUsers();
List<UserDTO> users = new ArrayList<UserDTO>();//Will never use directly User Object, will be using Tranferable objects
Iterator<User> iterator = entities.iterator();
while(iterator.hasNext())
User user = iterator.next();
users.add(ApiDTOBuilder.userToUserDTO(user));//We are building UserDTO object.
return users;
@Override
public UserDTO getUserByUsername(String username)
User user = userDAO.getUser(username);
return ApiDTOBuilder.userToUserDTO(user);
@Override
public void createUser(UserDTO user)
userDAO.createUser(ApiDTOBuilder.userDTOToUser(user));
@Override
public void updateUser(UserDTO user)
userDAO.updateUser(ApiDTOBuilder.userDTOToUser(user));
@Override
public void deleteUser(String username)
userDAO.deleteUser(username);
UserDAO 类:
@Repository 公共类 UserDAO 实现 IUserDAO
@PersistenceContext
EntityManager em;//JPA EntityManager is used to access a database in a particular application. It is used to manage persistent entity instances, to find entities by their primary key identity(As here is Username), and to query over all entities.
@Override
@Transactional
public User getUser(String username)
return em.find(User.class, username); //Here entitymanager can find the username as it has the capability to find an entity by unique identifies
@Override
@Transactional
public List<User> getUsers()
List<User> resultList = em.createQuery("FROM user_tbl", User.class).getResultList();
return resultList;
@Override
@Transactional
public void createUser(User user)
em.persist(user);//Make an instance managed and persistent.
@Override
@Transactional
public void updateUser(User user)
em.merge(user);//Merge the state of the given entity into the current persistence context.
@Override
@Transactional
public void deleteUser(String username)
User user = this.getUser(username);
em.remove(user);//Remove the entity instance.
Build.gradle:
buildscript
ext
springBootVersion = '2.0.0.RELEASE'
repositories
mavenCentral()
dependencies
classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories
mavenCentral()
dependencies
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final')
compile('javax.xml.bind:jaxb-api:2.2.11')
compile('org.springframework.boot:spring-boot-starter-test:2.0.2.RELEASE')
compile('com.sun.xml.bind:jaxb-core:2.2.11')
compile('com.sun.xml.bind:jaxb-impl:2.2.11')
runtime('org.springframework.boot:spring-boot-devtools')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
错误信息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userDAO in com.example.demo.service.UserService required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.
Action:
Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration.
我看到了所有相关的答案,并尝试实施这些建议,例如添加依赖项、在主类中添加符号等。但它显示了同样的错误。请有人帮助我。先感谢您。
P.S:我的应用程序的 Github 链接:https://github.com/heliOpenxCell/demo
【问题讨论】:
为什么application.properties中有这行:spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 这就是问题所在。另外,开始使用 Spring 数据。这比使用类似 sql 的查询创建自定义 DAO 要容易得多。使用 Spring Boot 时,您永远不必在任何地方注入实体管理器。 另外在主类和WebConfiguration类中有很多不必要的配置。另外还有多余的 gradle 依赖。你为什么不保留 Spring Initializer 正在创建的东西呢? 在 Spring Data 中,您永远不必显式配置 EntityManager。如果您这样做,则说明您错误地使用了整个库。也使用存储库模式。 DAO 模式已经过时了,而且已经远远超过了关系数据库的按日期销售。 如果我想创建持久上下文怎么办?如何创建并传入 Tasklet? 【参考方案1】:我遇到了同样的问题,只需将其添加到您的 maven 或 gradle,它对我有用!
马文:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>9.0.10</version>
</dependency>
分级:
compile group: 'org.apache.tomcat', name: 'tomcat-jdbc', version: '9.0.10'
【讨论】:
以上是关于Springboot:需要一个找不到的“javax.persistence.EntityManagerFactory”类型的bean的主要内容,如果未能解决你的问题,请参考以下文章
使用 spring r2dbc 时找不到 javax.persistence 注释
找不到工厂:javax.faces.context.FacesContextFactory
在 Open JDK 11(Redhat)上找不到 javax.smartcardio.*