spring autowired with queues and maps exception

1,166 views
Skip to first unread message

shane muffat

unread,
Jun 1, 2012, 1:31:25 PM6/1/12
to haze...@googlegroups.com
I'm trying to autowire queues and maps using spring.  I'm getting the following exception on startup.  Can I inject maps and queues into my app?  Am i doing something wrong?

Exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataGrid': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.altisource.real.servicing.datagrid.server.DataGrid.setQueue(com.hazelcast.core.IQueue); nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.util.HashMap$Values' to required type 'com.hazelcast.core.IQueue'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.util.HashMap$Values] to required type [com.hazelcast.core.IQueue]: no matching editors or conversion strategy found


Here is my xml:
    ...
<hz:hazelcast id="HazelcastInstance">
<hz:config>
<hz:instance-name>HazelcastInstance</hz:instance-name>
 ... other details omitted

<hz:queue name="rsJournalQueue" max-size-per-jvm="${hz.journalQueue.maxSizePerJVM}"
backing-map-ref="journalQueueBackingMap" />

<hz:map name="journalQueueBackingMap" backup-count="1">
<hz:map-store enabled="true" implementation="journalQueueBackingMapstore"
write-delay-seconds="0" />
</hz:map>

<hz:map name="workerStateMap" backup-count="1">
<hz:map-store enabled="true" implementation="workerStateMapstore"
write-delay-seconds="0" />
</hz:map>

</hz:config>
</hz:hazelcast>

        <bean id="rsJournalQueue" class="com.hazelcast.core.IQueue" factory-bean="HazelcastInstance" factory-method="getQueue">
<constructor-arg value="rsJournalQueue" />
</bean>

<bean id="workerStateMap" factory-bean="HazelcastInstance" factory-method="getMap">
<constructor-arg value="workerStateMap" />
</bean>

Inside my enclosing bean, I have the following:
    @Autowired
    @Qualifier("rsJournalQueue")
    public void setQueue(IQueue<Object> queue) {
this.rsJournalQueue = queue;
}



shane muffat

unread,
Jun 4, 2012, 8:53:05 AM6/4/12
to haze...@googlegroups.com
Found the answer but was a Spring issue and not Hazelcast.  Had to use @Resource instead of @Autowired.  
From section 4.9.3 of the Spring 3.1 reference docs:

Tip

If you intend to express annotation-driven injection by name, do not primarily use @Autowired, even if is technically capable of referring to a bean name through@Qualifier values. Instead, use the JSR-250 @Resource annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.

As a specific consequence of this semantic difference, beans that are themselves defined as a collection or map type cannot be injected through @Autowired, because type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection or map bean by unique name.

@Autowired applies to fields, constructors, and multi-argument methods, allowing for narrowing through qualifier annotations at the parameter level. By contrast,@Resource is supported only for fields and bean property setter methods with a single argument. As a consequence, stick with qualifiers if your injection target is a constructor or a multi-argument method.


Reply all
Reply to author
Forward
0 new messages