对于最新稳定版本,请使用 Spring Framework 7.0.6spring-doc.cadn.net.cn

Bean 定义中的表达式

您可以将 SpEL 表达式与基于 XML 或基于注解的配置元数据一起使用,以定义 BeanDefinition 实例。在这两种情况下,定义表达式的语法形式均为 #{ <expression string> }spring-doc.cadn.net.cn

XML 配置

可以使用表达式来设置属性或构造函数参数的值,如下例所示:spring-doc.cadn.net.cn

<bean id="numberGuess" class="org.spring.samples.NumberGuess">
	<property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>

	<!-- other properties -->
</bean>

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

以下示例展示了如何将 systemProperties bean 作为 SpEL 变量进行访问:spring-doc.cadn.net.cn

<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
	<property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>

	<!-- other properties -->
</bean>

请注意,此处您无需在预定义变量前加上 # 符号。spring-doc.cadn.net.cn

您也可以通过名称引用其他 bean 的属性,如下例所示:spring-doc.cadn.net.cn

<bean id="numberGuess" class="org.spring.samples.NumberGuess">
	<property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>

	<!-- other properties -->
</bean>

<bean id="shapeGuess" class="org.spring.samples.ShapeGuess">
	<property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/>

	<!-- other properties -->
</bean>

注解配置

要指定默认值,您可以将 @Value 注解应用于字段、方法以及方法或构造函数的参数上。spring-doc.cadn.net.cn

以下示例设置了字段的默认值:spring-doc.cadn.net.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 {

	@Value("#{ systemProperties['user.region'] }")
	var defaultLocale: String? = null
}

以下示例展示了等效的用法,但应用于属性的 setter 方法上:spring-doc.cadn.net.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 {

	@Value("#{ systemProperties['user.region'] }")
	var defaultLocale: String? = null
}

自动装配的方法和构造函数也可以使用 @Value 注解,如下例所示:spring-doc.cadn.net.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) {
	// ...
}