英文:
Problems setting DynamoDB table name from application.properties file
问题
// 我在StackOverflow的某处看到一个回复,可以像这样动态设置表名:
@DynamoDBTable(tableName = "${dynamodb.mentions}")
data class MentionItem (
@DynamoDBHashKey(attributeName = "mentioned_user")
var mentionedUserId: String = "",
@DynamoDBRangeKey(attributeName = "message_id")
var messageId: String = "",
@DynamoDBAttribute(attributeName = "mentioned_tag")
var mentionedTag: String? = null
)
// 需要补充的是,当我尝试在另一个类中像这样访问表名时,Spring会从我的属性文件中获取值:
@Value("${dynamodb.mentions}")
lateinit var mentionsTableName: String
Spring会从属性文件中获取值,但从DynamoDBTable
注释中不起作用。
是否有办法从application.properties
文件中设置表名?
英文:
I have seen a reply somewhere on StackOverflow that you can set a table name dynamically like this:
@DynamoDBTable(tableName = "${dynamodb.mentions}")
data class MentionItem (
@DynamoDBHashKey(attributeName = "mentioned_user")
var mentionedUserId: String = "",
@DynamoDBRangeKey(attributeName = "message_id")
var messageId: String = "",
@DynamoDBAttribute(attributeName = "mentioned_tag")
var mentionedTag: String? = null
)
Need to add that when I tried to access table name in another class like this, spring picks up the value from my properties file:
@Value("${dynamodb.mentions}")
lateinit var mentionsTableName: String
Spring picks up the value from the properties file, but the same doesn't work from the DynamoDBTable
annotation.
Is there a way to set a table name from the application.properties
file
答案1
得分: 1
到目前为止,我只找到了这种方法来实现它,那就是使用mapperConfig:
DynamoDBMapperConfig.Builder().withTableNameOverride(TableNameOverride.withTableNameReplacement(yourTableName)).build()
在我进行DynamoDB发布的服务中:
@Service
class MessageService() {
@Value("${dynamodb.mentions}")
lateinit var mentionsTableName: String
fun postMentions(messageId: String, mentionedEmployeeId: String, mentionedTag: String) {
val mapper = DynamoDBMapper(dynamoConfiguration.amazonDynamoDB())
val mapperConfig =
DynamoDBMapperConfig.Builder().withTableNameOverride(TableNameOverride.withTableNameReplacement(mentionsTableName))
.build()
mapper.save(
MentionDevItem(
mentionedUserId = it.mentionedEmployeeId,
messageId = messageId,
mentionedTag = it.mentionedTag,
),
mapperConfig,
)
}
}
英文:
So far I have only found this way to do it and it is to use mapperConfig:
DynamoDBMapperConfig.Builder().withTableNameOverride(TableNameOverride.withTableNameReplacement(yourTableName)).build()
In the service where I do the post to Dynamo:
@Service
class MessageService() {
@Value("${dynamodb.mentions}")
lateinit var mentionsTableName: String
fun postMentions(messageId: String, mentionedEmployeeId: String, mentionedTag: String) {
val mapper = DynamoDBMapper(dynamoConfiguration.amazonDynamoDB())
val mapperConfig =
DynamoDBMapperConfig.Builder().withTableNameOverride(TableNameOverride.withTableNameReplacement(mentionsTableName))
.build()
mapper.save(
MentionDevItem(
mentionedUserId = it.mentionedEmployeeId,
messageId = messageId,
mentionedTag = it.mentionedTag,
),
mapperConfig,
)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论