0

I have a bean which is a typed - map in my xml. But when I try to use it using @Autowired it does not work as expected. I also tried @Resource(name = "service") as suggested in a related question in stack overflow which also does not work.

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

Is there a way to see whether the bean in xml is instantiated properly. I am getting the attribute service value as null. I have been working on this for a day. still could not solve the puzzle. though i ransacked spring reference and stackoverflow (strangely spring's map autowiring is difficult or i am not able to do it). Thanks for your help in advance. Also strangely the related question i have refered to seems to have been accepted by many but does not work for me.

Community
  • 1
  • 1
Amar
  • 1,556
  • 3
  • 18
  • 28
  • The `Map` autowiring is a specific one as it will give you all the beans of type T in a map. It will not inject a bean of type map with the specific name. If it is still null as you stated you haven't setup annotation based autowiring which means you are missing eiether `` or `` the latter implies the first. – M. Deinum Nov 26 '14 at 13:12
  • does this mean i dont need anything (like util:map) in my xml and will get all the beans of type T in a map? . suppose i have serv1, serv2 as beans will i get serv1, serv2 beans against key set "serv1" & "serv2". – Amar Nov 26 '14 at 13:29
  • That is the theory as explained in the [spring reference guide](http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation). – M. Deinum Nov 26 '14 at 13:36
  • When i use @resource only, i get null. when i use @Autowired, i get all beans as you have pointed out. So that is verified. But how do i get my bean through annotation? when i do a `Object obj = applicationContext.getBean("service");` i am able to see the bean generated but i am not able to get it through annotation. I have `` – Amar Nov 26 '14 at 14:09
  • What is that "service" bean you are talking about? Also as stated initialy the `Map` is treated special by Spring. – M. Deinum Nov 26 '14 at 14:33
  • it is a map of all services i have with a key for each service. i want to dynamically pick a service based on the key value fed by user. Only `@Resource` gives null. `@Autowired @Resource(name="service")` gives map containing all beans but the keys are same as bean names which is not useful to me. in xml, i have specific keys for the services. But i am unable to get it through annotations. This is my problem. – Amar Nov 26 '14 at 14:38
  • Then why not simply use `@Autowired` instead of trying to create a `Map` yourself first and try to inject that. – M. Deinum Nov 26 '14 at 14:39
  • But that will create a tight coupling between my class names & the user input. That's wny i have created a bean of the map in xml. – Amar Nov 26 '14 at 14:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65695/discussion-between-amar-and-m-deinum). – Amar Nov 26 '14 at 14:44
  • Why would that be? What is the difference with your map? Bean name is not the same as a classname. You can specify a name in your `@Component` (or derived annotations). Or simply create aliases in xml for those beans. – M. Deinum Nov 26 '14 at 14:45
  • That's an excellent alternative and will work for me. You have made my day. – Amar Nov 26 '14 at 14:49

1 Answers1

0

Though i could not resolve the problem directly, i used spring service locator to resolve it finally wrapping the map in it.

Amar
  • 1,556
  • 3
  • 18
  • 28