您可以将 SpEL 表达式与基于 XML 或基于注释的配置元数据结合使用 定义实例。在这两种情况下,定义表达式的语法都是 形式。BeanDefinition#{ <expression string> }Spring中文文档

XML 配置

可以使用表达式设置属性或构造函数参数值,如下所示 示例显示:Spring中文文档

<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,例如 (of type) 和 和 (of type)。environmentorg.springframework.core.env.EnvironmentsystemPropertiessystemEnvironmentMap<String, Object>Spring中文文档

以下示例显示了对 Bean 作为 SpEL 变量的访问:systemPropertiesSpring中文文档

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

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

请注意,您不必在此处为预定义变量添加符号前缀。#Spring中文文档

您还可以按名称引用其他 Bean 属性,如以下示例所示:Spring中文文档

<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>

注释配置

要指定默认值,可以将注释放在字段、方法、 以及方法或构造函数参数。@ValueSpring中文文档

以下示例设置字段的默认值:Spring中文文档

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
}

下面的示例显示了等效的,但是在属性设置器方法上:Spring中文文档

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
}

Autowired 方法和构造函数也可以使用注释,如下所示 示例显示:@ValueSpring中文文档

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) {
	// ...
}