此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0spring-doc.cn

Bean 定义中的表达式

您可以将 SPEL 表达式与配置元数据一起使用来定义 Bean 实例。在两者中 cases,则定义表达式的语法格式为 .#{ <expression string> }spring-doc.cn

应用程序上下文中的所有 bean 都可以作为预定义的变量使用,其 通用 bean 名称。这包括用于访问运行时环境的标准上下文 bean,例如 (type ) 和 and (type )。environmentorg.springframework.core.env.EnvironmentsystemPropertiessystemEnvironmentMap<String, Object>spring-doc.cn

要指定默认值,您可以将注释放在字段、方法、 和方法或构造函数参数(或 XML 等效项)。@Valuespring-doc.cn

以下示例设置字段的默认值:spring-doc.cn

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
}

请注意,您不必在此处为预定义变量加上 symbol 前缀。#spring-doc.cn

以下示例显示了等效的 but on a property setter 方法:spring-doc.cn

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>

自动装配的方法和构造函数也可以使用 Comments,如下所示 示例显示:@Valuespring-doc.cn

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
	}

	// ...
}
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 属性,如下例所示:spring-doc.cn

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>