0

I can autowire two QueueChannel instances using @Autowired and @Qualifier in a test class just fine, until I add JMX export namespace handlers.

I've not got the exact config handy (it was a problem at work that's now bugging me out of hours!), but I'm using these elements:

<int-jmx:mbean-export default-domain="com.business" server="mbeanServer" />
<context:mbean-export />
<context:mbean-server />

When I've got these three things defined, the autowiring process fails throwing a NoSuchBeanException. However, I can see that there are beans with the IDs of my queues, as I've got a post processor that's iterating over all beans in the context.

Is this something to do with proxying obfuscating the declared type of the QueueChannels, preventing autowiring-by-type working?

DeejUK
  • 12,891
  • 19
  • 89
  • 169

1 Answers1

3

You need to autowire using the interface instead of the concrete class because the JMX export wraps the channel in a proxy. Use PollableChannel for QueueChannel, or SubscribableChannel for DirectChannel.

It's always good practice to code using interfaces rather than concrete classes, for exactly this reason.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Hi Gary, thanks for pointing that out. The autowiring now works, but I need the functionality of the QueueChannel class (clearing the queue between tests, asserting on size). I'm getting classcast exceptions `$Proxy13 cannot be cast to `o.s.i.c.QueueChannel` - how can I make use of the implementation's methods? – DeejUK Nov 13 '12 at 09:48
  • Found the least-bad workaorund here: http://www.techper.net/2009/06/05/how-to-acess-target-object-behind-a-spring-proxy/ – DeejUK Nov 13 '12 at 10:50
  • I don't have a better work around for you. In order to make these methods available via the proxy we'd have to create another interface, say QueueOperations, declaring these methods and have the QueueChannel implement it. Feel free to open a JIRA 'Improvement' ticket and we'll consider it for a future release. – Gary Russell Nov 14 '12 at 05:21
  • Thanks - I was only autowiring QueueChannels in tests, so for the meantime I've just separated out the JMX features to only be enabled when not testing. All good to know though, in case I encounter this elsewhere. – DeejUK Nov 14 '12 at 10:48