Spring3.0中的AOP方子法
Spring3.0中的AOP配方法
<h2><span style="color: #0000ff;">第一种配置方法:使用@aspectj标签</span></h2>
<h3>示例:</h3>
<h2><span style="color: #0000ff;">第二种配置方法:基于配置文件的配置</span></h2>
使用第二种方式的aop配置,在eclipse(有springide插件)中被拦截到的方法中有标识显示
</em>
<em>以上配置基于spring 3.0.5 进行设置,参考其《reference documentation》
<h2><span style="color: #0000ff;">第一种配置方法:使用@aspectj标签</span></h2>
- 在配置文件中添加<span style="white-space: pre;"><aop:aspectj-autoproxy/></span>注解
- 创建一个java文件,使用@aspect注解修饰该类
- 创建一个方法,使用@before、@after、@around等进行修饰,在注解中写上切入点的表达式
<h3>示例:</h3>
import org.aspectj.lang.proceedingjoinpoint;import org.aspectj.lang.annotation.after;import org.aspectj.lang.annotation.afterthrowing;import org.aspectj.lang.annotation.around;import org.aspectj.lang.annotation.aspect;import org.aspectj.lang.annotation.before;import org.springframework.stereotype.component;/** * 基于注解的aop日志示例 * @author zywang 2011-3-24 */@component@aspectpublic class aoplog { //方法执行前调用 @before("execution (* com.zywang.services.impl.*.*(..))") public void before() { system.out.println("before"); } //方法执行后调用 @after("execution (* com.zywang.services.impl.*.*(..))") public void after() { system.out.println("after"); } //方法执行的前后调用 @around("execution (* com.zywang.services.impl.*.*(..))") public object around(proceedingjoinpoint point) throws throwable{ system.out.println("begin around"); object object = point.proceed(); system.out.println("end around"); return object; } //方法运行出现异常时调用 @afterthrowing(pointcut = "execution (* com.zywang.services.impl.*.*(..))",throwing = "ex") public void afterthrowing(exception ex){ system.out.println("afterthrowing"); system.out.println(ex); }}<span style="color: #000000;"><span style="color: #0000ff;">上面这段代码中多次使用了重复的切入点,这种情况下,可以使用@pointcut标注,来修改一个切入点方法(这个方法不需要参数和方法体),然后就可以在@before等标注中引用该方法作为切入点,示例如下:</span></span>
import org.aspectj.lang.proceedingjoinpoint;import org.aspectj.lang.annotation.around;import org.aspectj.lang.annotation.aspect;import org.aspectj.lang.annotation.before;import org.aspectj.lang.annotation.pointcut;import org.springframework.stereotype.component;/** * 基于注解的aop日志示例 * @author zywang 2011-3-24 */@component@aspectpublic class aoplog { @pointcut("execution (* com.iflysse.school.services.impl.*.*(..))") public void pointcut(){} //方法执行前调用 @before("pointcut()") public void before() { system.out.println("before"); } //方法执行的前后调用 @around("pointcut()") public object around(proceedingjoinpoint point) throws throwable{ system.out.println("begin around"); object object = point.proceed(); system.out.println("end around"); return object; }}<span style="color: #000000;"></span>
<h2><span style="color: #0000ff;">第二种配置方法:基于配置文件的配置</span></h2>
- 创建一个java文件,并指定一个用于执行拦截的方法,该方法可以有0个或多个参数
- 在spring配置文件中注册该java类为一个bean
- 使用<aop:config/>、<aop:aspect/>等标签进行配置
import org.aspectj.lang.proceedingjoinpoint;/** * 基于配置文件的aop日志示例 * @author zywang 2011-3-24 */public class aoplog { //方法执行的前后调用 public object runonaround(proceedingjoinpoint point) throws throwable{ system.out.println("begin around"); object object = point.proceed(); system.out.println("end around"); return object; } }spring配置文件
<bean id="aoplog" class="com.iflysse.school.aop.aoplog"></bean> <aop:config> <aop:aspect ref="aoplog"> <aop:around method="runonaround" pointcut="execution (* com.zywang.services.impl.*.*(..))"/> </aop:aspect> </aop:config>注意:上面这个示例使用的是around方式的拦截,该方法要求java类中的方法有一个proceedingjoinpoint类型的参数
使用第二种方式的aop配置,在eclipse(有springide插件)中被拦截到的方法中有标识显示
</em>
<em>以上配置基于spring 3.0.5 进行设置,参考其《reference documentation》