报错信息如下

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private bupt.xrh.UserRepository bupt.xrh.UserServiceImpl.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [bupt.xrh.UserRepository] 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)}
  

 

原因:

自动注册bean时,持久层的bean没有生成,这个bean是一个接口,没有实现类,这个是spring JPA的高级特性, 但需要一个配置声明

使用代理模式,自动生成实现类

 在 Spring 配置文件中启用扫描并自动创建代理的功能

                
 <-- 需要在 <beans> 标签中增加对 jpa 命名空间的引用 --> 
 <jpa:repositories base-package="footmark.springdata.jpa.dao"
 entity-manager-factory-ref="entityManagerFactory" 
 transaction-manager-ref="transactionManager"/>