6

There is a class with 2 Generic Types.

public class GenericDao<T, ID extends Serializable> implements IGenericDao<T, ID> {
...
}

I need to initiate it from spring.

<bean id="myDao" class="com.xxx.yyy.GenericDao">
    <qualifier type="com.xxx.yyy.Item"/> <!-- Works for T -->
    <!-- Need something for ID -->
    ...
    <property name="name" ref="value"/>
    <property name="name" ref="value"/>
</bean>

with qualifier tag i can handle T. I also need to handle ID.

ufukgun
  • 6,889
  • 8
  • 33
  • 55

1 Answers1

0

You are doing something wrong. @Qualifier as well as <qualifier> are for injecting dependencies of beans. But you are not injecting anything into your bean and not injecting your bean anywhere, you are just trying to define the bean.

In your case it is important to understand that in xml you are defining beans which will be instantiated at runtime. And at runtime generic types are erasured: type erasure

That said it doesn't matter what generic types/hints you can possibly give to Spring it will be completely useless as at runtime - when the spring will instantiate the beans - all generic types information will be lost.

The correct usage of qualifier tag would be if you had:

public class MovieRecommender {

  @Autowired
  @Qualifier("main")
  private MovieCatalog movieCatalog;

  // ...
}

and xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:annotation-config/>

  <bean class="example.SimpleMovieCatalog">
      <qualifier value="main"/>
      <!-- inject any dependencies required by this bean -->
  </bean>

  <bean id="movieRecommender" class="example.MovieRecommender"/>

</beans>

For a fallback match, the bean name is considered a default qualifier value. You can read more at the spring docs

Vadim Kirilchuk
  • 3,532
  • 4
  • 32
  • 49