-1

Autowired by generics works perfectly fine with static type.But when i write

@Autowired

private GenericDAO<T> dao;

with dynamic type at runtime getting error at deploy time:"multiple bean definition found."

Does Autowired by type in Spring 4 not work with runtime type?.

BitTickler
  • 10,905
  • 5
  • 32
  • 53
js-hint
  • 66
  • 1
  • 10
  • No it won't and never will due to the nature of generics. So it isn't a lie it works if you specify the type it works while still being able to use a genericdao. – M. Deinum Sep 02 '15 at 10:11
  • possible duplicate of [Spring Problem with generic types](http://stackoverflow.com/questions/6857149/spring-problem-with-generic-types) – Fritz Duchardt Sep 02 '15 at 10:17
  • Some what similar but in my case haven't extends that class. so no case of hierarchy. – js-hint Sep 02 '15 at 10:32

1 Answers1

0

I'm not sure how you expect spring to resolve it for a generic type. If there was only one bean eligible it would be easy for the Spring container to resolve and autowire it.

The Generics is a mechanism for developer type safety which is erased/not available in the run time.

@Autowired
private GenericDAO<T> dao;

and if config has

<bean id="genericDAO" class="com.example.GenericDAO" />

as only bean that satisfies the requirement, then both Spring and yourself can be happy of serving the purpose. By the way, none lied :)

Karthik R
  • 5,523
  • 2
  • 18
  • 30