跳到主要内容
版本:7.0.3

Bean引用

Hunyuan 7b 中英对照 Bean References

如果评估上下文已经配置了Bean解析器,你可以使用@符号作为前缀来从表达式中查找Bean。以下示例展示了如何操作:

ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new MyBeanResolver());

// This will end up calling resolve(context, "someBean") on MyBeanResolver
// during evaluation.
Object bean = parser.parseExpression("@someBean").getValue(context);
备注

如果bean名称包含点(.)或其他特殊字符,您必须将bean的名称作为字符串字面量提供——例如,@'order.service'

要直接访问工厂bean,你应该在bean名称前加上&符号。以下示例展示了如何操作:

ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new MyBeanResolver());

// This will end up calling resolve(context, "&someFactoryBean") on
// MyBeanResolver during evaluation.
Object factoryBean = parser.parseExpression("&someFactoryBean").getValue(context);