package bean;
import java.util.*;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.annotation.After;
@Aspect
public class MyLogger {
@Before("execution(* print(..))")
public void before(JoinPoint jp){
System.out.println("------前置增强-------");
System.out.println("正在使用"+jp.getThis().getClass().getName()+"对象上的"+jp.getClass().getName()+"方法");
System.out.println("传入参数"+Arrays.toString(jp.getArgs()));
}
@AfterReturning(pointcut="execution(* print(..))",returning="returnV")
public void after(JoinPoint jp ,Object returnV){
System.out.println("------后置增强-------");
System.out.println("以使用完"+jp.getThis().getClass().getName()+"对象上的"+jp.getClass().getName()+"方法");
System.out.println("传入参数"+Arrays.toString(jp.getArgs()));
System.out.println("返回值"+returnV);
}
@AfterThrowing(pointcut="execution(* print(..))",throwing="e")
public void Throwing(JoinPoint jp,RuntimeException e){
System.out.println("\n------异常抛出增强-------");
System.out.println("正在使用"+jp.getThis().getClass().getName()+"对象上的"+jp.getClass().getName()+"方法");
System.out.println("传入参数"+Arrays.toString(jp.getArgs()));
System.out.println("异常信息"+e.getMessage());
}
@Around("execution(* print(..))")
public Object around(ProceedingJoinPoint jp) throws Throwable{
System.out.println("\n------环绕增强-------");
System.out.println("正在使用"+jp.getThis().getClass().getName()+"对象上的"+jp.getClass().getName()+"方法");
System.out.println("传入参数"+Arrays.toString(jp.getArgs()));
try{
jp.proceed();
}catch (Exception e) {
System.out.println("异常信息"+e.getMessage());
}
System.out.println("----------------------");
return null;
}
@After("execution(* print(..))")
public void afterLo(JoinPoint jp){
System.out.println("------最终增强-------");
System.out.println("以使用完"+jp.getThis().getClass().getName()+"对象上的"+jp.getClass().getName()+"方法");
System.out.println("传入参数"+Arrays.toString(jp.getArgs()));
}
import java.util.*;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.annotation.After;
@Aspect
public class MyLogger {
@Before("execution(* print(..))")
public void before(JoinPoint jp){
System.out.println("------前置增强-------");
System.out.println("正在使用"+jp.getThis().getClass().getName()+"对象上的"+jp.getClass().getName()+"方法");
System.out.println("传入参数"+Arrays.toString(jp.getArgs()));
}
@AfterReturning(pointcut="execution(* print(..))",returning="returnV")
public void after(JoinPoint jp ,Object returnV){
System.out.println("------后置增强-------");
System.out.println("以使用完"+jp.getThis().getClass().getName()+"对象上的"+jp.getClass().getName()+"方法");
System.out.println("传入参数"+Arrays.toString(jp.getArgs()));
System.out.println("返回值"+returnV);
}
@AfterThrowing(pointcut="execution(* print(..))",throwing="e")
public void Throwing(JoinPoint jp,RuntimeException e){
System.out.println("\n------异常抛出增强-------");
System.out.println("正在使用"+jp.getThis().getClass().getName()+"对象上的"+jp.getClass().getName()+"方法");
System.out.println("传入参数"+Arrays.toString(jp.getArgs()));
System.out.println("异常信息"+e.getMessage());
}
@Around("execution(* print(..))")
public Object around(ProceedingJoinPoint jp) throws Throwable{
System.out.println("\n------环绕增强-------");
System.out.println("正在使用"+jp.getThis().getClass().getName()+"对象上的"+jp.getClass().getName()+"方法");
System.out.println("传入参数"+Arrays.toString(jp.getArgs()));
try{
jp.proceed();
}catch (Exception e) {
System.out.println("异常信息"+e.getMessage());
}
System.out.println("----------------------");
return null;
}
@After("execution(* print(..))")
public void afterLo(JoinPoint jp){
System.out.println("------最终增强-------");
System.out.println("以使用完"+jp.getThis().getClass().getName()+"对象上的"+jp.getClass().getName()+"方法");
System.out.println("传入参数"+Arrays.toString(jp.getArgs()));
}
}
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<aop:aspectj-autoproxy/>
</beans>