直接看代码:
package com.cn.spring.aop.impl; //加减乘除的接口类 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); }
package com.cn.spring.aop.impl; //实现类 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; } }
package com.cn.spring.aop.impl; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import java.util.Arrays; import java.util.List; public class LoggingAspect { public void declareJoinPointExpression() {} public void beforeMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " begins with " + args); } public void afterMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " ends with " + args); } public void afterReturning(JoinPoint joinPoint, Object result) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method ends witd " + result); } public void afterThrowing(JoinPoint joinPoint, Exception ex) { String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " occures exception with: " + ex); } public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) { Object result = null; String methodName = proceedingJoinPoint.getSignature().getName(); //执行目标方法 try { //前置通知 System.out.println("The method " + methodName + " begins with " + Arrays.asList(proceedingJoinPoint.getArgs())); result = proceedingJoinPoint.proceed(); //返回通知 System.out.println("The method ends with " + result); } catch (Throwable throwable) { //异常通知 System.out.println("The method " + methodName + " occures exception with: " + throwable); throw new RuntimeException(throwable); } //后置通知 System.out.println("The method " + methodName + " ends"); return result; } }
package com.cn.spring.aop.impl; import org.aspectj.lang.JoinPoint; import java.util.Arrays; public class ValidationAspect { public void validateArgs(JoinPoint joinPoint) { System.out.println("validate:" + Arrays.asList(joinPoint.getArgs())); } }
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.cn.spring.aop.impl"> </context:component-scan> <!--配置bean--> <bean class="com.cn.spring.aop.impl.ArithmeticCalculatorImpl"></bean> <!--配置切面的bean--> <bean id="loggingAspect" class="com.cn.spring.aop.impl.LoggingAspect"></bean> <bean id="validationAspect" class="com.cn.spring.aop.impl.ValidationAspect"></bean> <!--配置AOP--> <aop:config> <!--配置切点表达式--> <aop:pointcut id="pointcut" expression="execution(* com.cn.spring.aop.impl.ArithmeticCalculator.*(..))"></aop:pointcut> <!--配置切面及通知--> <aop:aspect ref="loggingAspect" order="2"> <aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before> <aop:after method="afterMethod" pointcut-ref="pointcut"></aop:after> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"></aop:after-throwing> <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"></aop:after-returning> <!--环绕通知--> <aop:around method="aroundMethod" pointcut-ref="pointcut"></aop:around> </aop:aspect> <aop:aspect ref="validationAspect" order="1"> <aop:before pointcut-ref="pointcut" method="validateArgs"></aop:before> </aop:aspect> </aop:config> <!--使AspjectJ注解起作用:自动为匹配的类生成代理对象--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
package com.cn.spring.aop.impl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//1.创建Spring的IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("22-1.xml");
//2.从IOC容器中huo获取bean的实例
ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
//3.使用bean
int result = arithmeticCalculator.add(3, 6);
System.out.println("result:" + result);
//result = arithmeticCalculator.div(3, 0);
// System.out.println("result:" + result);
}
}