Calculator.java
package com.gong.spring.aop.impl; public interface Calculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); }
CalculatorImpl.java
package com.gong.spring.aop.impl2; public class CalculatorImpl implements Calculator{ @Override public int add(int i, int j) { // TODO Auto-generated method stub int result = i+j; return result; } @Override public int sub(int i, int j) { // TODO Auto-generated method stub int result = i - j; return result; } @Override public int mul(int i, int j) { // TODO Auto-generated method stub int result = i * j; return result; } @Override public int div(int i, int j) { // TODO Auto-generated method stub int result = i / j; return result; } }
LoggingAspect.java
package com.gong.spring.aop.impl2; import java.util.Arrays; import java.util.List; import javax.management.RuntimeErrorException; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; public class LoggingAspect { public void beforeMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println(methodName+" begin with "+args); } public void afterMethod(JoinPoint joinPoint) { //获取名字 String methodName = joinPoint.getSignature().getName(); //获取参数 List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println(methodName+" end with "+args); } public void afterReturning(JoinPoint joinPoint,Object result) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("在afterReturning得到返回值:"+ result); System.out.println(methodName+" end with "+args); } public void afterThrowing(JoinPoint joinPoint,Exception ex) { String methodName = joinPoint.getSignature().getName(); System.out.println(methodName+" occurs exception:"+ex); } public Object aroundMethod(ProceedingJoinPoint pjd) { Object result = null; String methodName = pjd.getSignature().getName(); //执行目标方法 try { //前置通知 System.out.println(methodName+" begin with "+Arrays.asList(pjd.getArgs())); //执行目标方法 result = pjd.proceed(); //后置通知 System.out.println("在aroundMethod中得到值:"+result); } catch (Throwable e) { // TODO Auto-generated catch block //异常通知 System.out.println("the method occurs exception:" + e); throw new RuntimeException(e); } //后置通知 System.out.println(methodName+" end with "+Arrays.asList(pjd.getArgs())); return result; } }
applicationContext2.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" xmlns:context="http://www.springframework.org/schema/context" 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 配置bean --> <bean id="calculator" class="com.gong.spring.aop.impl2.CalculatorImpl"></bean> <!-- 配置切面bean --> <bean id="loggingAspect" class="com.gong.spring.aop.impl2.LoggingAspect"></bean> <!-- 配置AOP --> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut expression="execution(* com.gong.spring.aop.impl2.Calculator.*(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:aspect> </aop:config> </beans>
需要注意的是:execution(* com.gong.spring.aop.impl2.Calculator.*(int, int))中额第一个*不要漏掉,否则会报错。
Main.java
package com.gong.spring.aop.impl2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext2.xml"); //从IOC容器中获取bean的实例 Calculator calculator = (Calculator) ctx.getBean(Calculator.class); int res = calculator.add(2, 1); System.out.println("在主函数中加法计算的结果="+res); res = calculator.div(2, 1); System.out.println("在主函数中除法计算的结果="+res); } }
输出: