【问题标题】:Trying to inject two repositories with generic implementation is failing尝试使用通用实现注入两个存储库失败
【发布时间】:2016-01-15 14:08:02
【问题描述】:

我正在尝试使用泛型编写两个模型类的实现代码。

我有两个模型类:

@Entity
@Table(name = "SMS_INFO")
public class SmsInfo
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = Constants.ID_COLUMN)
    private Long smsInfoId;

    // other fields and public getters
}

EmailInfo 也有类似的模型类。

现在,对于这两个类,我正在尝试创建通用存储库和服务类,如下所示:

public interface InfoRepository <Info> extends JpaRepository<Info, Long> {}

public interface CommunicationInfoServiceI <Info>
{
    // Some abstract methods
}

@Named
public class CommunicationInfoServiceImpl<Info> implements CommunicationInfoServiceI<Info>
{
    @Inject
    private InfoRepository<Info> infoRepository;

    // Other implementations
}

现在,我尝试如下注入这两个服务:

@Named
@Singleton
public class ServiceFactory
{
    @Inject
    private CommunicationInfoServiceI<SmsInfo> smsInfoService;

    @Inject
    private CommunicationInfoServiceI<EmailInfo> emailInfoService;

    // Other Getter methods
}

但我收到以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceFactory': Injection of autowired dependencies failed; 
    nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private CommunicationInfoServiceI ServiceFactory.smsInfoService; 
    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'communicationInfoServiceImpl': Injection of autowired dependencies failed; 
    nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private InfoRepository CommunicationInfoServiceImpl.infoRepository; 
    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'infoRepository': Invocation of init method failed; 
    nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object

谁能帮帮我,我被困在这里了?

提前致谢。

注意:我已尝试删除泛型类的所有注入并离开 InfoRepository 原样,它仍然给出同样的错误。我认为它 应该不是因为serviceFactory,应该是有事做 使用 JPARepository,最初它可能会尝试注入它并 失败了,因为 JVM 可能不知道“信息”类型。能 我们为此做点什么?

【问题讨论】:

    标签: java spring generics dependency-injection javabeans


    【解决方案1】:

    如果您使用 Guice 进行注入,您应该将接口绑定到模块配置中的实现类。如果你使用 spring 上下文,你应该在 spring config 中定义你的存储库 bean。

    【讨论】:

      【解决方案2】:

      我能够通过为 smsInfo 和 emailInfo 创建一个更常见的父模型类来解决该问题,如下所示:

      @MappedSuperclass
      public class CommunicationInfo
      {
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          @Column(name = Constants.ID_COLUMN)
          protected Long id;
      }
      

      并从 SmsInfo 和 EmailInfo 类扩展它。

      之后我必须在存储库中使用扩展以及泛型类型,如下所示:

      public interface CommunicationInfoRepository <Info extends CommunicationInfo> extends JpaRepository<Info, Long>
      {
      
      }
      

      在其他地方也以同样的方式使用。

      感谢大家的回复。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-24
        • 1970-01-01
        • 1970-01-01
        • 2015-07-07
        • 2017-07-23
        • 2019-05-02
        相关资源
        最近更新 更多