本站首页    管理页面    写新日志    退出


«September 2025»
123456
78910111213
14151617181920
21222324252627
282930


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

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代理。


阅读全文(5862) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.281 second(s), page refreshed 144775111 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号