Spring 4 通用 类 的自动装配在测试中有效,部署到服务器时无效

Spring 4 autowiring of generic classes works in test, doesn't work when deployed to server

我有泛型 类,它根据泛型类型自动装配依赖项,如下所示:

public abstract class GenericRestService<C extends AbstractTenantEntity> extends RestResource<C> {

    protected final Logger log;
    @Autowired(required = false)
    protected GenericResourceService<C> service;
    @Autowired(required = true)
    protected JpaRepository<C, Long> repo;

 // Now use service if one with specified generic type C is found, otherwise use repo

在我的 spring 测试中,一切正常:如果没有 GenericResourceService<C> 和具体泛型类型 C 被定义,则没有任何东西被注入到字段 service 中,我很乐意使用repo 自动装配。

然而,当我 运行 我的应用程序在真实环境中时,总是会注入一些 GenericResourceService<C> 的实现,无论它的泛型类型是否与依赖项所需的类型匹配。

您可能会找到多个 bean 定义。也许 spring 4 不支持动态类型泛型自动装配。

这是由 JDK 代理与 cglib 代理冲突引起的。一个用于部署,另一个用于测试。