Announcement Announcement Module
No announcement yet.
How to DI a template/reusable bean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to DI a template/reusable bean

    I have a POJO that reads a message from a queue and based on some meta data within the payload will call a stored proc using some fields in the message. Fine.

    But within my domain there are multiple Qs that each target a specific database so I created a generic bean (GenericListener) that I want to create multiple instances of.

    <bean id="auBatchRequestListener" class="org.someco.common.listeners.GenericListener">
            <property name="dataSource" ref="batchdbDS"/>
    <bean id="auOLTPListener" class="org.someco.common.listeners.GenericListener">
        <property name="dataSource" ref="oltpDS"/>
    As you can see each listener instance is injected with a specific datasource and the datasource (defined elsewhere) targets a separate database. Also the listeners are wrapped in a <jms:listener-container> and bound to separate q names.

    The problem is;

    Could not autowire field: javax.sql.DataSource org.someco.common.listeners.GenericListener.dataSource; 
    nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No unique bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: [batchdbDS, oltpDS]
    I cant use @Qualifiers as there's only one class that I want to re-use (re-bind) with multiple Q & datasource members. I tried autowire='no' and tried nesting the datasource bean definitions within the GeneriListener beans but its always the same exception. As I have 2 interface based beans (the 2 javax.sql.DataSource) it jsut cant find the right one to assign.

    Any help much appreciated as I really think this is doable and dont want to degenerate into a cloned/derived java class for each listener.

    Last edited by smcnamara; Mar 31st, 2009, 04:19 PM. Reason: fix code tags

  • #2
    It looks like Spring is trying to autowire by type, but since there are two valid candidates of the expected type (DataSource), it doesn't know which one to use.

    You could try autowiring by name, as described here.


    • #3
      Thanks James but the problem with byName wiring here is that there is only 1 property on 1 class. I want to create multiple instances of these beans (where each is a singleton) and have each of those contain a different value for the property - in this case have each point to a different datasource.

      Is this just not possible with spring? Surely I've missed something?


      • #4
        the issue seems to have fixed it self I love it when software does that.

        so likely I had some mismatch with @autowired fields vs setters but having redone the various classes i'm now sucessfulyl wiring the datasource into each bean instance correctly.