rlovtang
rlovtang

Reputation: 5060

Spring Groovy DSL anonymous inner bean with constructor

Given these two beans:

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

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

I would like to replace myMessageAdapter with an anonymous inner bean.

Tried:

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

But it fails with "Cannot set property 'constructorArgs' on null object"

How can I pass the constructor param?

Upvotes: 0

Views: 230

Answers (2)

Kiril Tonev
Kiril Tonev

Reputation: 68

The way you can achieve this is to set the class within the bean closure:

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

Upvotes: 1

Sami Mäkelä
Sami Mäkelä

Reputation: 191

I don't think it's possible when you look at the source code for the BeanBuilder and the method setPropertyOnBeanConfig

Upvotes: 1

Related Questions