Spring Groovy DSL匿名内豆与构造函数

时间:2017-01-24 14:59:43

标签: spring grails groovy

鉴于这两个豆子:

myMessageAdapter(MessageListenerAdapter) { bean ->
    bean.constructorArgs = [ref('jmsReceiver')]
    defaultListenerMethod = 'processMessage'
}

listenerContainer(DefaultMessageListenerContainer) {
    connectionFactory = ref('connectionFactory')
    destinationName = "MyDest"
    messageListener = ref('myMessageAdapter')
}

我想用匿名内部bean替换myMessageAdapter

尝试:

listenerContainer(DefaultMessageListenerContainer) {
    connectionFactory = ref('connectionFactory')
    destinationName = "MyDest"
    messageListener = { MessageListenerAdapter bean ->
        bean.constructorArgs = [ref('jmsReceiver')]
        defaultListenerMethod = 'processMessage'
    }
}

但它失败了"无法设置属性' constructorArgs'在null对象"

如何传递构造函数参数?

2 个答案:

答案 0 :(得分:1)

当您查看BeanBuilder的源代码和方法setPropertyOnBeanConfig

时,我认为这是不可能的。

答案 1 :(得分:1)

实现此目的的方法是在Bean闭包内设置类:

listenerContainer(DefaultMessageListenerContainer) {
    connectionFactory = ref('connectionFactory')
    destinationName = "MyDest"
    messageListener = { bean ->
        bean.beanClass = MessageListenerAdapter 
        bean.constructorArgs = [ref('jmsReceiver')]
        bean.defaultListenerMethod = 'processMessage'
    }
}