Spring自动接线和通用类

时间:2019-04-10 18:16:29

标签: java spring generics autowired

我有一个通用的数据访问类:

public class DAO<T extends OrganisationalEntity, TR extends OrganisationalRepository<T>> {

    @Autowired OrganisationRepository organisationRepository;
    @Autowired TR entityRepository;
    ...

}

具有一个实体类型(OrganisationalEntity的子类型和一个存储库类型(OrganisationalRepository<T>的子类型作为类型参数)。

OrganisationalEntity是一个接口:

public interface OrganisationalEntity {

    Organisation getOrganisation();
    void setOrganisation(Organisation organisation);
}

OrganisationalRepository也是如此,它扩展了JpaRepository接口:

public interface OrganisationalRepository<T extends OrganisationalEntity> 
    extends JpaRepository<T, Long> {...}

由此,我尝试为特定数据类型创建数据访问类:

@Component
public class IssueDAO extends DAO<Issue, IssueRepository> {...}

然后在我的控制器中自动连接此数据访问类:

@RestController
@Transactional
class IssueController {
    ...
    @Autowired 
    IssueDAO issueDAO;
    ...
}

但这会导致以下错误:

...
Error creating bean with name 'issueDAO'
...
Failed to instantiate [IssueDAO]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Expected 1 type argument on generic interface [DAO<Issue, IssueRepository>] but found 2
...

如果有2个参数,为什么要期待1个参数? 甚至错误消息也列出了2种类型的参数。

0 个答案:

没有答案