设置DynamoDB表名称时出现问题,来自application.properties文件

huangapple go评论62阅读模式
英文:

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,
            )
    }


}

huangapple
  • 本文由 发表于 2023年3月7日 17:04:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定