Bean 定义中的表达式

您可以使用SpEL表达式和配置元数据来定义bean实例。在这两种情况下,定义表达式的语法格式为#{ <表达式字符串> }

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

要指定默认值,可以在字段、方法和方法或构造函数参数上放置@Value注解(或XML等效项)。

以下示例设置字段的默认值

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

请注意,您无需在此处使用#符号作为预定义变量的前缀。

以下示例显示了在属性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>

自动装配的方法和构造函数也可以使用@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>