| 
         此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10!  | 
    
| 
         此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10!  | 
    
您可以将 SpEL 表达式与基于 XML 或基于注释的配置元数据结合使用
定义实例。在这两种情况下,定义表达式的语法都是
形式。BeanDefinition#{ <expression string> }
XML 配置
可以使用表达式设置属性或构造函数参数值,如下所示 示例显示:
<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>
以下示例显示了对 Bean 作为 SpEL 变量的访问:systemProperties
<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
	<property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>
	<!-- other properties -->
</bean>
请注意,您不必在此处为预定义变量添加符号前缀。#
您还可以按名称引用其他 Bean 属性,如以下示例所示:
<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
以下示例设置字段的默认值:
- 
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 {
	@Value("#{ systemProperties['user.region'] }")
	var defaultLocale: String? = null
}
下面的示例显示了等效的,但是在属性设置器方法上:
- 
Java
 - 
Kotlin
 
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 方法和构造函数也可以使用注释,如下所示
示例显示:@Value
- 
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
 
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) {
	// ...
}