Expressions in Bean Definitions

你可以在配置元数据中使用 SpEL 表达式来定义 bean 实例。在这两种情况下,定义表达式的语法形式为 #{<expression string>}。

You can use SpEL expressions with configuration metadata for defining bean instances. In both cases, the syntax to define the expression is of the form #{ <expression string> }.

应用程序上下文中的所有 bean 都可以作为预定义的变量使用,其通用 bean 名称。这包括标准上下文 bean,例如 environment(类型为 org.springframework.core.env.Environment),以及 systemPropertiessystemEnvironment(类型为 Map<String, Object>),用于访问运行时环境。

All beans in the application context are available as predefined variables with their common bean name. This includes standard context beans such as environment (of type org.springframework.core.env.Environment) as well as systemProperties and systemEnvironment (of type Map<String, Object>) for access to the runtime environment.

要指定默认值,可以在字段、方法和方法或构造函数参数(或 XML 等效项)上放置 @Value 注解。

To specify a default value, you can place the @Value annotation on fields, methods, and method or constructor parameters (or XML equivalent).

下面的例子设置了字段的默认值:

The following example sets the default value of a field:

  • Java

  • Kotlin

public class FieldValueTestBean {

	@Value("#{ systemProperties['user.region'] }")
	private String defaultLocale;

	public void setDefaultLocale(String defaultLocale) {
		this.defaultLocale = defaultLocale;
	}

	public String getDefaultLocale() {
		return this.defaultLocale;
	}
}
class FieldValueTestBean {

	@field:Value("#{ systemProperties['user.region'] }")
	lateinit var defaultLocale: String
}

请注意,这里不必使用 # 符号作为预定义变量的前缀。

Note that you do not have to prefix the predefined variable with the # symbol here.

下面的例子展示了等效的属性设置器方法:

The following example shows the equivalent but on a property setter method:

  • Java

  • Kotlin

  • Xml

public class PropertyValueTestBean {

	private String defaultLocale;

	@Value("#{ systemProperties['user.region'] }")
	public void setDefaultLocale(String defaultLocale) {
		this.defaultLocale = defaultLocale;
	}

	public String getDefaultLocale() {
		return this.defaultLocale;
	}
}
class PropertyValueTestBean {

	@set:Value("#{ systemProperties['user.region'] }")
	lateinit var defaultLocale: String
}
<bean id="testBean" class="org.springframework.docs.core.expressions.expressionsbeandef.PropertyValueTestBean">
	<property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>
</bean>

自动装配的方法和构造函数也可以使用 @Value 注解,如下面的例子所示:

Autowired methods and constructors can also use the @Value annotation, as the following examples show:

  • Java

  • Kotlin

public class SimpleMovieLister {

	private MovieFinder movieFinder;
	private String defaultLocale;

	@Autowired
	public void configure(MovieFinder movieFinder,
			@Value("#{ systemProperties['user.region'] }") String defaultLocale) {
		this.movieFinder = movieFinder;
		this.defaultLocale = defaultLocale;
	}

	// ...
}
class SimpleMovieLister {

	private lateinit var movieFinder: MovieFinder
	private lateinit var defaultLocale: String

	@Autowired
	fun configure(movieFinder: MovieFinder,
				  @Value("#{ systemProperties['user.region'] }") defaultLocale: String) {
		this.movieFinder = movieFinder
		this.defaultLocale = defaultLocale
	}

	// ...
}
  • Java

  • Kotlin

  • Xml

public class MovieRecommender {

	private String defaultLocale;

	private CustomerPreferenceDao customerPreferenceDao;

	public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,
			@Value("#{systemProperties['user.country']}") String defaultLocale) {
		this.customerPreferenceDao = customerPreferenceDao;
		this.defaultLocale = defaultLocale;
	}

	// ...
}
class MovieRecommender(private val customerPreferenceDao: CustomerPreferenceDao,
					   @Value("#{systemProperties['user.country']}")
					   private val defaultLocale: String) {
	// ...
}
<bean id="testBean" class="org.springframework.docs.core.expressions.expressionsbeandef.MovieRecommender">
	<constructor-arg ref="customerPreferenceDao"/>
	<constructor-arg value="#{ systemProperties['user.country'] }"/>
</bean>

你也可以按名称引用其他 bean 属性,如下面的例子所示:

You can also refer to other bean properties by name, as the following example shows:

  • Java

  • Kotlin

  • Xml

public class ShapeGuess {

	private double initialShapeSeed;

	@Value("#{ numberGuess.randomNumber }")
	public void setInitialShapeSeed(double initialShapeSeed) {
		this.initialShapeSeed = initialShapeSeed;
	}

	public double getInitialShapeSeed() {
		return initialShapeSeed;
	}
}
class ShapeGuess {

	@set:Value("#{ numberGuess.randomNumber }")
	var initialShapeSeed: Double = 0.0
}
<bean id="shapeGuess" class="org.springframework.docs.core.expressions.expressionsbeandef.ShapeGuess">
	<property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/>
</bean>