弹簧。动态依赖注入 [英] Spring. Dynamic dependency injection
问题描述
我有一个泛型类A
public class A< T,DAO extends JpaRepository<?,?>>>
{
@Autowired
保护DAO daoObject;
......
在那里我试图注入如果我只有一个实现注入对象(JpaRepository)的实现,那么没有问题,但如果我有更多的注入对象(JpaRepository)那么Spring就不知道它要注入哪个对象并引发异常。
现在的问题是:如何根据通用信息来激活,注入正确的对象?
谢谢。
public interface IRegisteredUserDAO扩展JpaRepository< ; RegisteredUser,String> {
}
公共接口IMailLogDao扩展JpaRepository< MailLog,Long> {
findByTo(String to);
}
我使用它
public class RegisteredUserVM extends YBTableViewModel< RegisteredUser,IRegisteredUserDAO>
{
更新:
public class MailLogVM extends YBTableViewModel< MailLog,IMailLogDao>
{
}
你应该可以使用最新的Spring 4 RC1来做到这一点。之前的版本根本不支持通用注入。有关详细信息,请参阅此故事单和相关提交。
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>
{
}
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.
这篇关于弹簧。动态依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!