厦门专业网站建设建站,好的建站软件,linux wordpress安装,e时代速递搜索引擎网站建设Spring AOP注解例子一#xff1a;导入相关jar包。首先导入Spring的相关包(这里就不多说了#xff0c;我这里是3.2.4版本的)然后导入AOP注解的相关包(不是spring的包)aspectjrt-1.6.7.jar和aspectjweaver-1.6.8.jar和aopalliance.jar(注意这里最好是1.6.7以上的版本#xff0…Spring AOP注解例子一导入相关jar包。首先导入Spring的相关包(这里就不多说了我这里是3.2.4版本的)然后导入AOP注解的相关包(不是spring的包)aspectjrt-1.6.7.jar和aspectjweaver-1.6.8.jar和aopalliance.jar(注意这里最好是1.6.7以上的版本不然容易出错折腾了我好久最后才发现是包的版本问题。所以这里一定要注意spring 2.0以后的最好是用1.6.7的版本)二: 建一个class类作为切入面(这个只是用来演示而已这个切面是拦截com.afmobi.service包下的所有类的所有方法)package com.afmobi.frame;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;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.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;AspectComponentpublic class AopLogTest {Pointcut(execution(* com.afmobi.service.*.*(..)))public void anyMethod(){}Before(anyMethod())public void before() {System.out.println(---Aop 的 前置通知---);}AfterReturning(anyMethod())public void doAfter(){System.out.println(---Aop 的 后置通知---);}After(anyMethod())public void after(){System.out.println(---Aop 的最终通知(发生异常也会通知)---);}AfterThrowing(anyMethod())public void doAfterThrow(){System.out.println(---Aop 的抛出异常通知---);}Around(anyMethod())public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{System.out.println(---Aop 的 环绕通知 通知---);Object object pjp.proceed();//执行该方法return object;}}三: 在spring的配置文件里配置这个切面类(这里按spring AOP注解的方式配置不是xml的方式)注意核心AOP注解配置就是和到此就ok了。