论坛首页 Java企业应用论坛

ProxyFactoryBean什么时候需要配置proxyTargetClass

浏览 6110 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-10   最后修改:2009-03-10

spring ProxyFactoryBean什么时候需要配置proxyTargetClass

比如现在有这样一段代码:
<bean id="ttransactionDefinition" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="transactionAttributeSource">
            <ref bean="txAttributeSource" />
        </property>
</bean>

<bean id="test" parent="ttransactionDefinition">
 <property name="proxyTargetClass" value="true"></property>
 <property name="target">
  <bean class="com.alibaba.intl.biz.escrow.message.TestTram" />
 </property>
</bean>

<bean id ="testProxyCls" class="com.wolf.TestProxyClass">
 <property name="test" bean="test"/>
</bean>
proxyTargetClass需要配置的情况只有一种:
  当TestTram类有实现某个接口,而TestProxyClass类中配置的类对象是TestTram时(而不是TestTram实现的接口),这时候你需要配置proxyTargetClass=true
 
  如果TestTram没有实现某个接口,而TestProxyClass类中配置的类对象是TestTram,这个时候我们是不需要配置proxyTargetClass=true的.(使用cgilib来动态代理)
  如果TestTram实现某个接口, 而TestProxyClass类中配置的是TestTram实现的interface的话.那样我既不需要配置proxyInterface,也不需要配置proxyTargetClass

 
 为什么我们在没有配置proxyInterface情况下,去配置proxyTargetClass.因为spring会去拿到当前配置的target实现的所有接口,然后通过动态代理出类.
 
 可以看看spring的这段代码:(AbstractSingletonProxyFactoryBean类)
  if (this.proxyInterfaces != null) {
   proxyFactory.setInterfaces(this.proxyInterfaces);
  }
  else if (!isProxyTargetClass()) {
   // Rely on AOP infrastructure to tell us what interfaces to proxy.
   proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass()));
  }
  this.proxy = getProxy(proxyFactory);

   发表时间:2009-03-10  
写的真够绕的,这些spring手册都已经说的很清楚了!!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics