【问题标题】:@Autowired annotation is not recognized for repository in spring boot applicationSpring Boot 应用程序中的存储库无法识别 @Autowired 注释
【发布时间】:2019-08-06 04:49:54
【问题描述】:

我创建了一个小型 Spring Boot 项目,它将进行 DB 交互,我正在使用 @Controller@Service@Repository

spring boot主类在父包com.bisnu.main下,那么controller、service和repository都在com.bisnu.main.service....这样的父包下。

一切都很好,但是当@Autowired 用于存储库时,它无法为存储库创建 bean,它给出了错误。

@EnableSwagger2
@RestController
@CrossOrigin(origins="*",allowedHeaders="*")
@RequestMapping("/TestController")
public class TestController {

    @Autowired
    private SourceService sourceService;

    @GetMapping("/getSourcelist")
    public List<SourceListDTO> getAllTelevisionSource(){
        List<SourceListDTO> televisionSource = null;

        televisionSource = sourceService.getTelevisionSource();
        return televisionSource;
    }

}

@Service
public class SourceService {

    @Autowired
    private TestRepository testRepo;

    public List<SourceListDTO> getTelevisionSource() {
        Pageable pageable = PageRequest.of(0, 100);
        List<SourceListDTO> list = testRepo.findSourceList(pageable);
        return list;
    } 
}

public interface TestRepository extends JpaRepository<MyTelevisionSource,Long> {
    @Query("select query")
    List<SourceListDTO> findSourceList(Pageable pageable);
}

我遇到了错误,

Field testRepo in com.tivo.extract.core.service.SourceService required a bean of type 'com.tivo.extract.core.repository.TestRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.tivo.extract.core.repository.TestRepository' in your configuration.

谁能指导一下,这里出了什么问题。

谢谢

【问题讨论】:

  • @Repository 在存储库类中使用
  • 我也试过了,同样的错误
  • 请分享项目结构。应用程序级别的@ComponentScan 会这样做。但是如果包结构是正确的,那么它应该可以工作。
  • @dassum, 项目结构正确 com.bisnu.main --> 父com.bisnu.main.controller com.bisnu.main.repository com.bisnu.main.service
  • 分享 @Query 。它实际上查询您使用的是哪个

标签: spring-boot spring-mvc autowired


【解决方案1】:

在类中添加@Repository注解

@Repository
public interface TestRepository extends JpaRepository<MyTelevisionSource,Long> {
   ...

并将以下注释添加到主类(标记为@SpringBootApplication)或任何@Configuration-标记的类

@SpringBootApplication
@EnableJpaRepositories(basePackages = {"com.example.repositories"})
@EntityScan("com.example.entities")
public class BootApplication {
    ...

【讨论】:

  • @Repository 添加到界面将无济于事。 Spring Data JPA 自动检测从通用 Spring Data Repository 接口扩展的接口。如果您遵循 Spring Boot 最佳实践,Spring Boot 将自动检测 Spring Data 存储库,无需添加 @EnableJpaRepositories 和/或 @EntityScan
  • Deinum,这是真的,因为默认情况下 @SpringBootApplication 注释包括所有内容,并且我的包结构遵循您所说的任何结构,但是我有一个查询我想创建一个类,该类将创建 entitymanagerFactory 并创建数据源,那一次我使用了这个注释,EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef="rcmEntityManagerFactory",transactionManagerRef="rcmTransactionManager") 在这种情况下,我有 2 个 JpaRepositories,,,这工作吗
  • 感谢大家的时间和cmets。
  • @mohan 有帮助吗?如果是,那么您可以接受答案
  • @NikolayShevchenko,是的,它有帮助,但在我的情况下......我创建了一个新类,我在其中保存了所有数据源创建类,并且我使用了 EnableJpaRepositories(entityManagerFactoryRef="rcmEntityManagerFactory ",transactionManagerRef="rcmTransactionManager")
猜你喜欢
  • 2022-12-19
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2017-05-27
  • 2017-04-23
  • 1970-01-01
相关资源
最近更新 更多