我知道在Spring2.5中没有直接支持JAVA泛型的注入支持,在Spring3后能够支持,网上为找到怎么配置的。现在我贴出我的泛型Spring的实现,然后再说产生的异常。TestClass1
TestClass2package com.entity;public class TestClass1 {
public void returnClass() {
System.out.println("得到了TestClass1类对象");
}
}
package com.entity;public class TestClass2 {
public void returnClass() {
System.out.println("得到了TestClass2类对象");
}
}GenericInter接口package com.generic;public interface GenericInter<T> {
T findById();
}
实现上面的接口 GenericImpl
package com.generic;import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;public class GenericImpl<T> implements GenericInter<T> { private Class<T> classType; public GenericImpl(Class<T> classType) {
// Type genType = getClass().getGenericSuperclass();
// Type[] params = ((ParameterizedType)
// genType).getActualTypeArguments();
// this.classType = (Class) params[0];
this.classType = classType;
} @SuppressWarnings("unchecked")
public T findById() {
return (T) classType;
}
}Spring的applicationContext.xml配置内容<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="testClass1" class="com.entity.TestClass1" />
<bean id="testClass2" class="com.entity.TestClass2" /> <bean id="genericImpl1" class="com.generic.GenericImpl">
<constructor-arg value="com.entity.TestClass1" />
</bean>
<bean id="genericImpl2" class="com.generic.GenericImpl">
<constructor-arg value="com.entity.TestClass1" />
</bean>
</beans>测试入口类package com.test;import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.entity.TestClass1;
import com.generic.GenericImpl;public class Client {
public static void main(String[] args) {
/// 采用Spring的注入方式获得泛型后的对象
/// BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
/// GenericImpl impl1 = (GenericImpl) beanFactory.getBean("genericImpl1"); ///直接访问泛型对象
GenericImpl<TestClass1> impl1 = new GenericImpl<TestClass1>(TestClass1.class);
TestClass1 testClass1 = (TestClass1) impl1.findById();
testClass1.returnClass();
}
}上面两种方式都会,输出的结果产生的类型强制转换异常Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to com.entity.TestClass1
at com.test.Client.main(Client.java:16)求解。
TestClass2package com.entity;public class TestClass1 {
public void returnClass() {
System.out.println("得到了TestClass1类对象");
}
}
package com.entity;public class TestClass2 {
public void returnClass() {
System.out.println("得到了TestClass2类对象");
}
}GenericInter接口package com.generic;public interface GenericInter<T> {
T findById();
}
实现上面的接口 GenericImpl
package com.generic;import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;public class GenericImpl<T> implements GenericInter<T> { private Class<T> classType; public GenericImpl(Class<T> classType) {
// Type genType = getClass().getGenericSuperclass();
// Type[] params = ((ParameterizedType)
// genType).getActualTypeArguments();
// this.classType = (Class) params[0];
this.classType = classType;
} @SuppressWarnings("unchecked")
public T findById() {
return (T) classType;
}
}Spring的applicationContext.xml配置内容<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="testClass1" class="com.entity.TestClass1" />
<bean id="testClass2" class="com.entity.TestClass2" /> <bean id="genericImpl1" class="com.generic.GenericImpl">
<constructor-arg value="com.entity.TestClass1" />
</bean>
<bean id="genericImpl2" class="com.generic.GenericImpl">
<constructor-arg value="com.entity.TestClass1" />
</bean>
</beans>测试入口类package com.test;import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.entity.TestClass1;
import com.generic.GenericImpl;public class Client {
public static void main(String[] args) {
/// 采用Spring的注入方式获得泛型后的对象
/// BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
/// GenericImpl impl1 = (GenericImpl) beanFactory.getBean("genericImpl1"); ///直接访问泛型对象
GenericImpl<TestClass1> impl1 = new GenericImpl<TestClass1>(TestClass1.class);
TestClass1 testClass1 = (TestClass1) impl1.findById();
testClass1.returnClass();
}
}上面两种方式都会,输出的结果产生的类型强制转换异常Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to com.entity.TestClass1
at com.test.Client.main(Client.java:16)求解。
解决方案 »
- 求教:为什么代码执行到 stmt.executeUpdate() 就诡异的停止了??
- 一个神奇的问题,关于TOMCAT的。不知道大家遇到过没有?
- ssh2+tomcat+sqlserver2005 的一个项目配置jndi时报错,具体如下:
- 请指教 myeclipse中运行无法初始化端点 不懂什么意思
- crud的问题
- 急!!Hibernate查询出来的数据与数据库中的不一样???!!!
- myeclipse
- 有关 XML 名字空间定义的问题
- 初学java,实验java小程序用什么环境,在哪里可以免费下载?
- 通过用户id查询用户信息(求助)
- jsp页面,值为空,就不显示null
- 关于springMVC配置文件的问题
class GenericImpl<T> implements GenericInter<T> { private Class<T> classType; public GenericImpl(Class<T> classType) {
// Type genType = getClass().getGenericSuperclass();
// Type[] params = ((ParameterizedType)
// genType).getActualTypeArguments();
// this.classType = (Class) params[0];
this.classType = classType;
} public T findById() {
try {
return classType.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}