您的位置:首页 > 编程语言 > Java开发

Spring2中aop的使用及遇到的一些问题

2016-11-07 09:11 253 查看
aop在spring中有两中实现的方式一种是xml,一种是基于注解的,我主要通过xml方式来实现
首先是要引入相关的jar包,如aspectjrt.jar,aspectjweaver.jar
<bean id="aaa" class="aaa"></bean>
<aop:config>
<aop:aspect ref="aaa">
<aop:pointcut id="login"
expression="execution(* bbbbbb*.checkLogin*(..))"/>
<aop:after-returning  method="loginLog" pointcut-ref="login"/>
</aop:aspect>
</aop:config>

execution(* bbbbbb*.checkLogin*(..)
* 返回类型
bbbbbb* 包路径
checkLogin* 方法名
.. 参数
method="loginLog" aaa中调用方法

pointcut-ref="login" target方法

execution(* *(..)) and !execution(* *(..)) 包含并且不包含指定路径中方法

aop:after-returning  方法之后调用
aop:around 方法前后
aop:before 调用之前
aop:throw 异常之后

在拦截方法时,原方法如果有异常抛出,要捕捉业务异常,并继续抛出
注意在指定包中存在的方法重名的现象
在websphere环境中,加入aop之后,带来了循环依赖的问题,因为在原有的程序中,存在着循环依赖的情况, 在没有加入aop之后,没有显现,加入之后,则出现
has been injected into other beans xxxxx
in its raw version as part of a circular reference, but has eventually been wrapped (for example as part of
auto-proxy creation). This means that said other beans do not use the final version of the bean. This is
often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit'
flag turned off, for example
最终去掉了引用,建立新的第三方对象,解决这个问题.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: