当前位置: 首页 > 知识库问答 >
问题:

Spring Boot:考虑在配置中定义一个名为“entityManagerFactory”的bean

郜谦
2023-03-14

尝试启动Spring Boot应用程序时,我收到以下错误:

注意:仅当我将自动连线注释置于IDeviceRepository IDeviceRepository上方时,才会发生此错误 。java类

我还没有让DeviceDao.java持久化到数据库-但是正在创建实体

> *************************** 
> APPLICATION FAILED TO START
> ***************************
> 
> Description: 
> Field iDeviceRepository in com.abc.dao.DeviceDao required a bean named 'entityManagerFactory' that could not be found. 
> Action:
> Consider defining a bean named 'entityManagerFactory' in your configuration.

这是项目的目录结构:

├───src
│   ├───main
│   │   ├───java
│   │   │   └───com
│   │   │       └───abc
│   │   │           ├───controller
│   │   │           ├───dao
│   │   │           │   └───repositories
│   │   │           ├───init
│   │   │           ├───model
│   │   │           ├───service
│   │   │           └───util
│   │   │               ├───common
│   │   │               ├───enums
│   │   │               ├───exceptions
│   │   │               └───interfaces
│   │   └───resources
│   │       ├───static
│   │       │   ├───css
│   │       │   ├───fonts
│   │       │   ├───img
│   │       │   └───js
│   │       └───templates

com。美国广播公司。初始化。应用JAVA

package com.abc.init;

@SpringBootApplication
@EnableJpaRepositories("com.abc.dao.repositories")
@EntityScan(basePackages = { "com.abc.model" })
@ComponentScan(basePackages={ "com.abc.controller", "com.abc.service", "com.abc.dao" })
public class Application
{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

com.abc.controller.ontroller.java

package com.abc.controller;

@Controller
public class RegisterController
{
    @Autowired
    RegisterServiceImpl registerService;

    @GetMapping("/register")
    public String registerForm(Model model) {
        model.addAttribute("device", new Device());
        return "registerDevice";
    }

    @PostMapping("/register")
    public String registerSubmit(@ModelAttribute Device device) {
        registerService.registerDevice(device)
        return "registerDeviceSuccess";
    }
}

com。美国广播公司。服务RegisterServiceImpl。JAVA

package com.abc.service;

@Service
public class RegisterServiceImpl implements IRegisterService
{
    @Autowired
    DeviceDao devDao;

    public boolean registerDevice (Device device) {
        devDao.saveDevice(device);
        return true;
    }
}

com.abc.util.interfaces.注册服务

package com.abc.util.interfaces;

public interface IRegisterService
{
    public boolean registerDevice(Device device);
}

com。美国广播公司。道。设备DAO。JAVA

package com.abc.dao;

@Repository
public class DeviceDao 
{
    @Autowired
    IDeviceRepository iDeviceRepository;

    public Device saveDevice(Device device) {
        return iDeviceRepository.save(device);
    }
}

com.abc.dao.repositories.IDeviceRepository.java

package com.abc.dao.repositories;

@Repository
public interface IDeviceRepository extends CrudRepository<Device, Long> {}

application.properties

# Exposed HTTP Port
server.port = 8090

# Database Configuration Parameters
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=mydbadmin
spring.datasource.password=mydbpassword
spring.datasource.driver-class-name=org.postgresql.Driver

# Hibernate Configurations
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false

# Display SQL Commands in Terminal
spring.jpa.show-sql=true

堆栈跟踪:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registerController': Unsatisfied dependency expressed through field 'registerService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registerServiceImpl': Unsatisfied dependency expressed through field 'devDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deviceDao': Unsatisfied dependency expressed through field 'iDeviceRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IDeviceRepository': Cannot create inner bean '(inner bean)#36dce7ed' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36dce7ed': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registerServiceImpl': Unsatisfied dependency expressed through field 'devDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deviceDao': Unsatisfied dependency expressed through field 'iDeviceRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IDeviceRepository': Cannot create inner bean '(inner bean)#36dce7ed' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36dce7ed': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deviceDao': Unsatisfied dependency expressed through field 'iDeviceRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IDeviceRepository': Cannot create inner bean '(inner bean)#36dce7ed' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36dce7ed': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IDeviceRepository': Cannot create inner bean '(inner bean)#36dce7ed' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36dce7ed': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#36dce7ed': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

有人能指点一下吗?

共有2个答案

羿宏硕
2023-03-14

几个月前,当我开始学习Spring Boot时,我也遇到了类似的问题。我不确定Spring是否认真对待目录结构。我的目录结构与@Pawan相似。您所要做的就是将存储库文件夹向上移动一级,即将其移动到父abc文件夹中。用@服务为DeviceDao类添加注释。最后在@EnableJpaRepository中更新存储库路径。这对我有用。希望这有帮助。

殳智志
2023-03-14

在spring boot中,您不需要使用repository注释来注释repository类。

@Repository

您只需要在接口上扩展JPARepository,剩下的就由Spring boot来处理了。例如:

public interface YourRepository extends JpaRepository<YourDomain, Serializable> {

    YourDomain findBysomeparameter(Long parameter);

}

您不需要添加这些注释:

@EnableJpaRepositories
@EntityScan
@ComponentScan

除非您正在进行一些配置,否则Spring boot会自动执行此操作。

我希望这会有所帮助。

 类似资料:
  • 角色JAVA 任务JAVA 使用者JAVA RoleRepository.java TaskRepository。JAVA 用户库 登录注册应用程序。JAVA application.properties http://maven.apache.org/xsd/maven-4.0.0.xsd" 波姆。xml [错误]测试运行:1,失败:0,错误:1,跳过:0,耗时:2.941秒

  • 我从以下链接下载了示例代码:https://spring.io/guides/gs/accessing-data-mysql/我更改了应用程序。属性文件如下: 之后,我转到源文件夹并运行以下命令:mvn clean spring boot:run发生此错误: 请帮我解决这个问题

  • 我正在Spring中开发一个应用程序,使用Tomcat、Mysql5、Java8。。。问题是,由于“required bean‘entityManagerFactory’not found”问题,我无法部署它。我与同事一起开发了这个项目,但他们可以完美地部署它,即使我在Spring工具套件中复制粘贴相同的项目。怎么可能呢?错误: 这是我的pom.xml 这是我的存储库 这是我的控制器 应用属性 实

  • 尝试运行我的Spring启动应用程序,我的编译器说;“_worldcom.example.hello字段回购。UserService需要一个名为entityManagerFactory的bean,但找不到。 注入点有以下注释:-@org.springframework.beans.factory.annotation.自动生成(必需=true)" Spring启动的新手,所以我不太确定为什么我的项

  • 我只是在学习如何通过spring上传文档。所以,在进行这项工作时,我面临着上述问题。由于有许多问题与同一个问题相关,我参考了它们并尝试了所有方法,但没有一个问题得到纠正。事先,我不需要为我的存储库接口添加@Repository,也不需要在我的应用程序类中添加@EnableJpaRepositories,因为我已经使用了正确的包序列。如需更多参考,请参阅https://dzone.com/artic

  • 感谢您的帮助! 我有一个spring boot应用程序要连接到couchbase,详细信息如下 > 存储库接口 控制器类 Couchbase配置 当我尝试启动应用程序时,它会抛出以下错误,我尝试在这里和外面搜索许多文章,但它们没有帮助 任何帮助都将不胜感激,否则情况似乎很好。