Skip to content

Instantly share code, notes, and snippets.

@manojahi
Last active November 14, 2022 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manojahi/7ae0dd614140ae64547fc2979fe8705f to your computer and use it in GitHub Desktop.
Save manojahi/7ae0dd614140ae64547fc2979fe8705f to your computer and use it in GitHub Desktop.
@Configuration
@EnableDynamoDBRepositories(basePackages = "com.example.repository")
public class DynamoDbConfig {
@Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDbEndpoint;
@Value("${amazon.aws.access-key}")
private String amazonAwsAccessKey;
@Value("${amazon.aws.secret-key}")
private String amazonAwsSecretKey;
@Value("${amazon.aws.region}")
private String region;
@Value("${spring.profiles.active:Unknown}")
private String activeProfile;
@Autowired
private Environment environment;
@Bean("amazonDynamoDB")
@Primary
public AmazonDynamoDB amazonDynamoDb() {
AmazonDynamoDB amazonDynamoDb
= new AmazonDynamoDBClient(amazonAwsCredentials());
if (!ObjectUtils.isEmpty(amazonDynamoDbEndpoint)) {
amazonDynamoDb.setEndpoint(amazonDynamoDbEndpoint);
}
return amazonDynamoDb;
}
@Bean
public AWSCredentials amazonAwsCredentials() {
return new BasicAWSCredentials(
amazonAwsAccessKey, amazonAwsSecretKey);
}
@Bean
public AwsCredentialsProvider amazonAwsCredentialsProvider() {
return StaticCredentialsProvider.create(
AwsBasicCredentials.create(amazonAwsAccessKey, amazonAwsSecretKey));
}
@Bean
public DynamoDBMapperConfig.TableNameOverride tableNameOverrider() {
return DynamoDBMapperConfig.TableNameOverride.withTableNamePrefix("");
}
@Bean
@Primary
public DynamoDBMapperConfig dynamoDBMapperConfig(DynamoDBMapperConfig.TableNameOverride tableNameOverrider) {
// Create empty DynamoDBMapperConfig builder
DynamoDBMapperConfig.Builder builder = new DynamoDBMapperConfig.Builder();
// Inject missing defaults from the deprecated method
builder.withTypeConverterFactory(DynamoDBTypeConverterFactory.standard());
builder.withTableNameResolver(new DynamodbTableNameResolver(region,activeProfile, environment));
// Inject the table name overrider bean
builder.withTableNameOverride(tableNameOverrider);
return builder.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment