2019独角兽企业重金招聘Python工程师标准>>>
1.。ArithmeticCalculator.java
package com.huangliusong.spring.aop.xml;
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);
}
2。ArithmeticCalculatorImpl.java
package com.huangliusong.spring.aop.xml;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int result = i + j;
return result;
}
@Override
public int sub(int i, int j) {
int result = i - j;
return result;
}
@Override
public int mul(int i, int j) {
int result = i * j;
return result;
}
@Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
3。LoggingAspect.java
package com.huangliusong.spring.aop.xml;
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.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
public class LoggingAspect {
public void beforeMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("方法开始——————" + methodName + Arrays.asList(args));
}
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("方法结束——————" + methodName + Arrays.asList(args));
}
public void afterRunning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("方法结束——————" + methodName + Arrays.asList(args)
+ result);
}
public void afterThrowing(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out
.println("方法结束——————" + methodName + Arrays.asList(args) + ex);
}
public Object aroundMethod(ProceedingJoinPoint point) {
Object result = null;
String methodName = point.getSignature().getName();
// 执行目标方法
try {
// 前置通知
System.out.println("aroundMethod-" + methodName + "-begin with "
+ Arrays.asList(point.getArgs()));
result = point.proceed();
// 后置通知
System.out.println("aroundMethod-" + methodName + " -end with "
+ Arrays.asList(point.getArgs()));
} catch (Throwable e) {
// 异常通知
System.err.println(e);
e.printStackTrace();
}
return result;
}
}
4。TestAop.java
package com.huangliusong.spring.aop.xml;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
@Test
public void test1() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext_xml.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx
.getBean("arithmeticCalculator");
System.err.println(arithmeticCalculator.getClass().getName());
int result = arithmeticCalculator.add(11, 11);
System.out.println("结果" + result);
result = arithmeticCalculator.div(12, 10);
System.out.println("结果" + result);
}
}
5。VlitationAspect.java
package com.huangliusong.spring.aop.xml;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* 验证通知 可以指定切面的优先级
* 优先级使用Order(1)值越小优先级越高
* @author Administrator
*
*/
public class VlitationAspect {
public void validateArgs(JoinPoint joinPoint){
System.err.println("validateArgs"+Arrays.asList(joinPoint.getArgs()));
}
}
applicationContext_xml.xml
运行: