如何在Kotlin中为@PropertySource创建Java数组?
我正在尝试为我的基于Spring的应用程序配置@PropertySource。
在Java中,我可以做这样的事情:
@PropertySource(value = {"application.properties","other.properties" })
我在Kotlin中尝试过arrayOf
,但是最后我输入了一个类型不匹配的结果:
@PropertySource(value = arrayOf("application.properties", "other.properties"))
什么是正确的方式去这里?
value
注释参数在Kotlin中以特殊方式处理(在Java中进行特殊处理之后),如果它具有数组类型,则Kotlin将其转换为vararg
。 因此,这里正确的语法很简单:
@PropertySource("application.properties", "other.properties")
如果您确实要显式指定参数名称,请使用spread运算符将数组展开为可变参数:
@PropertySource(value = *arrayOf("application.properties", "other.properties"))
对于任何其他数组注释参数,您应该只是简单地使用arrayOf()
。