此版本仍在开发中,尚未被视为稳定版本。最新的稳定版本请使用 Spring Framework 6.1.13! |
此版本仍在开发中,尚未被视为稳定版本。最新的稳定版本请使用 Spring Framework 6.1.13! |
您可以将 SPEL 表达式与配置元数据一起使用来定义 Bean 实例。在两者中
cases,则定义表达式的语法格式为 .#{ <expression string> }
应用程序上下文中的所有 bean 都可以作为预定义的变量使用,其
通用 bean 名称。这包括用于访问运行时环境的标准上下文 bean,例如 (type ) 和 and (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
}
请注意,您不必在此处为预定义变量加上 symbol 前缀。#
以下示例显示了等效的 but on a property setter 方法:
-
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>
自动装配的方法和构造函数也可以使用 Comments,如下所示
示例显示:@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>