引入的jar包与基于注解的方式引入的jar包相同
ArithmeticCalculator接口
package com.spring.aop.impl.xml;
public interface ArithmeticCalculator {
public int add(int i, int j);
public int sub(int i, int j);
public int mul(int i, int j);
public int div(int i, int j);
}
接口实现类 ArithmeticCalculatorImpl.java
package com.spring.aop.impl.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;
}
}
切面类 LoggingAspect.java
package com.spring.aop.impl.xml;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggingAspect {
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 ");
}
public void afterReturnning(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 exception " + e);
}
public Object aroundMethod(ProceedingJoinPoint point) {
Object result = null;
String methodName = point.getSignature().getName();
try {
//前置通知
System.out.println("The method "+ methodName +" begins with " + Arrays.asList(point.getArgs()));
//执行目标方法
result = point.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;
}
}
切面类 ValidationAspect.java
package com.spring.aop.impl.xml;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
public class ValidationAspect {
public void validateArgs(JoinPoint joinPoint) {
System.out.println("validate:" + Arrays.asList(joinPoint.getArgs()));
}
}
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.1.xsd"> <!-- 配置bean --> <bean id="arithmeticCalculator" class="com.spring.aop.impl.xml.ArithmeticCalculatorImpl"></bean> <!-- 配置切面的bean --> <bean id="loggingAspect" class="com.spring.aop.impl.xml.LoggingAspect"></bean> <bean id="validationAspect" class="com.spring.aop.impl.xml.ValidationAspect"></bean> <!-- 配置AOP --> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut expression="execution(* com.spring.aop.impl.xml.ArithmeticCalculator.*(..))" id="pointcut"/> <!-- 配置切面及通知,使用order指定优先级 --> <aop:aspect ref="loggingAspect" order="1"> <!-- 环绕通知 --> <!-- <aop:around method="aroundMethod" pointcut-ref="pointcut"/> --> <!-- 前置通知 --> <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="afterReturnning" pointcut-ref="pointcut" returning="result"/> </aop:aspect> <aop:aspect ref="validationAspect" order="2"> <!-- 前置通知 --> <aop:before method="validateArgs" pointcut-ref="pointcut"/> </aop:aspect> </aop:config> </beans>
Main.java
package com.spring.aop.impl.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//创建spring IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
//从IOC容器中获取bean实例
ArithmeticCalculator arithmeticCalculator = applicationContext.getBean(ArithmeticCalculator.class);
int result = arithmeticCalculator.add(4, 6);
System.out.println(result);
result = arithmeticCalculator.sub(4, 6);
System.out.println(result);
System.out.println(result);
result = arithmeticCalculator.mul(4, 6);
System.out.println(result);
System.out.println(result);
result = arithmeticCalculator.div(4, 0);
System.out.println(result);
}
}
源码 http://yunpan.cn/cgsrQHmUvrIQt 提取码 6564
本文出自 “优赛工作室” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1557743