Java 从DynamoDB表中创建@Bean是一种糟糕的做法吗?
我使用下面的代码在SpringBoot框架中创建配置bean 据我所知,AWS允许两种存储项目的方法。首先是使用DynamoDBMapper.save()方法,其次是直接获取表,然后对表使用PutItem方法Java 从DynamoDB表中创建@Bean是一种糟糕的做法吗?,java,amazon-web-services,spring-boot,amazon-dynamodb,Java,Amazon Web Services,Spring Boot,Amazon Dynamodb,我使用下面的代码在SpringBoot框架中创建配置bean 据我所知,AWS允许两种存储项目的方法。首先是使用DynamoDBMapper.save()方法,其次是直接获取表,然后对表使用PutItem方法 @Configuration public class DynamoDBConfig { @Value("${amazon.access.key}") private String awsAccessKey; @Value("${am
@Configuration
public class DynamoDBConfig
{
@Value("${amazon.access.key}")
private String awsAccessKey;
@Value("${amazon.access.secret-key}")
private String awsSecretKey;
@Value("${amazon.region}")
private String awsRegion;
@Value("${amazon.end-point.url}")
private String awsDynamoDBEndPoint;
/*client builder*/
public AmazonDynamoDB amazonDynamoDBClient()
{
return AmazonDynamoDBClientBuilder
.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(awsDynamoDBEndPoint,awsRegion))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, awsSecretKey)))
.build();
}
/*uses client builder to return DB mapper*/
@Bean
public DynamoDBMapper mapper()
{
return new DynamoDBMapper(amazonDynamoDBClient());
}
@Bean
@Qualifier("TABLENAME")
public Table getTable()
{
DynamoDB dynamoDB = new DynamoDB(amazonDynamoDBClient());
return dynamoDB.getTable("TABLENAME");
}
}
我首先在项目中单独使用DynamoDBMapper.save()方法,但随后我需要保存一个JSON数据类型,这只能使用Table.putItem()完成正因为如此,我发现现在需要创建两个bean——一个用于映射器,另一个用于我在@Repository类中使用的表,然后将项目放入表中
@Configuration
public class DynamoDBConfig
{
@Value("${amazon.access.key}")
private String awsAccessKey;
@Value("${amazon.access.secret-key}")
private String awsSecretKey;
@Value("${amazon.region}")
private String awsRegion;
@Value("${amazon.end-point.url}")
private String awsDynamoDBEndPoint;
/*client builder*/
public AmazonDynamoDB amazonDynamoDBClient()
{
return AmazonDynamoDBClientBuilder
.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(awsDynamoDBEndPoint,awsRegion))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, awsSecretKey)))
.build();
}
/*uses client builder to return DB mapper*/
@Bean
public DynamoDBMapper mapper()
{
return new DynamoDBMapper(amazonDynamoDBClient());
}
@Bean
@Qualifier("TABLENAME")
public Table getTable()
{
DynamoDB dynamoDB = new DynamoDB(amazonDynamoDBClient());
return dynamoDB.getTable("TABLENAME");
}
}
我的问题是:
使用这两种方法保存项或创建表外bean对象是一种糟糕的做法吗?我是否应该在每次存储对象时使用AmazonDynamoDB客户端在我的存储库中创建表实例?Beans除外,这里的一个不好的做法是在应用程序中使用持久IAM用户凭据。如果可能的话,您应该将IAM角色用于临时的、自动轮换的凭据。