Spring AOP 总结

来源:互联网 发布:淘宝活动第三方平台 编辑:程序博客网 时间:2024/06/06 05:58

连接点(Joinpoint):类中可以被增强方法,这些方法称为连接点;

切入点(Pointcut):实际被增强的类;

通知/增强(Advice):增强的逻辑;

前置通知:在方法之前执行;

后置通知:在方法之后执行;

异常通知:方法出现异常;

最终通知:在后置之后执行;

环绕通知:在方法之前和之后通知;

切面(Aspect):把增强应用到具体方法上面的过程称为切面;

实现AOP功能:


public class test{public void add(){}public void delete(){}public void update(){}public void find(){}} class test1{public void before(){System.out.println("前置增强");}public void after(){System.out.println("后置增强");}public void around(ProceedingJoinPoint pjp){System.out.println("环绕前置");pjp.Proceed();System.out.println("环绕后置");}}

1,基于xml实现

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  <bean id="common" class="com.spring.aop.Test"/>  <bean id="check" class="com.spring.aop.Test1"/>  <!--配置AOP操作-->  <aop:config>    <!--配置 切入点--><aop:pointcut id="target" expression="execution(* com.spring.aop.Test.add(..))"/> <!--配置切面,把增强用到方法上-->    <aop:aspect id="myAop" ref="check"><!--配置增强类型, method:增强类中方法--> <!--前置增强--><aop:before method="before" pointcut-ref="target"/><!--后置增强--><aop:after-returning method="after" pointcut-ref="target"/><!--环绕增强--><aop:around method="around" pointcut-ref="target"/></aop:aspect></aop:config></beans>

切入点表达式:

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

1,execution(* com.spring.aop.Test.add(..))//切入点为add方法

2,execution(* com.spring.aop.Test.*(..))//Test类中的所有方法

3,execution(* *.*(..))//所有方法

4,execution(* add*(..))//所有add开头的方法


       2,基于注解实现AOP


  

原创粉丝点击