【问题标题】:i am facing issues in using spring crud reprositry我在使用 spring crud reprositry 时遇到问题
【发布时间】:2017-12-20 22:44:29
【问题描述】:

在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“rabbitMqController”的bean时出错:通过字段“recordsReprositry”表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.rabbitmq.config.RecordsReprositry”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

【问题讨论】:

  • 请展示您定义存储库的方式。堆栈跟踪不足以帮助您
  • 包 com.rabbitmq.config;导入 java.util.UUID;导入 org.springframework.data.repository.CrudRepository;导入 org.springframework.stereotype.Repository; @Repository public interface RecordsReprositry extends CrudRepository{ public Records findById(UUID id); }
  • 您可以编辑您的问题以提供更多信息。将控制器和存储库代码粘贴到那里
  • 这些是带有自动装配和请求映射的注释 @RestController public class RabbitMqController { Autowired private RecordsReprositry recordsReprositry; RequestMapping(name="/btsocr", method=RequestMethod.GET) public String btsOCR() throws Exception { return "Working"; } }
  • 为什么要在别人的包命名空间中定义 bean?

标签: java spring spring-data


【解决方案1】:

从春季开始尝试 JPA... (DOC : http://docs.spring.io/spring-data/jpa/docs/current/reference/html/)

例子:

@Repository
public interface MyRepository extends JpaRepository<EntityName,Long> {
    // here you can write your query; example:
    EntityName findByAttribute(Type value);
    // or
    @Query("SELECT * FROM EntityName t WHERE t.ID=?1")
    EntityName findByID(Long id);
}

然后你可以在服务中使用这个存储库(你必须使用自动装配)

例子:

@Service
public class MyService{
    @Autowired 
    private MyRepository repo;

    // here you can call in a method your query
    public EntityName example() {
        EntityName e = repo.findByID((long)1);
        return e;
    }
}

重要提示:您必须仅在服务中使用它的存储库以及您必须在控制器中使用它的服务

【讨论】:

    【解决方案2】:

    看起来你是带注释的接口..而你应该把@repository放到它的实现类中。

    package com.rabbitmq.config;                                                                                  
    
    import java.util.UUID;
    
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Repository;
    
    
    @Repository
    public **interface** RecordsReprositry extends CrudRepository<Records, Long>{
    
        public Records findById(UUID id);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-22
      • 2020-12-23
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多