« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9702221 建立时间:2004年12月20日 |

| |
[java语言]深入浅出 spring AOP (三) 原创空间, 软件技术
邢红瑞 发表于 2005/11/26 9:58:58 |
spring AOP使用,使用CGLIB应该使用接口不是类,这点务必注意。
使用BeanNameAutoProxyCreator声明事务,
<!-- define transaction interceptor -->
<bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="transactionAttributeSource"><ref bean="txAttributes"/></property>
</bean>
<!-- Define transactional methods
(NameMatchTransactionAttributeSource applies specific attributes to
methods that match to a pattern) -->
<bean id="txAttributes" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties"><value>add*=PROPAGATION_REQUIRED
update*=PROPAGATION_REQUIRED
delete*=PROPAGATION_REQUIRED
</value>
</property>
</bean>
<!-- 使用Autoproxy定义事务的beans,应用于Controllers -->
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames"><value>txInterceptor</value></property>
<property name="beanNames"><value>*Controller</value></property>
</bean>
<!-- 事务管理的beans -->
<bean id="userManager" class="com.tatan.domain.user.UserManager" singleton="true" dependency-check="objects">
<constructor-arg index="0"><ref bean="userController"/></constructor-arg>
</bean>
spring文档说明“when BeanNameAutoProxyCreator postprocesses the target
object and create the proxy, it causes the proxy to be inserted into
the Application context under the name of the original bean”,
UserController 应该是个接口,而不是类,默认情况下,spring使用dynamic proxies,它只使用接口。如果使用CGLIB,最好proxyTargetClass设为true。
使用TransactionProxyFactoryBean也是如此
<bean id="MimeTarget" class="com。tatan.task.MimeTarget">
<property
name="sessionFactory"><ref
bean="sessionFactory"/></property>
<property name="MimeDao"><ref bean="MimeDao"/></property>
</bean>
<bean id="MimeTargetProxyFactoryBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property
name="transactionManager"><ref
bean="transactionManager"/></property>
<property name="target"><ref local="MimeTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="MimeTrigger" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<property name="timerTask"><ref bean="MimeTargetProxyFactoryBean"/></property>
<property name="delay"><value>1000</value></property>
<property name="period"><value>500000</value></property>
</bean>
这样会抛出异常,exception org.springframework.beans.TypeMismatchException:
Failed to convert property value of type [$Proxy0] to required type
[java.util.TimerTask] for property 'timerTask'; nested exception is
java.lang.IllegalArgumentException: argument type mismatch
TimerTask是个类,只能用CGLIB产生代理,TransactionProxyFactoryBean 默认使用接口来产生target object,将proxyTargetClass = true即可使用CGLIB proxy代理。
|
|
|