В этом уроке мы покажем вам, как импортировать “Список” из файла свойств с помощью SpringEL @Value
Протестировано с:
- Весна 4.0.6
- JDK 1.7
Весна @Значение и список
В Spring @Value
вы можете использовать метод split()
, чтобы вставить “Список” в одну строку.
server.name=hydra,zeus server.id=100,102,103
package com.mkyong.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(); } }
Выход
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
Рекомендации
Оригинал: “https://mkyong.com/spring/spring-value-import-a-list-from-properties-file/”