如果将字段template定义为泛型,则意味着template只能发送相同的数据类型。U可以将_template`定义为
private final KafkaTemplate<String, Object> _template;
然后添加一个新的私有泛型方法
private <T> void send(String key ,T t){
this._template.send(key, t);
}
整个类定义:
public class ProductProducer implements IProductProducer{
private final KafkaTemplate<String, Object> _template;
public ProductProducer(KafkaTemplate<String, Object> _template) {
this._template = _template;
}
@Override
public ProductViewModel GetProduct(String id) {
this.send(ProductTopicConstants.GET_PRODUCT, id);
return new ProductViewModel("","",0,"");
}
@Override
public void AddProduct(ProductViewModel product) {
this.send(ProductTopicConstants.ADD_PRODUCT, product);
}
private <T> void send(T t){
this._template.send(ProductTopicConstants.GET_PRODUCT, id);
}
}