spring通知的注解
1、代理类接口Person.java
1 package com.xiaostudy; 2 3 /** 4 * @desc 被代理类接口 5 * 6 * @author xiaostudy 7 * 8 */ 9 public interface Person { 10 11 public void add(); 12 public void update(); 13 public void delete(); 14 }
2、代理类PersonImple.java
1 package com.xiaostudy; 2 3 import org.springframework.stereotype.Component; 4 5 /** 6 * @desc 被代理类 7 * 8 * @author xiaostudy 9 * 10 */ 11 @Component("person")//类注解 12 public class PersonImple implements Person { 13 14 /** 15 * @desc 实现接口方法 16 */ 17 public void add() { 18 System.out.println("add()....."); 19 } 20 21 @Override 22 public void update() { 23 System.out.println("update()....."); 24 // int i = 1/0; 25 } 26 27 @Override 28 public void delete() { 29 System.out.println("delete()....."); 30 } 31 32 }
3、通知类MyAspectJ.java
1 package com.xiaostudy; 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 import org.springframework.stereotype.Component; 13 14 /** 15 * @desc 通知类 16 * 17 * @author xiaostudy 18 * 19 */ 20 @Component//类注解 21 @Aspect//AspectJ注解 22 public class MyAspectJ { 23 24 //声明公共切入点 25 @Pointcut("execution(* com.xiaostudy.PersonImple.*(..))") 26 public void myPointcut() { 27 28 } 29 30 //前置通知注解,只有一个参数时,value可以省略不写 31 @Before("execution(* com.xiaostudy.PersonImple.*(..))") 32 public void myBefort(JoinPoint joinPoint) { 33 System.out.println("前置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()); 34 } 35 36 //后置通知注解,当参数大于1时,value必须写 37 @AfterReturning(value="myPointcut()", returning="ret") 38 public void myAfterReturning(JoinPoint joinPoint, Object ret) { 39 System.out.println("后置通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName() 40 + ", ret: " + ret); 41 } 42 43 //环绕通知注解 44 @Around("myPointcut()") 45 public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable { 46 System.out.println("环绕通知====前>>>>>>>>>>>"); 47 Object obj = joinPoint.proceed(); 48 System.out.println("环绕通知====后<<<<<<<<<<<"); 49 return obj; 50 } 51 52 //异常通知注解 53 @AfterThrowing(value="myPointcut()", throwing="e") 54 public void myThrowint(JoinPoint joinPoint, Throwable e) { 55 System.out.println("异常通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName() 56 + ", e: " + e.getMessage()); 57 System.exit(0); 58 } 59 60 //最终通知注解 61 @After("myPointcut()") 62 public void myAfter(JoinPoint joinPoint) { 63 System.out.println("最终通知>>>>>>>>>joinPoint: " + joinPoint.getSignature().getName()); 64 } 65 }
4、spring配置文件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="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop 9 http://www.springframework.org/schema/aop/spring-aop.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd"> 12 <!-- 扫描注解类 --> 13 <context:component-scan base-package="com.xiaostudy"></context:component-scan> 14 <!-- 确定 AOP注解生效 --> 15 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 16 </beans>
5、测试类Test.java
1 package com.xiaostudy; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 /** 7 * @desc 测试类 8 * 9 * @author xiaostudy 10 * 11 */ 12 public class Test { 13 14 public static void main(String[] args) { 15 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 16 Person person = ac.getBean("person", Person.class); 17 person.add(); 18 person.update(); 19 person.delete(); 20 } 21 22 }