博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

SSH中BaseDao利用泛型和反射报错

Posted on 2013-06-16 19:40  TimeFight  阅读(391)  评论(0)    收藏  举报

public BaseDaoImp() {
  
  ParameterizedType type= (ParameterizedType) this.getClass().getGenericSuperclass();
  this.class1=(Class<T>) type.getActualTypeArguments()[0];
  System.out.println(class1);
 }

原因是 beans.xml中的事务配置

 <!-- 配置事务 --> 
<!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

  <!-- a PlatformTransactionManager is still required -->
  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <!-- (this dependency is defined somewhere else) -->
  <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

:proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理将起作用。

由于项目使用的是基于接口的代理 所以应该把红色部分改成false或者将其去除