SPRING_AOP_实现

xml配置实现aop 【SPRING_AOP_实现】1)需要导入jar包:
spring-aop-4.3.5.RELEASE.jar
aopalliance.jar
aspectjrt.jar aspectj-1.8.2\lib
aspectjweaver.jar aspectj-1.8.2\lib
2)引入aop的名称空间


3)创建切面类对象和aop配置


切入点表达式用法:
参考帮助文档11.2.3 Declaring a pointcut Some examples of common pointcut expressions are given below. ?the execution of any public method: execution(public * *(..)) ?the execution of any method with a name beginning with "set": execution(* set*(..)) ?the execution of any method defined by the AccountService interface: execution(* com.xyz.service.AccountService.*(..)) ?the execution of any method defined in the service package: execution(* com.xyz.service.*.*(..)) ?the execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..)) ?any join point (method execution only in Spring AOP) within the service package: within(com.xyz.service.*) ?any join point (method execution only in Spring AOP) within the service package or a sub-package: within(com.xyz.service..*) ?any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface: this(com.xyz.service.AccountService)

五类通知类型:
前置通知[Before advice]:在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常。
正常返回通知[After returning advice]:在连接点正常执行完成后执行,如果连接点抛出异常,则不会执行。
异常返回通知[After throwing advice]:在连接点抛出异常后执行。
返回通知[After (finally) advice]:在连接点执行完成后执行,不管是正常执行完成,还是抛出异常,都会执行返回通知中的内容。
环绕通知[Around advice]:环绕通知围绕在连接点前后,比如一个方法调用的前后。这是最强大的通知类型,能在方法调用前后自定义一些操作。环绕通知还需要负责决定是继续处理join point(调用ProceedingJoinPoint的proceed方法)还是中断执行。
4 注解方式实现aop
@Aspect
@PointCut
@Before
@Component
@Aspect //指定当前类为切面类
public class LogAop {// 指定切入点表单式: 拦截哪些方法; 即为哪些类生成代理对象 @Pointcut("execution(* com.rr.xmlaop.MyDao.*(..))") public void pointcut(){}//前置通知 执行目标方法前执行 @Before("pointcut()") public void begin(JoinPoint jp) { System.out.println("begin:"); System.out.println(jp.getSignature().getName()); }//后置通知,执行目标方法后执行,不论是否异常 @After("pointcut()") //public void end(){ public void end(JoinPoint jp) throws Exception, SecurityException {}//正常返回后通知,调用目标方法结束后执行,在代理返回前执行,异常不执行 @AfterReturning("pointcut()") public void afterReturn(){ System.out.println("after return"); }//环绕方法,必须使用ProceedingJoinPoint参数 @Around("pointcut()") public void around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("环绕前...."); pjp.proceed(); // 执行目标方法 System.out.println("环绕后...."); } }

aop的扫描

    推荐阅读