Bean References
如果评估上下文已配置了bean解析器,则可以使用@
符号从表达式中查找bean。以下示例展示了如何执行此操作:
-
Java
-
Kotlin
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new MyBeanResolver());
// 在评估过程中,这将调用MyBeanResolver上的resolve(context,"something")
Object bean = parser.parseExpression("@something").getValue(context);
val parser = SpelExpressionParser()
val context = StandardEvaluationContext()
context.setBeanResolver(MyBeanResolver())
// 在评估过程中,这将调用MyBeanResolver上的resolve(context,"something")
val bean = parser.parseExpression("@something").getValue(context)
要访问工厂bean本身,您应该使用&
符号作为bean名称的前缀。以下示例展示了如何执行此操作:
-
Java
-
Kotlin
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new MyBeanResolver());
// 在评估过程中,这将调用MyBeanResolver上的resolve(context,"&foo")
Object bean = parser.parseExpression("&foo").getValue(context);
val parser = SpelExpressionParser()
val context = StandardEvaluationContext()
context.setBeanResolver(MyBeanResolver())
// 在评估过程中,这将调用MyBeanResolver上的resolve(context,"&foo")
val bean = parser.parseExpression("&foo").getValue(context)