1

I have 2 maven projects, a web-app, and a 'service' project. I am using spring to wire everything together. I would like to create a map in my application-context.xml, and have that injected into my class. When I try to start my web application I get an error message.

Here is my class:

 @Named("tranformer")
 public class IdentifierTransformerImpl implements IdentifierTransformer {


private Map<String, String> identifierMap;

@Inject
public IdentifierTransformerImpl(
        @Named("identifierMap")
        final Map<String, String> identifierMap) {
            this.identifierMap= identifierMap;
}

and the application-context.xml:

    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd"

  <util:map id="identifierMap" map-class="java.util.HashMap">
    <entry key="Apple" value="fruit"/>
    <entry key="BlackBerry" value="fruit"/>
    <entry key="Android" value="robot"/>
   <util:map>

  <context:component-scan base-package="com.example" />

I get the following error:

 ..... No matching bean of type [java.lang.String] found for dependency [map with value type java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Named(value=identifierMap)

This works when I define a string in my application-context, and constructor inject it into a class, how can I do the same for a map?

Andy N
  • 392
  • 3
  • 8
  • 24

3 Answers3

2

I think Map collection is not supported by @Inject or @Autowired annotation (see: Spring can't autowire Map bean). However I managed to autowire a Map by annotating it as a resource as suggested by that answer. Try this:

@Resource(name="identifierMap") private Map<String, String> identifierMap;

The downside is ofcourse that isn't a constructor autowiring, but a field. I haven't found a way of doing it through constructor autowiring yet

Community
  • 1
  • 1
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • Thanks for the info. I create a 'wrapper' bean for the map and injected the map into it via application-context.xml (with getter/setter and the tag. I could then use my @Inject annotation for putting that new bean into my identifierTransformer class. It works, but it isn't pretty – Andy N Apr 30 '13 at 16:28
0

If that is a direct copy/paste of your xml file, you aren't ending your <util:map> tag. You're missing a / in the second one.

DatBear
  • 139
  • 1
  • 2
  • 8
  • Yah, that was sloppy copying from my application-context. It wouldn't have caused the problems described above – Andy N May 06 '13 at 18:56
-1

With constructor injection.

@Inject
public IdentifierTransformerImpl(
    @Value(value = "#{identifierMap}")
    final Map<String, String> identifierMap) {
        this.identifierMap= identifierMap;
}

Not ideal but Spring declines to provide a better option.

Check out more details at https://jira.spring.io/browse/SPR-8519

jqmichael
  • 79
  • 1
  • 3