简单版:
完整版例子:
package com.imooc.aspectJ.demo1; public class ProductDao { public void save(){ System.out.println("保存商品..."); } public String update(){ System.out.println("修改商品..."); return "hello"; } public void delete(){ System.out.println("删除商品..."); } public void findOne() { System.out.println("查询一个商品..."); // int i=1/0; } public void findAll(){ System.out.println("查询所有商品..."); // int i=1/0; } }
package com.imooc.aspectJ.demo1; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; /** * 切面类 */ @Aspect public class MyAspectAnno { @Before(value = "MyPointCut1()||MyPointCut2()") public void before(JoinPoint joinPoint){ System.out.println("前置通知=================="+joinPoint); } @AfterReturning(value = "MyPointCut2()",returning="result") public void afterReturning(JoinPoint joinPoint,Object result){ System.out.println("后置通知============="+joinPoint+",result:"+result); } @Around(value = "MyPointCut3()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("=======环绕前通知==========="); Object obj = joinPoint.proceed(); System.out.println("=======环绕后通知==========="); return obj; } @AfterThrowing(value="MyPointCut4()",throwing = "e") public void afterThrowing(Throwable e){ System.out.println("异常抛出通知==============="+e.getMessage()); } @After(value = "MyPointCut5()") public void after(){ System.out.println("最终通知=========="); } @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.save(..))))") private void MyPointCut1(){} @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.update(..))))") private void MyPointCut2(){} @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.delete(..))))") private void MyPointCut3(){} @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.findOne(..))))") private void MyPointCut4(){} @Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.findAll(..))))") private void MyPointCut5(){} }
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!--开启AspectJ的注解开发,自动代理=========--> <aop:aspectj-autoproxy/> <!--目标类================================--> <bean id="productDao" class="com.imooc.aspectJ.demo1.ProductDao"/> <!--定义切面--> <bean class="com.imooc.aspectJ.demo1.MyAspectAnno"/> </beans>
package com.imooc.aspectJ.demo1; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SpringDemo1 { @Resource(name="productDao") private ProductDao productDao; @Test public void demo1(){ productDao.save(); productDao.update(); productDao.delete(); productDao.findAll(); productDao.findOne(); } }
===============XML方式===============
package com.imooc.aspectJ.demo2; public interface CustomerDao { public void save(); public String update(); public void delete(); public void findOne(); public void findAll(); }
package com.imooc.aspectJ.demo2; public class CustomerDaoImpl implements CustomerDao { @Override public void save() { System.out.println("保存客户..."); } @Override public String update() { System.out.println("修改客户..."); return "spring"; } @Override public void delete() { System.out.println("删除客户..."); } @Override public void findOne() { System.out.println("查询一个客户..."); int i=1/0; } @Override public void findAll() { System.out.println("查询多个客户..."); //int i=1/0; } }
package com.imooc.aspectJ.demo2; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class MyAspectXml { //前置通知 public void before(JoinPoint joinPoint){ System.out.println("XML方式的前置通知=========="+joinPoint); } //后置通知 public void afterRetruning(String result){ System.out.println("XML方式的后置通知=========="+result); } //环绕通知 public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("XML方式的环绕通知前=========="); Object obj=joinPoint.proceed(); System.out.println("XML方式的环绕通知后=========="); return obj; } //异常抛出通知 public void afterThrowing(Throwable e){ System.out.println("XML方式的异常抛出通知============="+e.getMessage()); } //最终通知 public void after(){ System.out.println("XML方式的最终通知通知============="); } }
package com.imooc.aspectJ.demo2; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value="classpath:applicationContext2.xml") public class SpringDemo2 { @Resource(name="customerDao") private CustomerDao customerDao; @Test public void demo1(){ customerDao.save(); customerDao.update(); customerDao.delete(); customerDao.findOne(); customerDao.findAll(); } }
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!--XML的配置方式完成AOP的开发===========--> <!--配置目标类========================--> <bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/> <!--配置切面类--> <bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/> <!--aop的相关配置==============--> <aop:config> <!--配置切入点--> <aop:pointcut id="pointcut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/> <aop:pointcut id="pointcut2" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))"/> <aop:pointcut id="pointcut3" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))"/> <aop:pointcut id="pointcut4" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))"/> <aop:pointcut id="pointcut5" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))"/> <!--配置AOP的切面--> <aop:aspect ref="myAspectXml"> <!--配置前置通知--> <aop:before method="before" pointcut-ref="pointcut1"/> <!--配置后置通知--> <aop:after-returning method="afterRetruning" pointcut-ref="pointcut2" returning="result"/> <!--配置环绕通知--> <aop:around method="around" pointcut-ref="pointcut3"/> <!--配置异常通知--> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/> <!--配置最终通知--> <aop:after method="after" pointcut-ref="pointcut5"/> </aop:aspect> </aop:config> </beans>
一般在实际开发过程中AOP,基本不会使用传统AOP开发。一般使用AspectJ的方式。企业中有些用注解,有些使用XML方式。