小编ami*_*esh的帖子

Spring JPA Data Repository无法为扩展CrudRepository的接口创建bean

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

  • 一个基本的用户域类(@Entity),
  • UserDao扩展的接口CrudRepository
  • 和服务层实现.

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

这是我得到的错误:

警告:org.springframework.web.context.support.AnnotationConfigWebApplicationContext - 在上下文初始化期间遇到异常 - 取消刷新尝试org.springframework.beans.factory.BeanCreationException:创建名为'UserController'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private edu.sjsu.services.UserService edu.sjsu.controllers.UserController.userService; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'userServiceImpl'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:edu.sjsu.models.UserDao edu.sjsu.services.UserServiceImpl.userDao; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[edu.sjsu.models.UserDao]类型的限定bean:期望至少有一个bean符合此依赖关系的autowire候选资格.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}在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> {}
Run Code Online (Sandbox Code Playgroud)

RootConfig.Java:

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

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 …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate spring-data spring-data-jpa

5
推荐指数
1
解决办法
8443
查看次数

标签 统计

hibernate ×1

java ×1

spring ×1

spring-data ×1

spring-data-jpa ×1