File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Spring Autowired with Qualifier automatically converting Map to List? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Autowired with Qualifier automatically converting Map to List?" Watch "Spring Autowired with Qualifier automatically converting Map to List?" New topic
Author

Spring Autowired with Qualifier automatically converting Map to List?

Peter Kovac
Ranch Hand

Joined: Aug 08, 2010
Posts: 42
Hi All,

In an existing code I saw the following:



and in XML :




My question if systemFields is declared as Map in XML why is it possible to use it as List<Map<String, Field>> parameter in setSystemDefaultFields?

Is it something like that if there are multiple systemFields definitions they're merged into a list?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"Is it something like that if there are multiple systemFields definitions they're merged into a list?"

Well, yes and no.

The no part, One you can't make more than one bean be called systemFields in Spring. Basically, that id/name, is used in Spring in a Map as a key, so only one "systemFields" key could ever be in a Map. But that isn't your Map in your code. So only one systemFields bean is merged into a List.

So, the yes part. If you did not have @Qualifier there, then Spring will go out and find all the beans of type Map<String, Field> and automatically put them all into a List and do the autowiring.

Hope that helps

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Peter Kovac
Ranch Hand

Joined: Aug 08, 2010
Posts: 42


Mark Spritzler wrote:
So, the yes part. If you did not have @Qualifier there, then Spring will go out and find all the beans of type Map<String, Field> and automatically put them all into a List and do the autowiring.


Thanks for the answer.

The thing I don't get is why the code uses List<Map<String, Field>> instead of Map<String, Field>? Is that a precaution in case somebody specifies multiple maps of the same key and value types?

I guess if @Qualifier is used then Map<String, Field> would suffice and don't need List?

Thanks,
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yeah, can't tell you what they were thinking when they passed in a List of those Maps to their api.

Are they still around that you can ask them?

Mark
Peter Kovac
Ranch Hand

Joined: Aug 08, 2010
Posts: 42
Mark Spritzler wrote:
Are they still around that you can ask them?


Nope, but thanks for the information.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Spring Autowired with Qualifier automatically converting Map to List?
 
Similar Threads
clarity required on spring annotations
Spring IOC issues in MVC WebApplication with JSF integration
Beginning Spring: Syntax for Map<Integer, Double>?
Problem with <util:map - Invalid nested exception is org.xml.sax.SAXParseException
Spring Related-- @Autowired with Map<String, myObjectType> is not initialized in the IoC container