Spring @Value - Importer une liste à partir du fichier de propriétés
Dans ce tutoriel, nous allons vous montrer comment importer une «Liste» depuis un fichier de propriétés, via Spring EL@Value
Testé avec:
-
Printemps 4.0.6
-
JDK 1.7
Spring @Value and List
Dans Spring@Value
, vous pouvez utiliser la méthodesplit()
pour injecter la «Liste» sur une ligne.
config.properties
server.name=hydra,zeus server.id=100,102,103
AppConfigTest.java
package com.example.analyzer.test; import java.util.List; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration @PropertySource(value="classpath:config.properties") public class AppConfigTest { @Value("#{'${server.name}'.split(',')}") private Listservers; @Value("#{'${server.id}'.split(',')}") private List serverId; //To resolve ${} in @Value @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); } }
Sortie
System.out.println(servers.size()); for(String temp : servers){ System.out.println(temp); } System.out.println(serverId.size()); for(Integer temp : serverId){ System.out.println(temp); }
2 hydra zeus 3 100 102 103