在 class 路径资源 [org/axonframework/springboot/autoconfig/AxonAutoConfiguration.class] 中定义名称 'commandGateway' 创建 bean 时出错

Error creating bean with name 'commandGateway' defined in class path resource [org/axonframework/springboot/autoconfig/AxonAutoConfiguration.class]

在我添加 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[=16=](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

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

@Autowired @Lazy
private Class class;

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

这里最重要的是要知道你正在使用哪个版本的 Spring 启动,我可以从堆栈跟踪中看到:2.6.0.

本周某处发布的这个版本添加了一项额外的检查,可以解释您遇到的问题,可以在 here.

中找到

引用他们的话:

Circular References Prohibited by Default

Circular references between beans are now prohibited by default. If your application fails to start due to a BeanCurrentlyInCreationException you are strongly encouraged to update your configuration to break the dependency cycle. If you are unable to do so, circular references can be allowed again by setting spring.main.allow-circular-references to true, or using the new setter methods on SpringApplication and SpringApplicationBuilder This will restore 2.5’s behaviour and automatically attempt to break the dependency cycle.

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

韩国,

对于 spring 启动 2.6.2 或更高版本,使用 axon 4.5.8 或更高版本,您的问题将消失。

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