【发布时间】: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