• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

@Autowired does not inject a Map ?

 
Ranch Hand
Posts: 1738
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, in Getting Started with Spring Framework 4th edition , in section 6-6 JSR 250's @Resource annotation, there is a quote:


@Autowired annotation doesn't work for beans that are themselves of collection or Map type. For instance, if you define a bean using util schema's map element, you can't autowire it using @Autowired annotation.


But as I tried , it works with @Autowired.




To my surprised, the output works fine. It should not work with @Autowired.


INFO  sample.spring.SampleApp - retrieving from my map : Maryland

 
Master Rancher
Posts: 451
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you are using a new version and they changed it?  Or maybe it's because you extended HashMap? It might be talking about if your bean was a Map<> as opposed to like HashMap. No idea either way
 
Bartender
Posts: 20842
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"collection or Map type"?  

A Map is a collection! Someone doesn't seem to know what they are talking about.

And this doesn't jibe with my experience with Spring in general. I think at best it's related to a particular environment. I note that your Map is a @Component and not literally a @Resource.
 
Himai Minh
Ranch Hand
Posts: 1738
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the quote:


@Autowired annotation doesn't work for beans that are themselves of collection or Map type. For instance, if you define a bean using util schema's map element, you can't autowire it using @Autowired annotation.



It refers to a bean that is a Map.  So, @Component MyMap is a bean and a Map type.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!