Error creating bean with name 'dynamodbClient': Lookup method resolution failed

282 Views Asked by At

Java: 20.0

Springboot: 3.0.1

Dependency

<dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>aws-sdk-java</artifactId>
      <version>2.20.115</version>
      <scope>provided</scope>
 </dependency>

Service class

@Slf4j
@Service
public class DynamodbClient {

  private final DynamoDbClient dynamoDbClient;
  
  @Value("${amazon.aws.dynamodb.endpoint}")
  private String endpoint;

  @Value("${amazon.aws.dynamodb.region}")
  private String region;

  public DynamodbClient() {
    this.dynamoDbClient =
        DynamoDbClient.builder()
            .endpointOverride(URI.create(endpoint))
            .region(Region.of(region))
            .build();
  }
}

Note: Auth credential is not required. dynamodb is accessible through cli.

Context: Main database is Cassandra for this application and actual requirement should be implemented through an API call, but for some reason, we are not doing it, instead updating a record in dynamodb directly and it is once a while operation.

2

There are 2 best solutions below

0
On BEST ANSWER

The correct way of doing it is

@Configuration
public class DynamodbClient {
  
  @Value("${amazon.aws.dynamodb.endpoint}")
  private String endpoint;

  @Value("${amazon.aws.dynamodb.region}")
  private String region;

  @Bean
  public DynamodbClient dynamodbClient {
    return 
        DynamoDbClient.builder()
            .endpointOverride(URI.create(endpoint))
            .region(Region.of(region))
            .build();
  }
}

Now we can use it like

@Autowired private DynamodbClient dynamodbClient;

in service classes.

2
On

The class DynamodbClient should have a @Configuration annotation instead of @Service. In order to configure your bean definition correctly, you will have to use this annotation. Also, instance variable DynamoDbClient is not required.

The final code will be:

@Configuration
public class DynamodbClient {
  
  @Value("${amazon.aws.dynamodb.endpoint}")
  private String endpoint;

  @Value("${amazon.aws.dynamodb.region}")
  private String region;

  public DynamodbClient() {
    this.dynamoDbClient =
        DynamoDbClient.builder()
            .endpointOverride(URI.create(endpoint))
            .region(Region.of(region))
            .build();
  }
}

@Configuration is a class-level annotation indicating that an object is a source of bean definitions.

Please see this Spring documentation for more details.