Error in injecting commandgateway

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

By excluding the axon-server-connector it seems like you don’t want to use Axon Server.

You likely want to use a distributed command gateway, and thus distributed command bus through. For that you need one of two extensions, and set a property to disable the auto wiring for Axon Server. You can get the details from the reference guide.

Please let is know if that helped to get it working.

Thank you for answering. I just removed the exclusion part and it stills throw the same error

I just started the axon server too

java -jar AxonServer.jar

@Gerard please looking forward to resolving this problem once and for all. It has really hindered my progress.

My full pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.0.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.lawrence</groupId>
	<artifactId>digital-banking</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>digital-banking</name>
	<description>digital banking</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.axonframework</groupId>
			<artifactId>axon-spring-boot-starter</artifactId>
			<version>4.6.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

then I injected it here

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

import org.axonframework.commandhandling.gateway.CommandGateway;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lawrence.digitalbanking.command.dto.CreateAccountRequestDTO;
import com.lawrence.digitalbanking.coreapi.commands.createAccountCommand;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/commands/accounts")
@Slf4j
public class AccountCommandRestAPI {
	private CommandGateway commandGateway;
	public AccountCommandRestAPI(CommandGateway commandGateway) {
		this.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;
	}
	
}

Axon Framework is not Spring Boot 3 compatible yet. It should work by changing to 2.7.6. We expect the next release, 4.7.0., that is expected to be released in January to have Spring Boot 3 support.

thank you. Now I can continue learning axon.

1 Like