建立相应的接口
ArithmeticCalculator.java
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);
}
接口的实现类
ArithmeticCalculatorImpl.java
@Component("ArithmeticCalculatorImpl")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
public int add(int i, int j) {
return i+j;
}
public int sub(int i, int j) {
return i-j;
}
public int mul(int i, int j) {
return i*j;
}
public int div(int i, int j) {
return i/j;
}
}
两个切面
LoggingAspect.java
public class LoggingAspect {
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The LoggingAspect "+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 afterReturning(JoinPoint joinPoint,Object r){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" "+args+" result is " + r);
}
public void afterThrowing(JoinPoint joinPoint,Exception exception){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" exception is "+exception);
}
public Object around(ProceedingJoinPoint proceedingJoinPoint){
System.out.println("Around");
Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName();
List<Object> args = Arrays.asList(proceedingJoinPoint.getArgs());
try {
// 前置通知
System.out.println(" --> Around The method "+methodName+" begins with "+args);
result = proceedingJoinPoint.proceed();
// 返回通知
System.out.println(" --> Around The method "+methodName+" "+args+" result is " + result);
} catch (Throwable throwable) {
// 异常通知
System.out.println(" --> Around The method "+methodName+" exception is "+throwable);
}
// 后置通知
System.out.println(" --> Around The method "+methodName+" ends");
return result;
}
}
ViberAspect.java
public class ViberAspect {
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("==》 The ViberAspect "+methodName+" begins with "+args);
}
}
配置文件
<?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.xsd">
<!-- 配置bean-->
<bean id="ArithmeticCalculatorImpl"
class="spring.aop.xml.ArithmeticCalculatorImpl">
</bean>
<!-- 配置切面bean-->
<bean id="loggingAspect"
class="spring.aop.xml.LoggingAspect"></bean>
<bean id="viberAspect"
class="spring.aop.xml.ViberAspect"></bean>
<!-- 配置AOP-->
<aop:config>
<!-- 配置切点表达式-->
<aop:pointcut id="pointcut"
expression="execution(* spring.aop.xml.ArithmeticCalculatorImpl.*(..))"/>
<!-- 配置切面、通知、优先级-->
<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-returning method="afterReturning" pointcut-ref="pointcut" returning="r"></aop:after-returning>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="exception"></aop:after-throwing>
<aop:around method="around" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
<aop:aspect ref="viberAspect" order="1">
<aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
</aop:aspect>
</aop:config>
</beans>
主函数
public static void main(String[] args) {
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("aop-xml.xml");
ArithmeticCalculator arithmeticCalculator =
(ArithmeticCalculator) applicationContext.getBean("ArithmeticCalculatorImpl");
System.out.println("result: "+arithmeticCalculator.add(1,7));
}
运行结果