CommandGateway in springboot

390 Views Asked by At

I have this error that has made me not advance in the CQRS implementation in spring boot.

Err: Parameter 0 of constructor in com.lawrence.digitalbanking.command.controller.AccountCommandRestAPI required a bean of type 'org.axonframework.commandhandling.gateway.CommandGateway' that could not be found.

Here is my class code:`

@RestController
@RequestMapping("/commands/accounts")
@Slf4j
@AllArgsConstructor
public class AccountCommandRestAPI {
    private CommandGateway commandGateway;
    
    @PostMapping("/create")
    public CompletableFuture<String> newAccount(@RequestBody CreateAccountRequestDTO request) {
        log.info("CreateAccountRequestDTO =>"+request.getInitialBalance().toString());
        CompletableFuture<String> resp =  commandGateway.send(new createAccountCommand(
                UUID.randomUUID().toString(),
                request.getInitialBalance(),
                request.getCurrency()
                ));
        return resp;
    }
}

This is my pom.xml dependency

<dependency>
            <groupId>org.axonframework</groupId>
            <artifactId>axon-spring-boot-starter</artifactId>
            <version>4.6.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.axonframework</groupId>
                    <artifactId>axon-server-connector</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

I don't really know what I am missing. I have tried several times. My CommandGateway is not getting injected.

Please thanks in Advance.

I was expecting it to run but it throws that error despite the dependency being included in the pom.xml

1

There are 1 best solutions below

0
lapadets On

I think your CommandGateway bean is not registered in the Spring context.

Try

@Autowired
private CommandGateway commandGateway;

Or you can define it as a bean:

@Bean
public CommandGateway commandGateway(CommandBus commandBus) {
    return new DefaultCommandGateway(commandBus);
}