AspectJ
AspectJ教程详情访问
https://www.yiibai.com/spring_aop/
bean配置
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<aop:aspectj-autoproxy/>
<bean id="arithmeticcalculator"class="com.imnu.dproxy.ArithmeticCalculatorImpl"></bean>
<bean id="loggingasceptj"class="com.imnu.dproxy.LoggingAsceptJ"></bean>
</beans>
切面类型
@Aspect
@Component
public class LoggingAsceptJ {
@Pointcut("execution(*com.imnu.dproxy.ArithmeticCalculatorImpl.*(..))")
privatevoid selectAll() {
}
@Before("selectAll()")
publicvoid beforeMethod(JoinPoint jpJoinPoint) {
Stringname = jpJoinPoint.getSignature().getName();
List<Object>arg = Arrays.asList(jpJoinPoint.getArgs());
System.out.println("这是前置通知Name" + name + "arg:" + arg);
}
@After("selectAll()")
publicvoid afterMethod(JoinPoint jpJoinPoint) {
Stringname = jpJoinPoint.getSignature().getName();
List<Object>arg = Arrays.asList(jpJoinPoint.getArgs());
System.out.println("这是后置通知Name" + name + "arg:" + arg);
}
@AfterReturning(pointcut= "selectAll()", returning = "retVal")
publicvoid afterReturning(JoinPoint jpJoinPoint, Object retVal) {
Stringname = jpJoinPoint.getSignature().getName();
List<Object>arg = Arrays.asList(jpJoinPoint.getArgs());
System.out.println("这是返回通知" + name + "arg:" + arg + "retVal:" +retVal);
}
@AfterThrowing(pointcut= "selectAll()", throwing = "error")
publicvoid afterThrowingAdvice(JoinPoint jp, Throwable error) {
System.out.println("[afterThrowingAdvice]Method Signature: " + jp.getSignature());
System.out.println("[afterThrowingAdvice]Exception: " + error);
}
/**
* 环绕通知需要携带ProceedingJoinPoint类型的参数
* 环绕通知类似于动态代理的全过程proceedingJoinPoint类型参数可以决定是否执行目标方法
* 且环绕通知必须有返回值,返回值为目标方法的返回值
* @param proceedingJoinPoint
*/
@Around(value= "selectAll()")
publicObject anroudMethod(ProceedingJoinPoint proceedingJoinPoint) {
Objectresult = null;
StringmethodName = proceedingJoinPoint.getSignature().getName();
try{
System.out.println("前置通知");
result= proceedingJoinPoint.proceed();
System.out.println("后置通知");
}catch (Throwable e) {
System.out.println("异常通知");
e.printStackTrace();
}
returnresult;
}
}
Pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
测试代码
ApplicationContext context = new ClassPathXmlApplicationContext("com/imnu/dproxy/bean.xml");
ArithmeticCalculator ari =(ArithmeticCalculator) context.getBean("arithmeticcalculator");
int resout = ari.add(1, 2);
System.out.println(resout);