繁体   English   中英

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

[英]Spring Boot: Consider defining a bean named 'entityManagerFactory' in your configuration

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

注意:仅当将@Autowired注释放在IDeviceRepository iDeviceRepository;时,才会发生此错误IDeviceRepository iDeviceRepository; DeviceDao.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.abc.init.Application.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.RegisterController.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.abc.service.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.IRegisterService

package com.abc.util.interfaces;

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

com.abc.dao.DeviceDao.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

任何人都可以请教吗?

在spring-boot中,您不需要使用存储库注释来注释存储库类。

@Repository

您只需要在接口上扩展JPARepository,Spring-boot将负责其余的工作。 例如:

public interface YourRepository extends JpaRepository<YourDomain, Serializable> {

    YourDomain findBysomeparameter(Long parameter);

}

而且您不需要添加以下注释:

@EnableJpaRepositories
@EntityScan
@ComponentScan

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

我希望这将有所帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM