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

16,597

In spring-boot, you don't need to annotate your repository class with the repository annotation.

@Repository

You just need to extend JPARepository on your interface and Spring-boot will take care of the rest. For Example:

public interface YourRepository extends JpaRepository<YourDomain, Serializable> {

    YourDomain findBysomeparameter(Long parameter);

}

And you don't need to add these annotations:

@EnableJpaRepositories
@EntityScan
@ComponentScan

Spring-boot does that automatically unless you're doing some configuration.

I hope this will help.

Share:
16,597

Related videos on Youtube

AK47
Author by

AK47

Updated on June 04, 2022

Comments

  • AK47
    AK47 almost 2 years

    I am receiving the following error when trying to start up my Spring Boot application:

    Note: This error only occurs when I put the @Autowired annotation over the IDeviceRepository iDeviceRepository; in the DeviceDao.java class

    I have yet to get the DeviceDao.java to persist to the database - but the entities are being created

    > *************************** 
    > 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.
    

    This is directory structure of the project:

    ├───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
    

    Stack Trace:

    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
    

    Can anyone please advise?

    • Kamil W
      Kamil W over 5 years
      Remove @EnableJpaRepositories, @EntityScan, @ComponentScan annotations and check if this exception appears. Springboot automaticly makes a lot of configurations and those annotations can has incorrect impact on it. Besides you should user @Service annotation on classes which do some business logic (like RegisterServiceImpl in your case). @Repository annotation is dedicated for classes responsible for fetching data from diffrent sources.
    • AK47
      AK47 over 5 years
      Unfortunatley it doesnt work - I think I need to explicitly use the 3 annotations due to the structure of the project. I've added the stack trace
    • Kamil W
      Kamil W over 5 years
      Can you also post application.properties file? Might be helpful.
    • AK47
      AK47 over 5 years
      @KamilW. ServiceImpl classes should have @Service instead of @Repository ?
    • AK47
      AK47 over 5 years
      Added application.properties
    • Kamil W
      Kamil W over 5 years
      Please take a look at this article: baeldung.com/spring-bean-annotations
    • M. Deinum
      M. Deinum over 5 years
      Put your Application in com.abc instead of com.abc.init. Remove all the annotations but @SpringBootApplication.
    • AK47
      AK47 over 5 years
      @M. Deinum when I move application to com.abc and remove annotations I get the following error: Description: Field emailService in com.abc.controller.RegisterController required a bean of type 'com.abc.service.EmailServiceImpl' that could not be found. Action: Consider defining a bean of type 'com.abc.service.EmailServiceImpl' in your configuration.
    • kj007
      kj007 over 5 years
      @AK47 can you remove DeviceDao repository as you already have IDeviceRepository with extending Curd or JPA, it has all crud methods so no need to define crud operations in it..if you need any custom then you can define same in your repository interface.
    • vanillaSugar
      vanillaSugar over 5 years
      Try to use IRegisterService in RegisterController class instead of RegisterServiceImpl. Like this: @Autowired IRegisterService registerService;
    • Haripriya
      Haripriya over 5 years
      If you are using maven, then (1). Delete the repositories in .m2 (2). Then recompile project and try build again. Reference
  • AK47
    AK47 over 5 years
    When I updated the interface to public interface IDeviceRepository extends JpaRepository<Device, Long> {} and remove the @Repository annotation it still throws the same error. As for the annotations - see my last comment on the question - the annotations are required currently because of the structure of the project, but even when moving the Application.java class to com.abc package and removing the annotation it's still failing to build
  • srinivas
    srinivas over 2 years
    Yes, the same issue I am observing. Is there any solution ?.
  • fsalazar_sch
    fsalazar_sch almost 2 years
    Stop coping the same solution without testing
  • Pawan Tiwari
    Pawan Tiwari almost 2 years
    @fsalazar_sch can you share the reference because this code is from my project that I've used.