1. Overview
1.概述
In this quick tutorial, we’re going to learn how to inject values into an array or List from a Spring properties file.
在这个快速教程中,我们将学习如何从Spring属性文件中向数组或List注入值。
2. Default Behavior
2.默认行为
We’ll start with a simple application.properties file:
我们将从一个简单的application.properties文件开始。
arrayOfStrings=Baeldung,dot,com
Let’s see how Spring behaves when we set our variable type to String[]:
让我们看看当我们将变量类型设置为String[]时,Spring的表现如何。
@Value("${arrayOfStrings}")
private String[] arrayOfStrings;
@Test
void whenContextIsInitialized_thenInjectedArrayContainsExpectedValues() {
assertArrayEquals(new String[] {"Baeldung", "dot", "com"}, arrayOfStrings);
}
We can see that Spring correctly assumes our delimiter is a comma and initializes the array accordingly.
我们可以看到,Spring正确地假定我们的分隔符是逗号,并相应地初始化了数组。
We should also note that, by default, injecting an array works correctly only when we have comma-separated values.
我们还应该注意到,默认情况下,只有当我们有逗号分隔的值时,注入数组才能正确工作。
3. Injecting Lists
3.注入清单
If we try to inject a List in the same way, we’ll get a surprising result:
如果我们试图以同样的方式注入一个List,我们会得到一个令人惊讶的结果。
@Value("${arrayOfStrings}")
private List<String> unexpectedListOfStrings;
@Test
void whenContextIsInitialized_thenInjectedListContainsUnexpectedValues() {
assertEquals(Collections.singletonList("Baeldung,dot,com"), unexpectedListOfStrings);
}
Our List contains a single element, which is equal to the value we set in our properties file.
我们的List包含一个元素,它等于我们在属性文件中设置的值。
In order to properly inject a List, we need to use a special syntax called Spring Expression Language (SpEL):
为了正确地注入一个List,我们需要使用一种特殊的语法,称为Spring Expression Language(SpEL)。
@Value("#{'${arrayOfStrings}'.split(',')}")
private List<String> listOfStrings;
@Test
void whenContextIsInitialized_thenInjectedListContainsExpectedValues() {
assertEquals(Arrays.asList("Baeldung", "dot", "com"), listOfStrings);
}
We can see that our expression starts with # instead of the $ that we’re used to with @Value.
我们可以看到,我们的表达式以#开始,而不是我们习惯的$与@Value。
We should also note that we’re invoking a split method, which makes the expression a bit more complex than a usual injection.
我们还应该注意,我们正在调用一个split方法,这使得表达式比通常的注入要复杂一些。。
If we’d like to keep our expression a bit simpler, we can declare our property in a special format:
如果我们想让我们的表达式更简单一些,我们可以用一种特殊的格式声明我们的属性。
listOfStrings={'Baeldung','dot','com'}
Spring will recognize this format, and we’ll be able to inject our List using a somewhat simpler expression:
Spring会识别这种格式,我们就可以用一个更简单的表达式来注入我们的List。
@Value("#{${listOfStrings}}")
private List<String> listOfStringsV2;
@Test
void whenContextIsInitialized_thenInjectedListV2ContainsExpectedValues() {
assertEquals(Arrays.asList("Baeldung", "dot", "com"), listOfStringsV2);
}
4. Using Custom Delimiters
4.使用自定义定界符
Let’s create a similar property, but this time, we’re going to use a different delimiter:
让我们创建一个类似的属性,但这一次,我们将使用一个不同的分隔符。
listOfStringsWithCustomDelimiter=Baeldung;dot;com
As we’ve seen when injecting Lists, we can use a special expression where we can specify our desired delimiter:
正如我们在注入Lists时看到的,我们可以使用一个特殊的表达式,在这里我们可以指定我们想要的分隔符。
@Value("#{'${listOfStringsWithCustomDelimiter}'.split(';')}")
private List<String> listOfStringsWithCustomDelimiter;
@Test
void whenContextIsInitialized_thenInjectedListWithCustomDelimiterContainsExpectedValues() {
assertEquals(Arrays.asList("Baeldung", "dot", "com"), listOfStringsWithCustomDelimiter);
}
5. Injecting Other Types
5.注入其他类型
Let’s take a look at the following properties:
让我们来看看以下属性。
listOfBooleans=false,false,true
listOfIntegers=1,2,3,4
listOfCharacters=a,b,c
We can see that Spring supports basic types out-of-the-box, so we don’t need to do any special parsing:
我们可以看到Spring支持开箱即用的基本类型,所以我们不需要做任何特殊的解析。
@Value("#{'${listOfBooleans}'.split(',')}")
private List<Boolean> listOfBooleans;
@Value("#{'${listOfIntegers}'.split(',')}")
private List<Integer> listOfIntegers;
@Value("#{'${listOfCharacters}'.split(',')}")
private List<Character> listOfCharacters;
@Test
void whenContextIsInitialized_thenInjectedListOfBasicTypesContainsExpectedValues() {
assertEquals(Arrays.asList(false, false, true), listOfBooleans);
assertEquals(Arrays.asList(1, 2, 3, 4), listOfIntegers);
assertEquals(Arrays.asList('a', 'b', 'c'), listOfCharacters);
}
This is only supported via SpEL, so we can’t inject an array in the same way.
这只支持通过SpEL,所以我们不能以同样的方式注入一个数组。
6. Reading Properties Programmatically
6.以编程方式读取属性
In order to read properties programmatically, we first need to get the instance of our Environment object:
为了以编程方式读取属性,我们首先需要获得我们的Environment对象的实例。
@Autowired
private Environment environment;
Then we can simply use the getProperty method to read any property by specifying its key and expected type:
然后我们可以简单地使用getProperty方法,通过指定其键和预期类型来读取任何属性。
@Test
void whenReadingFromSpringEnvironment_thenPropertiesHaveExpectedValues() {
String[] arrayOfStrings = environment.getProperty("arrayOfStrings", String[].class);
List<String> listOfStrings = (List<String>)environment.getProperty("arrayOfStrings", List.class);
assertArrayEquals(new String[] {"Baeldung", "dot", "com"}, arrayOfStrings);
assertEquals(Arrays.asList("Baeldung", "dot", "com"), listOfStrings);
}
7. Conclusion
7.结语
In this article, we learned how to easily inject arrays and Lists through quick and practical examples.
在这篇文章中,我们通过快速实用的例子,了解了如何轻松注入数组和Lists。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。