1. Overview
1.概述
In this quick tutorial, we’re going to have a look at the @Value Spring annotation.
在这个快速教程中,我们将看一下@Value Spring注解。
This annotation can be used for injecting values into fields in Spring-managed beans, and it can be applied at the field or constructor/method parameter level.
该注解可用于向Spring管理的Bean中的字段注入值,它可以在字段或构造函数/方法参数级别应用。
2. Setting Up the Application
2.设置应用程序
To describe different kinds of usage for this annotation, we need to configure a simple Spring application configuration class.
为了描述这个注解的不同种类的用法,我们需要配置一个简单的Spring应用配置类。
Naturally, we’ll need a properties file to define the values we want to inject with the @Value annotation. And so, we’ll first need to define a @PropertySource in our configuration class — with the properties file name.
当然,我们需要一个属性文件来定义我们想用@Value注解注入的值。因此,我们首先需要在我们的配置类中定义一个@PropertySource–带有属性文件名。
Let’s define the properties file:
我们来定义一下属性文件。
value.from.file=Value got from the file
priority=high
listOfValues=A,B,C
3. Usage Examples
3.使用实例
As a basic and mostly useless example, we can only inject “string value” from the annotation to the field:
作为一个基本的和大部分无用的例子,我们只能从注解中注入 “字符串值 “到字段。
@Value("string value")
private String stringValue;
Using the @PropertySource annotation allows us to work with values from properties files with the @Value annotation.
使用@PropertySource 注解允许我们使用@Value 注解处理来自属性文件的值。
In the following example, we get Value got from the file assigned to the field:
在下面的例子中,我们得到从文件中得到的值分配给该字段。
@Value("${value.from.file}")
private String valueFromFile;
We can also set the value from system properties with the same syntax.
我们也可以用同样的语法从系统属性中设置该值。
Let’s assume that we have defined a system property named systemValue:
让我们假设我们已经定义了一个名为systemValue的系统属性。
@Value("${systemValue}")
private String systemValue;
Default values can be provided for properties that might not be defined. Here, the value some default will be injected:
可以为可能没有被定义的属性提供默认值。在这里,值some default将被注入。
@Value("${unknown.param:some default}")
private String someDefault;
If the same property is defined as a system property and in the properties file, then the system property would be applied.
如果同一属性被定义为系统属性并在属性文件中,那么系统属性将被应用。
Suppose we had a property priority defined as a system property with the value System property and defined as something else in the properties file. The value would be System property:
假设我们有一个属性priority被定义为系统属性,其值为System property,并在属性文件中被定义为其他东西。该值将是系统属性。
@Value("${priority}")
private String prioritySystemProperty;
Sometimes, we need to inject a bunch of values. It would be convenient to define them as comma-separated values for the single property in the properties file or as a system property and to inject into an array.
有时,我们需要注入一堆值。将它们定义为属性文件中单个属性的逗号分隔的值,或者定义为系统属性并注入一个数组中,会很方便。
In the first section, we defined comma-separated values in the listOfValues of the properties file, so the array values would be [“A”, “B”, “C”]:
在第一节中,我们在属性文件的listOfValues中定义了逗号分隔的值,所以数组的值将是[“A”, “B”, “C”]:。
@Value("${listOfValues}")
private String[] valuesArray;
4. Advanced Examples With SpEL
4.使用SpEL的高级实例
We can also use SpEL expressions to get the value.
我们也可以使用SpEL表达式来获取数值。
If we have a system property named priority, then its value will be applied to the field:
如果我们有一个名为priority,的系统属性,那么它的值将被应用于字段。
@Value("#{systemProperties['priority']}")
private String spelValue;
If we have not defined the system property, then the null value will be assigned.
如果我们没有定义系统属性,那么将分配null值。
To prevent this, we can provide a default value in the SpEL expression. We get some default value for the field if the system property is not defined:
为了防止这种情况,我们可以在SpEL表达式中提供一个默认值。如果系统属性没有被定义,我们就会得到该字段的某个默认值。
@Value("#{systemProperties['unknown'] ?: 'some default'}")
private String spelSomeDefault;
Furthermore, we can use a field value from other beans. Suppose we have a bean named someBean with a field someValue equal to 10. Then, 10 will be assigned to the field:
此外,我们可以使用其他Bean的字段值。假设我们有一个名为someBean的bean,其字段someValue等于10。那么,10将被分配给该字段。
@Value("#{someBean.someValue}")
private Integer someBeanValue;
We can manipulate properties to get a List of values, here, a list of string values A, B, and C:
我们可以通过操作属性来获得一个List值,这里是一个字符串值A、B和C的列表。
@Value("#{'${listOfValues}'.split(',')}")
private List<String> valuesList;
5. Using @Value With Maps
5.使用@Value与Maps
We can also use the @Value annotation to inject a Map property.
我们还可以使用@Value注解来注入一个Map属性。
First, we’ll need to define the property in the {key: ‘value’ } form in our properties file:
首先,我们需要在{key: ‘value’ }中定义该属性。表格中的属性文件。
valuesMap={key1: '1', key2: '2', key3: '3'}
Note that the values in the Map must be in single quotes.
注意,Map中的值必须使用单引号。
Now we can inject this value from the property file as a Map:
现在我们可以把这个值从属性文件中作为一个Map注入。
@Value("#{${valuesMap}}")
private Map<String, Integer> valuesMap;
If we need to get the value of a specific key in the Map, all we have to do is add the key’s name in the expression:
如果我们需要在Map中获得一个特定键的值,我们所要做的就是在表达式中添加该键的名称。
@Value("#{${valuesMap}.key1}")
private Integer valuesMapKey1;
If we’re not sure whether the Map contains a certain key, we should choose a safer expression that will not throw an exception but set the value to null when the key is not found:
如果我们不确定Map是否包含某个键,我们应该选择一个更安全的表达式,它不会抛出一个异常,而是在没有找到该键时将值设置为null。
@Value("#{${valuesMap}['unknownKey']}")
private Integer unknownMapKey;
We can also set default values for the properties or keys that might not exist:
我们还可以为可能不存在的属性或键设置默认值。
@Value("#{${unknownMap : {key1: '1', key2: '2'}}}")
private Map<String, Integer> unknownMap;
@Value("#{${valuesMap}['unknownKey'] ?: 5}")
private Integer unknownMapKeyWithDefaultValue;
Map entries can also be filtered before injection.
Map条目也可以在注入前被过滤。
Let’s assume we need to get only those entries whose values are greater than one:
让我们假设我们只需要得到那些数值大于1的条目。
@Value("#{${valuesMap}.?[value>'1']}")
private Map<String, Integer> valuesMapFiltered;
We can also use the @Value annotation to inject all current system properties:
我们还可以使用@Value注解来注入所有当前系统属性。
@Value("#{systemProperties}")
private Map<String, String> systemPropertiesMap;
6. Using @Value With Constructor Injection
6.使用@Value与构造函数注入
When we use the @Value annotation, we’re not limited to a field injection. We can also use it together with constructor injection.
当我们使用@Value注解时,我们并不局限于字段注入。我们也可以将它与构造函数注入一起使用。。
Let’s see this in practice:
让我们在实践中看到这一点。
@Component
@PropertySource("classpath:values.properties")
public class PriorityProvider {
private String priority;
@Autowired
public PriorityProvider(@Value("${priority:normal}") String priority) {
this.priority = priority;
}
// standard getter
}
In the above example, we inject a priority directly into our PriorityProvider‘s constructor.
在上面的例子中,我们直接在PriorityProvider的构造函数中注入一个priority。
Note that we also provide a default value in case the property isn’t found.
请注意,我们还提供了一个默认值,以防止没有找到该属性。
7. Using @Value With Setter Injection
7.使用@Value与设定器注入
Analogous to the constructor injection, we can also use @Value with setter injection.
与构造函数注入类似,我们也可以使用@Value进行设置器注入。
Let’s take a look:
让我们来看看。
@Component
@PropertySource("classpath:values.properties")
public class CollectionProvider {
private List<String> values = new ArrayList<>();
@Autowired
public void setValues(@Value("#{'${listOfValues}'.split(',')}") List<String> values) {
this.values.addAll(values);
}
// standard getter
}
We use the SpEL expression to inject a list of values into the setValues method.
我们使用SpEL表达式向setValues方法注入一个值列表。
8. Conclusion
8.结语
In this article, we examined the various possibilities of using the @Value annotation with simple properties defined in the file, with system properties, and with properties calculated with SpEL expressions.
在这篇文章中,我们研究了使用@Value注解的各种可能性,包括文件中定义的简单属性、系统属性以及用SpEL表达式计算的属性。
As always, the example application is available on the GitHub project.
一如既往,该示例应用程序可在GitHub项目上获得。