实现方法省去.....
//把这个类声明为一个切面,需要吧该类放入IOC容器中,在声明一个切面
@Aspect
@Component
public class LogginAspect {
// 定义一个方法,用于声明切入点的表达式,一般在放中不需要添加其他的代码
@Pointcut("execution(* com.wzq.spring.aop.impl.*.*(..))")
public void declareJoinPointExpression() {
}
@Before("declareJoinPointExpression()")
public void beforeMethod(JoinPoint joinPoint) {
//获取ArithnmeticCalulator的method方法
String methodName = joinPoint.getSignature().getName();
//获取ArithnmeticCalulator的method值
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + "begins" + args);
}
// 后置通知:在目标方法执行后执行的通知,执行的通通知 //后置通知不会返回结果
@After("declareJoinPointExpression()")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + "ends");
}
// 异常通知
@AfterThrowing(value ="declareJoinPointExpression()", throwing = "ex")
public void afterThrowing(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + "execution" + ex);
}
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
ArithnmeticCalulator arithnmeticCalulator = ctx
.getBean(ArithnmeticCalulator.class);
int result = arithnmeticCalulator.sub(6, 6);
System.out.println("result:" + result);
int result1=arithnmeticCalulator.div(10, 0);
System.out.println(result1);
}
}