I am trying to load an array from application.properties in a Spring boot 2.4 application whereby the array is distributed across multiple property files.
- application.properties
spring.cloud.routes[0].id=1
spring.cloud.routes[0].name=ONE
spring.cloud.routes[1].id=2
spring.cloud.routes[1].name=TWO
spring.config.import=one.properties
- one.properties
spring.cloud.routes[2].id=3
spring.cloud.routes[2].name=THREE
spring.cloud.routes[3].id=4
spring.cloud.routes[3].name=FOUR
- Java Beans
@Getter
@Setter
public class Route {
private String id;
private String name;
}
@Getter
@Setter
@ConfigurationProperties(prefix = "spring.cloud")
@Component
public class Routes {
private List<Route> routes;
}
On starting application, I am getting below error -
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target [Bindable@1591271 type = java.util.List<Route>, value = 'provided', annotations = array<Annotation>[[empty]]] failed:
Property: spring.cloud.routes[2].id
Value: 3
Origin: class path resource [one.properties] - 1:27
Reason: The elements [spring.cloud.routes[2].id,spring.cloud.routes[2].name,spring.cloud.routes[3].id,spring.cloud.routes[3].name] were left unbound.
Property: spring.cloud.routes[2].name
Value: THREE
Origin: class path resource [one.properties] - 2:29
Reason: The elements [spring.cloud.routes[2].id,spring.cloud.routes[2].name,spring.cloud.routes[3].id,spring.cloud.routes[3].name] were left unbound.
Property: spring.cloud.routes[3].id
Value: 4
Origin: class path resource [one.properties] - 3:27
Reason: The elements [spring.cloud.routes[2].id,spring.cloud.routes[2].name,spring.cloud.routes[3].id,spring.cloud.routes[3].name] were left unbound.
Property: spring.cloud.routes[3].name
Value: FOUR
Origin: class path resource [one.properties] - 4:29
Reason: The elements [spring.cloud.routes[2].id,spring.cloud.routes[2].name,spring.cloud.routes[3].id,spring.cloud.routes[3].name] were left unbound.
Action:
Update your application's configuration
Is there any way to make this work without having to explicitly mention one.properties file (and any other additional files besides application.properties file) in java annotations?
Regards
Jacob