Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring Autowired with Qualifier automatically converting Map to List?

 
Peter Kovac
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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

 
Peter Kovac
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic