Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JPA Data Repository failed to create bean for interface that extends CrudRepository

I am facing problems working with Spring JPA Repositories. I have created:

  • a basic user domain class (@Entity),
  • an interface UserDao which extends CrudRepository
  • and a service layer implementation.

When I run the project, it fails because of bean creation exception of UserDao. As far as I understand it is the responsibility of Spring JPA repositories to create the bean for this interface (because it extends CrudRepository) and inject it wherever it is required.

This is the error I am getting:

WARN : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private edu.sjsu.services.UserService edu.sjsu.controllers.UserController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: edu.sjsu.models.UserDao edu.sjsu.services.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [edu.sjsu.models.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)

UserDao.Java:

package edu.sjsu.models;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;

public interface UserDao extends CrudRepository<User, Long> {}

RootConfig.Java:

@Configuration
@ComponentScan(basePackages={"edu.sjsu"}, excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
@Import(JpaConfig.class)
public class RootConfig {}

JpaConfig.java I think I don't even need this if using Spring JPA but still i have created this config class following guides and tutorials.

@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class JpaConfig {

    @Bean
    public DataSource dataSource() {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return builder.setType(EmbeddedDatabaseType.HSQL).build();
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("edu.sjsu");
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();

        return factory.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {

        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }   

}

Let me know if you require other config and java classes to understand the situation.

like image 720
amit rakesh Avatar asked Apr 01 '16 09:04

amit rakesh


People also ask

Does JpaRepository extends CrudRepository?

It extends both CrudRepository and PagingAndSortingRepository. To perform CRUD operations, define repository extending CrudRepository. To perform CRUD as well as batch operations, define repository extends JpaRepository.

What is difference between JpaRepository and CrudRepository?

CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

What is a CrudRepository?

CrudRepository is a Spring Data interface for generic CRUD operations on a repository of a specific type. It provides several methods out of the box for interacting with a database.


1 Answers

Use basePackages in EnableJpaRepositories:

@Configuration
@EnableJpaRepositories(basePackages = "edu.sjsu.models")
@EnableTransactionManagement
public class JpaConfig { ... }

By default, EnableJpaRepositories will scan the package of the annotated configuration class for Spring Data repositories. Since, i'm guessing, your configuration and repository classes are in different packages, you should tell Spring Data JPA which base packages to scan in order to find JPA repositories.

like image 107
Ali Dehghani Avatar answered Oct 04 '22 22:10

Ali Dehghani