Ps:访问修饰符,可有可无。Ps:当然 JoinPoint 形参当中也可以去掉的。Ps:around方法返回值为 Object 是因为针对目标方法如果有返回值的情况。
packagecom.imooc.aspectJ.demo1; publicclassProductDao { publicvoidsave(){ System.out.println("保存商品..."); } publicStringupdate(){ System.out.println("修改商品..."); return"hello"; } publicvoiddelete(){ System.out.println("删除商品..."); } publicvoidfindOne(){ System.out.println("查询一个商品..."); //int i = 1/0; } publicvoidfindAll(){ System.out.println("查询所有商品..."); // int j = 1/0; } }
packagecom.imooc.aspectJ.demo1; importorg.aspectj.lang.JoinPoint; importorg.aspectj.lang.ProceedingJoinPoint; importorg.aspectj.lang.annotation.*; /*** 切面类*/@AspectpublicclassMyAspectAnno { @Before(value="myPointcut1()") publicvoidbefore(JoinPointjoinPoint){ System.out.println("前置通知=================="+joinPoint); } @AfterReturning(value="myPointcut2()",returning="result") publicvoidafterReturing(Objectresult){ System.out.println("后置通知=================="+result); } @Around(value="myPointcut3()") publicObjectaround(ProceedingJoinPointjoinPoint) throwsThrowable { System.out.println("环绕前通知================"); Objectobj=joinPoint.proceed(); // 执行目标方法System.out.println("环绕后通知================"); returnobj; } @AfterThrowing(value="myPointcut4()",throwing="e") publicvoidafterThrowing(Throwablee){ System.out.println("异常抛出通知=============="+e.getMessage()); } @After(value="myPointcut5()") publicvoidafter(){ System.out.println("最终通知=================="); } @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.save(..))") privatevoidmyPointcut1(){} @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.update(..))") privatevoidmyPointcut2(){} @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.delete(..))") privatevoidmyPointcut3(){} @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.findOne(..))") privatevoidmyPointcut4(){} @Pointcut(value="execution(* com.imooc.aspectJ.demo1.ProductDao.findAll(..))") privatevoidmyPointcut5(){} }
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--开启AspectJ的注解开发,自动代理--><aop:aspectj-autoproxy/><!--目标类--><beanid="productDao"class="com.imooc.aspectJ.demo1.ProductDao"/><!--定义切面--><beanclass="com.imooc.aspectJ.demo1.MyAspectAnno"/></beans>
packagecom.imooc.aspectJ.demo1; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.test.context.ContextConfiguration; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; importjavax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") publicclassSpringDemo1 { @Resource(name="productDao") privateProductDaoproductDao; @Testpublicvoiddemo1(){ productDao.save(); productDao.update(); productDao.delete(); productDao.findAll(); productDao.findOne(); } }
运行结果:前置通知==================execution(voidcom.imooc.aspectJ.demo1.ProductDao.save()) 保存商品... 修改商品... 后置通知==================hello环绕前通知================删除商品... 环绕后通知================查询所有商品... 最终通知==================查询一个商品...