Original link:
http://www.cnblogs.com/c0liu/p/7469189.html
spring-3 1. Xml <!-- \src\applicationContext-xml.xml --> <?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-4.0.xsd"> <!- the bean<->Configure the bean id="arithmeticCalculator" class="com.atguigu.spring.aop.xml.ArithmeticCalculatorImpl"></bean> <!-- 配置切面的 bean. --> <bean id="loggingAspect" class="com.atguigu.spring.aop.xml.LoggingAspect"></bean> <bean id="vlidationAspect" class="com.atguigu.spring.aop.xml.VlidationAspect"></bean> <!-- 配置 AOP --> <aop:config> <!- <->Configuration pointcut expression aop:pointcut expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(int, int))" id="pointcut"/> <!-- 配置切面及通知 --> <aop:aspect ref="loggingAspect" order="2"> <aop:before method="beforeMethod" pointcut-ref="pointcut"/> <aop:after method="afterMethod" pointcut-ref="pointcut"/> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/> <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/> <!-- <aop:around method="aroundMethod" pointcut-ref="pointcut"/> --> </aop:aspect> <aop:aspect ref="vlidationAspect" order="1"> <aop:before method="validateArgs" pointcut-ref="pointcut"/> </aop:aspect> </aop:config> </beans> <!-- \src\applicationContext.xml --> <?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/context " > <-! configured to automatically scan packages -> < context : Scan-Component Base-Package = "com.atguigu.spring.aop" > </ context: Component-Scan > ! <- configured to automatically match the Java class aspectJ annotation proxy objects -> < AOP: aspectj- the autoproxy > </ AOP: AspectJ-the autoproxy > </ Beans >
2. Java // \src\com\atguigu\spring\aop\ArithmeticCalculator.java package com.atguigu.spring.aop; public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); } // \src\com\atguigu\spring\aop\ArithmeticCalculatorImpl.java package com.atguigu.spring.aop; import org.springframework.stereotype.Component; @Component("arithmeticCalculator") public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; return result; } } // \src\com\atguigu\spring\aop\LoggingAspect.java package com.atguigu.spring.aop; import java.util.Arrays; import org.aspectj.lang.JoinPoint; importorg.aspectj.lang.annotation.After; Import org.aspectj.lang.annotation.AfterReturning; Import org.aspectj.lang.annotation.AfterThrowing; Import org.aspectj.lang.annotation.Aspect; Import org.aspectj.lang. annotation.Before; Import org.aspectj.lang.annotation.Pointcut; Import org.springframework.core.annotation.Order; Import org.springframework.stereotype.Component; / ** * @Order annotation can be used to specify the priority section, values are higher priority * / the @Order ( 2 ) @Aspect @Component public class LoggingAspect { / ** * Definition of a method used to declare pointcut expression. Generally, the method does not require re-insertion of other codes. * Used to declare @Pointcut pointcut expression. * Other alerts the direct use of this name to refer to pointcut expression. * / @Pointcut ( "Execution (com.atguigu.spring.aop.ArithmeticCalculator public int. * (..))" ) public void declareJointPointExpression () {} / ** * in com.atguigu. each method spring.aop.ArithmeticCalculator each implementation of the interface class begin before a code * / @Before ( "declareJointPointExpression ()" ) public void beforeMethod (the JoinPoint Joinpoint) { String methodName = joinPoint.getSignature (). getName ( ); Object [] args =joinPoint.getArgs (); System.out.println ( "Method of The" + methodName + "Begins with" + Arrays.asList (args)); } / ** * code is performed after the method is performed regardless of whether the process occurs. abnormal * / @After ( "declareJointPointExpression ()" ) public void afterMethod (the JoinPoint Joinpoint) { String methodName = joinPoint.getSignature () getName ();. System.out.println ( "Method of The" + methodName + "ends" ) ; } / ** * in the process method executed by the normal end code * return notification can access the return value of! * / @AfterReturning (value = "declareJointPointExpression()", Returning = "Result" ) public void the afterReturning (the JoinPoint Joinpoint, Result Object) { String methodName = joinPoint.getSignature () getName ();. System.out.println ( "Method of The" + methodName + "ends with" + Result ); } / ** * code executed when abnormality occurs in the target method. * abnormal access to objects; and may occur in a particular exception specified in performing notification code * / the @AfterThrowing (value = "declareJointPointExpression ()" , the throwing = "E" ) public void afterThrowing (the JoinPoint Joinpoint,Exception e){ String methodName = JoinPoint.getSignature () getName ();. System.out.println ( "Method of The" + methodName + "Occurs EXCETION:" + E); } / ** . * ProceedingJoinPoint need to carry around the notification type parameter * around advice like the dynamic agent of the whole process: ProceedingJoinPoint types of parameters can decide whether the target method. *, and around advice must have a return value, the return value is the target method's return value * / / * @Around ( "execution (public int COM . .atguigu.spring.aop.ArithmeticCalculator * (..)) ") public Object aroundMethod (ProceedingJoinPoint PJD) { Object Result = null; String methodName = pjd.getSignature () getName ();. the try { // pre-notification System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs())); //执行目标方法 result = pjd.proceed(); //返回通知 System.out.println("The method " + methodName + " ends with " + result); } catch (Throwable e) { //异常通知 System.out.println("The method " + methodName + " occurs exception:" + e); throw new RuntimeException(e); } //后置通知 System.out.println("The method " + methodName + " ends"); return result; } */ } // \src\com\atguigu\spring\aop\Main.java package com.atguigu.spring.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator"); System.out.println(arithmeticCalculator.getClass().getName()); int result = arithmeticCalculator.add(1, 2); System.out.println("result:" + result); result = arithmeticCalculator.div(1000, 10); System.out.println("result:" + result); } } // \src\com\atguigu\spring\aop\VlidationAspect.java package com.atguigu.spring.aop; import java.util.Arrays; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Order(1) @Aspect @Component public class VlidationAspect { @Before("com.atguigu.spring.aop.LoggingAspect.declareJointPointExpression()") public void validateArgs(JoinPoint joinPoint){ System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs())); } } // \src\com\atguigu\spring\aop\xml\ArithmeticCalculator.java package com.atguigu.spring.aop.xml; public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); } // \src\com\atguigu\spring\aop\xml\ArithmeticCalculatorImpl.java package com.atguigu.spring.aop.xml; public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; return result; } } // \src\com\atguigu\spring\aop\xml\LoggingAspect.java package com.atguigu.spring.aop.xml; import java.util.Arrays; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; public class LoggingAspect { public void beforeMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); Object [] args = joinPoint.getArgs(); System.out.println("The method " + methodName + " begins with " + Arrays.asList(args)); } public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends"); } public void afterReturning(JoinPoint joinPoint, Object result){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends with " + result); } public void afterThrowing(JoinPoint joinPoint, Exception e){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " occurs excetion:" + e); } @Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))") public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null; String methodName = pjd.getSignature().getName(); try { //前置通知 System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs())); //执行目标方法 result = pjd.proceed(); //返回通知 System.out.println("The method " + methodName + " ends with " + result); } catch (Throwable e) { //异常通知 System.out.println("The method " + methodName + " occurs exception:" + e); throw new RuntimeException(e); } //后置通知 System.out.println("The method " + methodName + " ends"); return result; } } // \src\com\atguigu\spring\aop\xml\Main.java package com.atguigu.spring.aop.xml; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml"); ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator"); System.out.println(arithmeticCalculator.getClass().getName()); int result = arithmeticCalculator.add(1, 2); System.out.println("result:" + result); result = arithmeticCalculator.div(1000, 0); System.out.println("result:" + result); } } // \src\com\atguigu\spring\aop\xml\VlidationAspect.java package com.atguigu.spring.aop.xml; import java.util.Arrays; import org.aspectj.lang.JoinPoint; public class VlidationAspect { public void validateArgs(JoinPoint joinPoint){ System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs())); } }
Reproduced in: https: //www.cnblogs.com/c0liu/p/7469189.html