从 application.yml 加载成对数组
Load array of pairs from application.yml
任务是从 application.yml 文件中填充对列表。
所以在我的科特林代码中我有这样的东西:
@Component
class LocalImageStore(
@Value("${images.thumbnails_sizes}")
private val thumbnailsSizes: List<Pair<Int, Int>>
) : ImageStore
{
// unrelated code here
}
在 application.yml
文件中,我有以下内容:
images:
dir:
path: images
thumbnails_sizes:
- [150, 150]
- [200, 200]
- [400, 450]
所以我预计我的 thumbnailsSizes
将包含 .yml 文件中的对列表,但我看到的只是错误消息 Could not resolve placeholder 'images.thumbnails_sizes' in value "${images.thumbnails_sizes}"n
我没有找到在 .yml 文件中存储对列表的方法,所以请告知如何以正确的方式进行操作。
尝试以下方法:
images:
dir:
path: images
thumbnails_sizes:
- 150: 150
- 200: 200
- 400: 450
或
images:
dir:
path: images
thumbnails_sizes:
- { 150: 150 }
- { 200: 200 }
- { 400: 450 }
直接使用配置 class 而不是 @Value
。假设您对这些属性具有以下 classes:
@ConstructorBinding
@ConfigurationProperties(prefix = "images")
class ImagesConfiguration(@field:NestedConfigurationProperty val thumbnailsSizes: List<ThumbnailSize>)
data class ThumbnailSize(val width: Int, val height: Int)
然后将 LocalImageStore
更改为以下内容:
@Component
class LocalImageStore(private val imagesConfiguration: ImagesConfiguration) : ImageStore {
// just use imagesConfiguration.thumbnailsSizes were needed
// unrelated code here
}
您可以在 YAML 中轻松配置,如下所示:
images:
dir:
path: images
thumbnails-sizes:
- width: 150
height: 150
- width: 200
height: 200
- width: 400
height: 450
任务是从 application.yml 文件中填充对列表。 所以在我的科特林代码中我有这样的东西:
@Component
class LocalImageStore(
@Value("${images.thumbnails_sizes}")
private val thumbnailsSizes: List<Pair<Int, Int>>
) : ImageStore
{
// unrelated code here
}
在 application.yml
文件中,我有以下内容:
images:
dir:
path: images
thumbnails_sizes:
- [150, 150]
- [200, 200]
- [400, 450]
所以我预计我的 thumbnailsSizes
将包含 .yml 文件中的对列表,但我看到的只是错误消息 Could not resolve placeholder 'images.thumbnails_sizes' in value "${images.thumbnails_sizes}"n
我没有找到在 .yml 文件中存储对列表的方法,所以请告知如何以正确的方式进行操作。
尝试以下方法:
images:
dir:
path: images
thumbnails_sizes:
- 150: 150
- 200: 200
- 400: 450
或
images:
dir:
path: images
thumbnails_sizes:
- { 150: 150 }
- { 200: 200 }
- { 400: 450 }
直接使用配置 class 而不是 @Value
。假设您对这些属性具有以下 classes:
@ConstructorBinding
@ConfigurationProperties(prefix = "images")
class ImagesConfiguration(@field:NestedConfigurationProperty val thumbnailsSizes: List<ThumbnailSize>)
data class ThumbnailSize(val width: Int, val height: Int)
然后将 LocalImageStore
更改为以下内容:
@Component
class LocalImageStore(private val imagesConfiguration: ImagesConfiguration) : ImageStore {
// just use imagesConfiguration.thumbnailsSizes were needed
// unrelated code here
}
您可以在 YAML 中轻松配置,如下所示:
images:
dir:
path: images
thumbnails-sizes:
- width: 150
height: 150
- width: 200
height: 200
- width: 400
height: 450