测试代码目录结构:
1、定义一个接口,代码:
package com.ehomy.spring.aop;
/**
* @author zxy
* @date 2018年9月5日 上午11:14:31
* 类说明
*/
public interface ArithmeticImpl {
int add(int a,int b);
int sub(int a,int b);
int mul(int a,int b);
int div(int a,int b);
}
2、接口实现类:
package com.ehomy.spring.aop;
import org.springframework.stereotype.Component;
/**
* @author zxy
* @date 2018年9月5日 上午11:16:12
* 类说明
*/
@Component
public class Arithmetic implements ArithmeticImpl {
@Override
public int add(int a, int b) {
return a+b;
}
@Override
public int sub(int a, int b) {
return a-b;
}
@Override
public int mul(int a, int b) {
return a*b;
}
@Override
public int div(int a, int b) {
return a/b;
}
}
3、环绕通知类:
package com.ehomy.spring.aop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* @author zxy
* @date 2018年9月5日 上午11:31:50
* 类说明
*/
@Order(2)
@Aspect
@Component
public class ArithmeticAspect {
/**
* 前置通知
* @param joinPoint
*/
@Before("execution(public int com.ehomy.spring.aop.ArithmeticImpl.* (int,int) )")
public void beforeMethod(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();//方法的属性
System.out.println("before method name "+joinPoint.getSignature().getName()+" params "+Arrays.asList(args));
}
/**后置通知
* @param joinPoint
*/
@After("execution(public int com.ehomy.spring.aop.ArithmeticImpl.* (int,int) )")
public void afterMethod(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();//方法的属性
System.out.println("after method name "+joinPoint.getSignature().getName()+" params "+Arrays.asList(args));
}
/**环绕通知
* @param joinPoint
* @throws Throwable
*/
@Around("execution(public int com.ehomy.spring.aop.ArithmeticImpl.* (int,int) )")
public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
String methodName = pjp.getSignature().getName();
System.out.println("方法前置通知,方法名:"+methodName+",参数:"+Arrays.asList(pjp.getArgs()));
Object proceed = pjp.proceed();
System.out.println("方法后置通知,方法名:"+methodName+",参数:"+Arrays.asList(pjp.getArgs()));
return proceed;
}
}
application.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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 自动扫描的包 -->
<context:component-scan base-package="com.ehomy.spring.aop"></context:component-scan>
<!-- 使代理通知起作用 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
4、主方法测试:
package com.ehomy.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author zxy
* @date 2018年9月5日 上午11:23:09
* 类说明
*/
public class Main {
public static void main(String[] args) {
ApplicationContext atc=new ClassPathXmlApplicationContext("application.xml");
ArithmeticImpl arithmetic=(ArithmeticImpl)atc.getBean("arithmetic");
System.out.println(arithmetic.add(1, 2));
}
}
结束语:
1、其中@Order注解为切面优先级的注解,,值越小优先级越高。
2、在第3步的代码中可以将每个方法上面的方法用切入点表达式替换,其他方法只需要引用此表达式即可。
代码:
/**
* 定义一个方法, 用于声明切入点表达式. 一般地, 该方法中再不需要添入其他的代码.
* 使用 @Pointcut 来声明切入点表达式.
* 后面的其他通知直接使用方法名来引用当前的切入点表达式.
*/
@Pointcut("execution(execution(public int com.ehomy.spring.aop.ArithmeticImpl.*.(..))")
public void declareJointPointExpression(){}
/**
* 前置通知
* @param joinPoint
*/
@Before("declareJointPointExpression")
public void beforeMethod(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();//方法的属性
System.out.println("before method name "+joinPoint.getSignature().getName()+" params "+Arrays.asList(args));
}