Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Java array in Kotlin for @PropertySource?

I am trying to configure @PropertySource for my Spring-based application.

In Java, I could do something like this:

@PropertySource(value = {"application.properties","other.properties" })

I tried arrayOf in Kotlin but I end up with a type mismatch:

@PropertySource(value = arrayOf("application.properties", "other.properties"))

What is the right way to go here?

like image 216
Grzegorz Piwowarek Avatar asked Dec 19 '22 08:12

Grzegorz Piwowarek


1 Answers

The value annotation parameter is handled in a special way in Kotlin (following its special handling in Java), and if it has an array type, Kotlin converts it to a vararg. Therefore, the correct syntax here is simply:

@PropertySource("application.properties", "other.properties")

If you do want to specify the parameter name explicitly, use the spread operator to expand the array into varargs:

@PropertySource(value = *arrayOf("application.properties", "other.properties"))

For any other array annotation parameter, you should simply use arrayOf() normally.

like image 127
yole Avatar answered Dec 26 '22 11:12

yole