一、准备
1.架包
2.配置文件
二、注解的形式
UserDao.java
1 package cn.itcast.spring.aspectj.annocation; 2 3 public class UserDao { 4 5 public void add(){ 6 System.out.println("add......"); 7 } 8 9 public void delete(){ 10 System.out.println("delete......"); 11 int i = 1/0; 12 } 13 14 public String update(){ 15 System.out.println("update......"); 16 17 return "我被返回了"; 18 } 19 20 public void find(){ 21 System.out.println("find......"); 22 } 23 24 public void edit(){ 25 System.out.println("edit"); 26 } 27 28 }
MyAspectj.java
1 package cn.itcast.spring.aspectj.annocation; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.ProceedingJoinPoint; 5 import org.aspectj.lang.annotation.After; 6 import org.aspectj.lang.annotation.AfterReturning; 7 import org.aspectj.lang.annotation.AfterThrowing; 8 import org.aspectj.lang.annotation.Around; 9 import org.aspectj.lang.annotation.Aspect; 10 import org.aspectj.lang.annotation.Before; 11 import org.aspectj.lang.annotation.Pointcut; 12 13 /** 14 * 定义切面类: 切点和通知 15 * @author Administrator 16 * 17 */ 18 @Aspect//切面类的标识 19 public class MyAspectj { 20 21 /** 22 * 前置通知 23 */ 24 @Before("MyAspectj.myPointcut()") 25 public void before(JoinPoint point){ 26 System.out.println("前置通知"); 27 System.out.println("方法名"+point.getSignature().getName()); 28 } 29 30 /** 31 * 后置通知 32 */ 33 @AfterReturning(value="execution(* cn.itcast.spring.aspectj.annocation.UserDao.update(..))",returning="val") 34 public void afterReturning(JoinPoint point,Object val){ 35 System.out.println("后置通知"); 36 System.out.println("方法名"+point.getSignature().getName()); 37 System.out.println("返回值:"+val); 38 } 39 40 /** 41 * 环绕增强 42 * @throws Throwable 43 */ 44 @Around(value="execution(* cn.itcast.spring.aspectj.annocation.UserDao.find(..))") 45 public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ 46 System.out.println("环绕前"); 47 Object result = proceedingJoinPoint.proceed();//可以组织方法执行 48 System.out.println("环绕后"); 49 return result; 50 } 51 52 /* 53 * 异常通知 54 */ 55 @AfterThrowing(value="execution(* cn.itcast.spring.aspectj.annocation.UserDao.delete(..))",throwing="tw") 56 public void afterThrowing(Throwable tw){ 57 System.out.println("异常通知"); 58 System.out.println(tw.getMessage()); 59 } 60 61 /* 62 * 最终通知 63 */ 64 @After("execution(* cn.itcast.spring.aspectj.annocation.UserDao.edit(..))") 65 public void after(){ 66 System.out.println("最终通知"); 67 } 68 69 /* 70 * 定义一个切点,通用型的表达式 71 * 使用切面类.切点() 72 */ 73 74 @Pointcut("execution(* cn.itcast.spring.aspectj.annocation.UserDao.add(..))") 75 private void myPointcut(){}; 76 77 78 }
TestAspectjAnnocation.java
1 package cn.itcast.spring.aspectj.annocation; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Qualifier; 7 import org.springframework.test.context.ContextConfiguration; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 10 @RunWith(SpringJUnit4ClassRunner.class) 11 @ContextConfiguration(locations="classpath:applicationContext.xml") 12 public class TestAspectjAnnocation { 13 @Autowired 14 @Qualifier("userDao") 15 private UserDao userDao; 16 17 @Test 18 public void testAspectjAnnocation(){ 19 userDao.add(); 20 //userDao.delete(); 21 } 22 23 24 }
applicationContxt.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 10 11 <!-- 引入目标类 和切面类--> 12 <bean id="userDao" class="cn.itcast.spring.aspectj.annocation.UserDao"></bean> 13 <bean id="myAspectj" class="cn.itcast.spring.aspectj.annocation.MyAspectj"></bean> 14 15 <!-- 使用aop的自动代理自动扫描 --> 16 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 17 18 </beans>
三、使用xml的形式
UserDao.java
1 package cn.itcast.spring.aspectj.xml; 2 3 public class UserDao { 4 5 public void add(){ 6 System.out.println("add......"); 7 } 8 9 public void delete(){ 10 System.out.println("delete......"); 11 int i = 1/0; 12 } 13 14 public String update(){ 15 System.out.println("update......"); 16 17 return "我被返回了"; 18 } 19 20 public void find(){ 21 System.out.println("find......"); 22 } 23 24 public void edit(){ 25 System.out.println("edit"); 26 } 27 28 }
MyAspectj.java
1 package cn.itcast.spring.aspectj.xml; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.ProceedingJoinPoint; 5 import org.aspectj.lang.annotation.After; 6 import org.aspectj.lang.annotation.AfterReturning; 7 import org.aspectj.lang.annotation.AfterThrowing; 8 import org.aspectj.lang.annotation.Around; 9 import org.aspectj.lang.annotation.Aspect; 10 import org.aspectj.lang.annotation.Before; 11 import org.aspectj.lang.annotation.Pointcut; 12 13 /** 14 * 定义切面类: 切点和通知 15 * @author Administrator 16 * 17 */ 18 19 public class MyAspectj { 20 21 /** 22 * 前置通知 23 */ 24 25 public void before(JoinPoint point){ 26 System.out.println("前置通知"); 27 System.out.println("方法名"+point.getSignature().getName()); 28 } 29 30 /** 31 * 后置通知 32 */ 33 public void afterReturning(JoinPoint point,Object val){ 34 System.out.println("后置通知"); 35 System.out.println("方法名"+point.getSignature().getName()); 36 System.out.println("返回值:"+val); 37 } 38 39 /** 40 * 环绕增强 41 * @throws Throwable 42 */ 43 public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ 44 System.out.println("环绕前"); 45 Object result = proceedingJoinPoint.proceed();//可以组织方法执行 46 System.out.println("环绕后"); 47 return result; 48 } 49 50 /* 51 * 异常通知 52 */ 53 public void afterThrowing(Throwable tw){ 54 System.out.println("异常通知"); 55 System.out.println(tw.getMessage()); 56 } 57 58 /* 59 * 最终通知 60 */ 61 public void after(){ 62 System.out.println("最终通知"); 63 } 64 65 66 }
TestAspectjXml.java
1 package cn.itcast.spring.aspectj.xml; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Qualifier; 7 import org.springframework.test.context.ContextConfiguration; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 10 @RunWith(SpringJUnit4ClassRunner.class) 11 @ContextConfiguration(locations="classpath:applicationContext2.xml") 12 public class TestAspectjXML { 13 @Autowired//自动装配bean 14 @Qualifier("userDao") 15 private UserDao userDao; 16 17 @Test 18 public void testAspectjAnnocation(){ 19 System.out.println(userDao); 20 userDao.add(); 21 //userDao.delete(); 22 } 23 24 25 }
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 10 11 <!-- 引入目标类 和切面类--> 12 <bean id="userDao" class="cn.itcast.spring.aspectj.xml.UserDao"></bean> 13 <bean id="myAspectj" class="cn.itcast.spring.aspectj.xml.MyAspectj"></bean> 14 15 <!-- aop的配置 --> 16 <aop:config> 17 <!-- 定义切入点:也就是也要增强的哪些方法 --> 18 <aop:pointcut expression="execution(* cn.itcast.spring.aspectj.xml.UserDao.add(..))" id="myPointcut1"/> 19 <aop:pointcut expression="execution(* cn.itcast.spring.aspectj.xml.UserDao.find(..))" id="myPointcut2"/> 20 <aop:pointcut expression="execution(* cn.itcast.spring.aspectj.xml.UserDao.delete(..))" id="myPointcut3"/> 21 <aop:pointcut expression="execution(* cn.itcast.spring.aspectj.xml.UserDao.update(..))" id="myPointcut4"/> 22 <aop:pointcut expression="execution(* cn.itcast.spring.aspectj.xml.UserDao.edit(..))" id="myPointcut5"/> 23 <!-- 切面类的配置 --> 24 <aop:aspect ref="myAspectj"> 25 <aop:before method="before" pointcut-ref="myPointcut1"/> 26 <aop:after-returning method="afterReturning" returning="val" pointcut-ref="myPointcut2" /> 27 <aop:after-throwing method="afterThrowing" throwing="tw" pointcut-ref="myPointcut3"/> 28 <aop:around method="around" pointcut-ref="myPointcut4"/> 29 <!-- 最终通知 --> 30 <aop:after method="after" pointcut-ref="myPointcut5"/> 31 </aop:aspect> 32 33 </aop:config> 34 </beans>