当前位置: 首页 > 知识库问答 >
问题:

应用程序上下文中一些bean的依赖关系形成了一个循环

穆铭晨
2023-03-14

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

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

存储库

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

A存储自定义

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

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);
    }
}

和一个服务AServiceImpl

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

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

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

Description:

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

|  aRepositoryImpl
└─────┘

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

请帮帮忙!

劳伦特

共有3个答案

公良阳波
2023-03-14

使用@Lazy注释将解决此问题

@Component
public class Bean1 {
    @Lazy
    @Autowired
    private Bean2 bean2;
}
曹鹏海
2023-03-14

对于您的原始问题,有一个简单的修复方法:只需从ARepositoryCustom和ARepositoryImpl中删除@Repository。保留所有命名和接口/类层次结构。他们都很好。

洪伟兆
2023-03-14

使用Lazy

打破循环的一个简单方法是让Spring延迟初始化其中一个bean。也就是说:它不会完全初始化bean,而是创建一个代理将其注入另一个bean。注入的bean只有在第一次需要时才会完全创建。

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

资料来源:https://www.baeldung.com/circular-dependencies-in-spring

 类似资料:
  • 我试图创建一个jwt与Spring安全,但当我启动程序,我采取这个错误从Spring: İt也是一个spring web程序,因此该程序与控制器一起工作。此控制器用于创建带有登录名的jwt。以下是我的authController类: 这里是WebServiceConfig类: 以下是UserDetailsService: 谢谢你的帮助阿莱尔迪

  • 几周前,我将Spring的版本从1.4.x.RELEASE升级到1.5.1.RELEASE。 突然之间,由于这个错误,启动Spring Boot服务成了一场斗争: “应用程序上下文中某些bean的依赖关系形成一个循环” 同样的代码,不同的版本 这很有趣,因为在我的本地环境(OSX)上,相同的代码通常都可以正常启动,但在Heroku上,在不同的部署上会随机失败(在类路径类解析上看起来是不同的顺序,由

  • 应用程序上下文中一些bean的依赖关系形成了一个循环: 我试过用@Lazy,但没用。 当我尝试将构造函数更改为setter时,它会给我错误,例如: 证券配置 UserServiceImpl JWTManager JWTManager是一个自定义类,负责生成新的JWT。它使用来自auth0的java jwt库。通用域名格式。我使用公钥/私钥对令牌进行签名。 AuthController用@RestC

  • 当我运行我的应用程序时,我得到了下面显示的错误,即应用程序上下文中一些bean的依赖关系形成了一个循环。我不确定问题到底是从哪里来的。对于Spring Boot/Spring Security性来说还是相对较新的,所以如果您能在我的代码上提供任何提示,我们将不胜感激。 UserServiceImpl 安全配置

  • 问题内容: 我有一个依赖关系如下: 当我部署一切正常时,这将拉下另一个引发ClassDefNotFound的依赖项。 我添加了两个依赖项,如下所示: 并且仍然面临着同样的问题,即:MVN带来下来不 我该如何解决? 编辑: 添加; 问题答案: 您可能有一个传递依赖项,另一个依赖项取决于您不需要的版本。 要获得所有直接和传递依赖关系的概述,请尝试: mvn依赖项:树 如果您发现同一依赖项的不同版本之间

  • 问题内容: 我有一个简单的节点应用程序,它对github上另一个应用程序具有单一依赖性。使用可以很好地安装依赖项,但是当我尝试在其中安装某些东西时,它说不可用。例如,github应用程序将Mongoose安装为依赖项。我认为该父应用程序可以访问该模块,因为它位于子模块中: 结构看起来像这样: 我是否只需要在父级应用程序中同时包含猫鼬作为依赖项,还是可以通过子级方式访问该模块? 问题答案: 我是否只