【问题标题】:Spring Integration - Use SpEL in service-activator nested bean constructor-argSpring Integration - 在服务激活器嵌套 bean 构造函数中使用 SpEL
【发布时间】:2017-03-25 11:34:39
【问题描述】:

我正在尝试使用factory-method 来初始化service-activator,如下所示

<int:service-activator>
    <bean class="com.sample.FileWriterFactory" factory-method="createFileWriter">
        <constructor-arg index="0" value="${xml.out-directory}/xml"/>
        <constructor-arg index="1" value="#{ headers['file_name'] + '.xml' }"/>
    </bean>
</int:service-activator>

但是,SpEL 评估失败,因为在评估上下文中找不到 headers 属性。确切的错误 sn-p 是

org.springframework.expression.spel.SpelEvaluationException: EL1008E: 在“org.springframework.beans.factory.config.BeanExpressionContext”类型的对象上找不到属性或字段“headers” - 可能不公开?

这样做的目的是我想通过根据需要传递不同的参数来重用相同的 POJO。 我做错了什么?

【问题讨论】:

    标签: spring-integration spring-el


    【解决方案1】:

    #{...} 表达式在上下文初始化期间计算一次。

    访问属性的表达式(例如headers)需要在运行时进行评估(针对作为根对象的消息)。

    如果您要这样做,请使用value="headers['file_name'] + '.xml'",然后在您的构造函数中...

    private final Expression expression;
    
    public FileWriterFactory(String directory, String expression) {
        ...
        this.expression = new SpelExpressionParser().parseExpression(expression);
    }
    

    然后,在你的运行时服务方法中

    String fileName = this.expression.getValue(message, String.class);
    

    【讨论】:

    • 感谢您的澄清,@Gary。像魅力一样工作!
    猜你喜欢
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多