I've been trying to inject a hashmap into a class. According to this post: Auto-wiring a List using util schema gives NoSuchBeanDefinitionException I should be using @Resource
rather than @Autowire
for a map.
My code looks like this:
@Configurable
public class MyClass
{
@Resource(name="myMap")
private Map<String,String> myMap = new HashMap<String, String>();
My config looks like this:
<context:annotation-config/>
<context:component-scan base-package="com.MyClass"/>
<util:map id="myMap"
key-type="java.lang.String"
value-type="java.lang.String">
<entry key="k1" value="v1"/>
<entry key="k2" value="v2"/>
</util:map>
The application starts up OK, but when I try to run code that references MyClass
, I get this WebSphere error:
CNTR0035E: EJB container caught com.ibm.wsspi.injectionengine.InjectionException: Failed to process bindings for metadata at com.ibm.ws.injectionengine.InjectionEngineImpl.processBindings(InjectionEngineImpl.java:529) at com.ibm.ws.injectionengine.InjectionEngineImpl.processInjectionMetaData(InjectionEngineImpl.java:322) at com.ibm.ws.util.ComponentNameSpaceHelper.populateJavaNameSpace(ComponentNameSpaceHelper.java:806) ... Caused by: com.ibm.wsspi.injectionengine.InjectionException: CWNEN0044E: A resource reference binding could not be found for the following resource references [myMap], defined for the MyService component. at com.ibm.wsspi.injectionengine.InjectionProcessor.collectInjectionNBindingData(InjectionProcessor.java:1042) at com.ibm.ws.injectionengine.InjectionEngineImpl.processBindings(InjectionEngineImpl.java:516) ... 52 more and is throwing com.ibm.ejs.container.ContainerException: Failed to initialize BeanMetaData instance; nested exception is: com.ibm.wsspi.injectionengine.InjectionException: Failed to process bindings for metadata.
Is there any way to inject the map into my class in WebSphere?
(using Spring 2.5, WebSphere 7)
Earlier, I'd tried autowiring by name with this code:
@Configurable(autowire=Autowire.BY_NAME, preConstruction=true)
public class MyClass
{
@Qualifier("myMap")
@Autowired(required=true)
private Map<String,String> myMap = new HashMap<String, String>();
and with this config:
<context:annotation-config/>
<context:component-scan base-package="com.MyClass"/>
<util:map id="myMap"
key-type="java.lang.String"
value-type="java.lang.String">
<entry key="k1" value="v1"/>
<entry key="k2" value="v2"/>
</util:map>
And the result is that the fields I'm trying to autowire are null.