怎样根据泛型的类型自动选择调用不同的实现
环境为J2EE的SSI架构
两个对象、省略setter&getter
一个接口
两个实现
现在调用
也就是需要接口根据我传入的对象类型自动实例化对应的实现。
然而在Spring实例化对象时,需要明确指定实现的路径,如id = "XXX" class = "com.xx.xxx.impl.XxxxImpl"
以上想根据入参的类型自动实例化是不是不能使用Spring的Bean管理了。
还有在调用newInterface.save(m)一直提示空指针,想问一下怎么编码。
谢谢
两个对象、省略setter&getter
public class Material {
private String materialNo;
private String materialType;
private String materialUnit;
}
public class Supply {
private String supplyNo;
private String materialType;
private String materialUnit;
}一个接口
public interface NewInterface<T> {
public void save(T t);
public void select(T t);
}两个实现
public class MaterialImpl implements NewInterface<Material>{
@Override
public void save(Material t) {
System.out.println("MaterialImpl.save " + t.getMaterialNo());
}
@Override
public void select(Material t) {
System.out.println("MaterialImpl.select " + t.getMaterialNo());
}
}
public class SupplyImpl implements NewInterface<Supply>{
@Override
public void save(Supply t) {
System.out.println("SupplyImpl.save " + t.getSupplyNo());
}
@Override
public void select(Supply t) {
System.out.println("SupplyImpl.select " + t.getSupplyNo());
}
}现在调用
private NewInterface<Material> newInterface;
Material m = new Material();
m.setMaterialNo("ddd");
newInterface.save(m);也就是需要接口根据我传入的对象类型自动实例化对应的实现。
然而在Spring实例化对象时,需要明确指定实现的路径,如id = "XXX" class = "com.xx.xxx.impl.XxxxImpl"
以上想根据入参的类型自动实例化是不是不能使用Spring的Bean管理了。
还有在调用newInterface.save(m)一直提示空指针,想问一下怎么编码。
谢谢
...全文


