gpt4 book ai didi

java - 尝试注入(inject)两个具有通用实现的存储库失败

转载 作者:行者123 更新时间:2023-12-01 10:34:59 25 4
gpt4 key购买 nike

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

我有两个模型类:

@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
}

现在,我尝试注入(inject)这两个服务,如下所示:

@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

有人可以帮帮我吗,我被困在这里了?

提前致谢。

Note: I have tried removing all injections of generic classes and left InfoRepository as it is, it is still giving the same error. I think it shouldn't be because of serviceFactory, it should be something to do with JPARepository, initially it might be trying to inject it and failing in doing, as JVM might not be knowing about 'Info' type. Can we do something for this?

最佳答案

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

关于java - 尝试注入(inject)两个具有通用实现的存储库失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34812855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com