如何解决Spring IoC和通用接口类型
由于擦除,我认为这是不可能的。在进行全自动布线时,我们通常切换到强类型子接口:
public interface LongService extends ISimpleService<Long> {}
public interface StringService extends ISimpleService<String> {}
进行此切换后,我们发现我们实际上非常喜欢此功能,因为它使我们能够更好地进行“查找使用情况”跟踪,而使用泛型接口则有些松懈。
解决方法
我正在尝试将Spring IoC与这样的接口一起使用:
public interface ISimpleService<T> {
void someOp(T t);
T otherOp();
}
Spring可以基于通用类型参数T提供IoC吗?我的意思是这样的:
public class SpringIocTest {
@Autowired
ISimpleService<Long> longSvc;
@Autowired
ISimpleService<String> strSvc;
//...
}
当然,上面的例子不起作用:
expected single matching bean but found 2: [serviceLong,serviceString]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:957)
我的问题:是否可以提供对接口或实现类进行最少修改的类似功能?例如,我知道我可以使用@Qualifiers,但我想使事情尽可能简单。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。