Spring JPA Data Repository无法为扩展CrudRepository

时间:2016-04-01 09:58:53

标签: java spring hibernate spring-data spring-data-jpa

我在使用Spring JPA存储库时遇到问题。我创建了:

  • 基本用户域类(@Entity),
  • 扩展UserDao
  • 的界面CrudRepository
  • 和服务层实现。

当我运行项目时,由于UserDao的bean创建异常,它失败了。据我所知,Spring JPA存储库负责为此接口创建bean(因为它扩展了CrudRepository)并将其注入到需要的任何地方。

这是我得到的错误:

  

警告:   org.springframework.web.context.support.AnnotationConfigWebApplicationContext    - 上下文初始化期间遇到异常 - 取消刷新尝试   org.springframework.beans.factory.BeanCreationException:错误   创建名为'UserController'的bean:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private edu.sjsu.services.UserService   edu.sjsu.controllers.UserController.userService;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为'userServiceImpl'的bean:注入autowired   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:edu.sjsu.models.UserDao   edu.sjsu.services.UserServiceImpl.userDao;嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   找到[edu.sjsu.models.UserDao]类型的限定bean   依赖:预计至少有1个bean有资格成为autowire   这种依赖的候选人。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}   在   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我想如果使用Spring JPA我甚至不需要这个,但我仍然按照指南和教程创建了这个配置类。

@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;
    }   

}

如果您需要其他配置和java类来了解情况,请告诉我。

1 个答案:

答案 0 :(得分:7)

var list = employeeDictionary.Keys.ToList(); list.Sort(); Console.WriteLine("The Employee details according to EmpID is "); foreach(var temp in list) { Console.WriteLine(temp + " " + employeeDictionary[temp].empName + " " + employeeDictionary[temp].salary); } 中使用basePackages

EnableJpaRepositories

默认情况下,@Configuration @EnableJpaRepositories(basePackages = "edu.sjsu.models") @EnableTransactionManagement public class JpaConfig { ... } 将扫描Spring Data存储库的带注释配置类的包。因为,我猜,你的配置和存储库类在不同的包中,你应该告诉Spring Data JPA要扫描哪些基础包,以便找到JPA存储库。