考虑工作示例。
<bean id="simpleInt"
class="org.nipr.gateway.service.transaction_assistant.GenericSimple">
<constructor-arg>
<!-- this can be any full path to a class -->
<value>java.lang.Integer</value>
</constructor-arg>
</bean>
和
<bean id="simpleString"
class="org.nipr.gateway.service.transaction_assistant.GenericSimple">
<constructor-arg>
<value>java.lang.String</value>
</constructor-arg>
</bean>
简单泛型类:
public class GenericSimple<T> {
private Class<T> type;
public GenericSimple(Class<T> type) {
this.type = type;
}
public T get( T t) {
return t;
}
}
最后,测试方法(使用工厂):
public void testGeneric(){
Factory factory = new Factory(new String[]{"config/beanForGenericTest.xml"});
GenericSimple<Integer> simpleInt
= (GenericSimple<Integer>)factory.getClass("simpleInt");
System.out.println(simpleInt.get(Integer.valueOf(100)));
Assert.assertTrue(simpleInt.get(Integer.valueOf(100)).equals(100));
GenericSimple<String> simpleString =
(GenericSimple<String>)factory.getClass("simpleString");
System.out.println(simpleString.get(new String("Rockets go fast.")));
Assert.assertTrue(simpleString.get("Rockets go fast.")
.equals("Rockets go fast."));
}