1

I have the following problem.

I have a generic class A

public class A<T, DAO extends JpaRepository<?, ?>>
{    

    @Autowired
    protected DAO daoObject;

    ......

and there I am trying inject a genreic DAO-object of the JpaRepository-type.

If I have only one implemetation of injected object(of JpaRepository), then there is no problem, but if I have more then one, then spring doesn't know which object it is to inject and throws an exception.

The question is: How can I dynamish based on generic information, inject the correct object?

Thank you.

public interface IRegisteredUserDAO extends JpaRepository<RegisteredUser, String> {

}


public interface IMailLogDao extends JpaRepository<MailLog, Long> {

   findByTo(String to);
}

and i used it so

public class RegisteredUserVM extends YBTableViewModel<RegisteredUser, IRegisteredUserDAO>
{

UPDATE:

public class MailLogVM extends YBTableViewModel<MailLog, IMailLogDao>
{    

}
user1167253
  • 813
  • 1
  • 11
  • 27

2 Answers2

2

You can use Spring's @Qualifier annotation to inject the correct bean:

@Autowired
@Qualifier("IRegisteredUserDAOImpl")
protected DAO daoObject;

Here the bean with qualifier value "IRegisteredUserDAOImpl" is wired.

Debojit Saikia
  • 10,532
  • 3
  • 35
  • 46
2

You should be able to do this using the latest Spring 4 RC1. Versions before that do not support generic injection at all. See this ticket and related commits for details.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211