Java 应用上下文中部分bean的依赖形成一个循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41608705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:31:16  来源:igfitidea点击:

The dependencies of some of the beans in the application context form a cycle

javaspringspring-bootspring-data-jpa

提问by Laurent Maillet

I'm working on a Spring Boot v1.4.2.RELEASE application with JPA.

我正在使用 JPA 开发 Spring Boot v1.4.2.RELEASE 应用程序。

I defined repository interfaces and implementations

我定义了存储库接口和实现

ARepository

存储库

@Repository
public interface ARepository extends CrudRepository<A, String>, ARepositoryCustom, JpaSpecificationExecutor<A> {
}

ARepositoryCustom

ARepositoryCustom

@Repository
public interface ARepositoryCustom {
    Page<A> findA(findAForm form, Pageable pageable);
}

ARepositoryImpl

ARepositoryImpl

@Repository
public class ARepositoryImpl implements ARepositoryCustom {
    @Autowired
    private ARepository aRepository;
    @Override
    public Page<A> findA(findAForm form, Pageable pageable) {
        return aRepository.findAll(
                where(ASpecs.codeLike(form.getCode()))
                .and(ASpecs.labelLike(form.getLabel()))
                .and(ASpecs.isActive()),
                pageable);
    }
}

And a service AServiceImpl

和一个服务 AServiceImpl

@Service
public class AServiceImpl implements AService {
    private ARepository aRepository;
    public AServiceImpl(ARepository aRepository) {
        super();
        this.aRepository = aRepository;
    }
    ...
}

My application won't start with the message :

我的应用程序不会以以下消息开头:

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

|  aRepositoryImpl
└─────┘

I followed all steps discribed in http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behaviour

我遵循了http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behaviour 中描述的所有步骤

Please help !

请帮忙 !

Laurent

洛朗

采纳答案by Andrei Pietrusel

There's a simple fix for your original problem: Just remove @Repository from ARepositoryCustom and from ARepositoryImpl. Keep all the naming and interface/class hierarchies. They are all OK.

您的原始问题有一个简单的解决方法:只需从 ARepositoryCustom 和 ARepositoryImpl 中删除 @Repository。保留所有命名和接口/类层次结构。他们都没事。

回答by Phat H. VU

I've tested your source code, and found something tricky.

我已经测试了你的源代码,发现了一些棘手的问题。

First, with your source code, I got the following error:

首先,使用您的源代码,我收到以下错误:

There is a circular dependency between 1 beans in the application context:
- ARepositoryImpl (field private test.ARepository test.ARepositoryImpl.aRepository)
- aRepositoryImpl

Then, I guess Spring 'confused' between ARepository(JPA repository) and ARepositoryImpl(Custom repository). So, I would suggest you renameARepositoryto something else, such as BRepository. It worked if I renamed the class name.

然后,我猜 Spring 在ARepository(JPA 存储库)和ARepositoryImpl(自定义存储库)之间“混淆”了。因此,我建议您重命名ARepository为其他名称,例如BRepository. 如果我重命名类名,它会起作用。

According to offcial documentation of Spring Data (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/) :

根据 Spring Data 的官方文档(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/):

These classes need to follow the naming convention of appending the namespace element's attribute repository-impl-postfix to the found repository interface name. This postfix defaults to Impl

这些类需要遵循将命名空间元素的属性 repository-impl-postfix 附加到找到的存储库接口名称的命名约定。这个后缀默认为 Impl