此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10! |
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10! |
您可以将 SpEL 表达式与配置元数据一起使用来定义 Bean 实例。在两者中
情况下,定义表达式的语法为 。#{ <expression string> }
应用程序上下文中的所有 Bean 都可以作为预定义变量使用,其
普通 Bean 名称。这包括用于访问运行时环境的标准上下文 bean,例如 (of type) 和 和 (of type)。environment
org.springframework.core.env.Environment
systemProperties
systemEnvironment
Map<String, Object>
要指定默认值,可以将注释放在字段、方法、
以及方法或构造函数参数(或 XML 等效参数)。@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 {
@field:Value("#{ systemProperties['user.region'] }")
lateinit var defaultLocale: String
}
请注意,您不必在此处为预定义变量添加符号前缀。#
下面的示例显示了等效的,但是在属性设置器方法上:
-
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>
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
-
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 属性,如以下示例所示:
-
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>