【问题标题】:Error creating bean with name 'commandGateway' defined in class path resource [org/axonframework/springboot/autoconfig/AxonAutoConfiguration.class]在类路径资源 [org/axonframework/springboot/autoconfig/AxonAutoConfiguration.class] 中定义名称为“commandGateway”的 bean 创建错误
【发布时间】:2022-01-01 09:50:39
【问题描述】:

在我添加 spring-data-start-jpah2-database 依赖项之前,它会抛出这些错误。 我允许 Axon 的自动配置来做它的事情。

这是我得到的堆栈跟踪:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'postProductsController': Unsatisfied dependency expressed through method 'setCommandGateway' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'commandGateway' defined in class path resource [org/axonframework/springboot/autoconfig/AxonAutoConfiguration.class]: Unsatisfied dependency expressed through method 'commandGateway' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'axonServerCommandBus' defined in class path resource [org/axonframework/springboot/autoconfig/AxonServerAutoConfiguration.class]: Unsatisfied dependency expressed through method 'axonServerCommandBus' parameter 2; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'commandBus' defined in class path resource [org/axonframework/springboot/autoconfig/AxonAutoConfiguration.class]: Unsatisfied dependency expressed through method 'commandBus' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.axonframework.spring.config.AxonConfiguration': Cannot resolve reference to bean 'org.axonframework.config.Configurer' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'org.axonframework.config.Configurer': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments(AutowiredAnnotationBeanPostProcessor.java:767) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:719) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.13.jar:5.3.13]
    at 
    ... 77 common frames omitted

这是我创建的示例控制器:

@RestController
@RequestMapping(path = "/api/v1/products")
public class PostProductsController {

    private  CommandGateway commandGateway;

    @Autowired
    public void setCommandGateway(CommandGateway commandGateway) {
        this.commandGateway = commandGateway;
    }

    @PostMapping
    public String createProduct(@RequestBody CreateProductRestModel createProductRestModel){

        CreateProductCommand createProductCommand = CreateProductCommand.builder()
                .price(createProductRestModel.getPrice())
                .quantity(createProductRestModel.getQuantity())
                .title(createProductRestModel.getTitle())
                .productId(UUID.randomUUID().toString())
                .build();

        String returnValue;

        try{
            returnValue = commandGateway.sendAndWait(createProductCommand);
        } catch (Exception e){
            returnValue = e.getLocalizedMessage();
        }
        return returnValue;
    }
}

添加依赖后,它开始抛出这些错误:

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:355) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:227) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.isSingleton(AbstractBeanFactory.java:463) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.axonframework.spring.config.AbstractAnnotationHandlerBeanPostProcessor.postProcessAfterInitialization(AbstractAnnotationHandlerBeanPostProcessor.java:73) ~[axon-spring-4.5.4.jar:4.5.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:455) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1808) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.0.jar:2.6.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-2.6.0.jar:2.6.0]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) ~[spring-boot-2.6.0.jar:2.6.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.0.jar:2.6.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.0.jar:2.6.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[spring-boot-2.6.0.jar:2.6.0]
    at com.mrdiipo.productsservice.ProductsServiceApplication.main(ProductsServiceApplication.java:12) ~[classes/:na]

这是我的application.properties 文件:

server.port = 0

spring.application.name=products-service
eureka.client.service-url.defaultZone= http://localhost:8761/eureka
eureka.instance.instanceid=${spring.application.name}:${instanceId:${random.value}}

spring.datasource.url=jdbc:h2:file:~/products;AUTO_SERVER=true
spring.datasource.username=root
spring.datasource.password=XXXXX
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true

spring.datasource.generate-unique-name= false
spring.h2.console.path=/h2-console

spring.h2.console.settings.web-allow-others=true

【问题讨论】:

  • 你有一个配置类来配置你的 entityManagerFactory 吗?请提供一些代码,我想您可能会使用@Lazy Annotation。或者您想在流程未完成时使用自动装配的资源,例如。 G。在 main 方法中?

标签: spring-boot spring-data-jpa cqrs axon saga


【解决方案1】:

对于 spring boot 2.6.2 或更高版本,使用 axon 4.5.8 或更高版本,您的问题就会消失。

  <dependency>
     <groupId>org.axonframework</groupId>
     <artifactId>axon-spring-boot-starter</artifactId>
    <version>4.5.8</version>
 </dependency>

【讨论】:

    【解决方案2】:

    这里要知道的最重要的事情是您使用的是哪个版本的 Spring Boot,我可以从堆栈跟踪中看到:2.6.0。

    本周发布的这个版本增加了一个额外的检查,可以解释你的经历,可以在here找到。

    引用他们:

    默认禁止循环引用

    现在默认禁止 bean 之间的循环引用。如果您的应用程序由于 BeanCurrentlyInCreationException 而无法启动,强烈建议您更新配置以中断依赖循环。如果您无法这样做,可以通过将 spring.main.allow-circular-references 设置为 true 或在 SpringApplication 和 SpringApplicationBuilder 上使用新的 setter 方法再次允许循环引用。这将恢复 2.5 的行为并自动尝试中断依赖循环。

    在 Axon 方面,我们已经在努力解决这个问题,您可以在 here 找到解决此问题的 PR。 合并后,您可以期待一个带有此修复的次要版本,但如果您需要,请随时遵循 Spring 的建议,了解如何自行修复。

    韩国,

    【讨论】:

    • 哇!!经过数小时的调试,它就像魔术一样工作。我听从了春天的建议。我在 application.properties 文件中添加了“spring.main.allow-circular-references=true”。谢谢先生。
    【解决方案3】:

    问题无法理解,请与我们分享更多代码以获得详细答案。 看起来你的两个类是依赖的。正如 cmets 中提到的 mdh,@Lazy 可以解决您的问题。

    @Autowired @Lazy
    private Class class;
    

    更多:https://www.baeldung.com/circular-dependencies-in-spring

    【讨论】:

    • 非常感谢先生。老实说,这个错误有点令人惊讶。当我添加 spring-data-jpa 和 h2-database 时,这个错误就开始了。在我添加这些依赖项之前,它从 axon commandGateway 引发了一个错误。我会尽快添加来自轴突的错误。
    猜你喜欢
    • 2021-05-20
    • 2015-10-16
    • 2019-01-22
    • 2019-08-17
    • 2021-09-20
    • 2019-12-30
    • 2022-01-18
    相关资源
    最近更新 更多