//使用@Order 注解指定切面的优先级, 值越小优先级越高
@Order(1)
@Aspect
@Component
public class vliDataAspect {
/*
* 定义一个方法,用于声明切入点表达式,一般的,该方法不再需要填入其他代码
*/
@Pointcut("execution(public int spring.aop.impl.ArithmeticCalculator.*(..))")
public void loggingAspectJoinPointExpression() {}
/*
* 再需要使用该表达式的地方加入方法即可实现表达式的复用
* 在其他文件中使用该表达式需要类名.方法,不同包下还要加上包名
* value = "vliDataAspect.loggingAspectJoinPointExpression()
*/
@Before("loggingAspectJoinPointExpression()")
public void validataArgs(JoinPoint joinpoint) {
System.out.println("valiData: " + joinpoint);
}
}
AOP
(loggingAspect.java)·······················································(写入需要的方法即可,不需要注解)
package spring.aop.xml;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggingAspect {
public void beforeMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println("the method "+ methodName +" begin..." + args);
}
public void afterMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println("the method "+ methodName +" end with ..." + args);
}
public void afterReturning(JoinPoint joinpoint, Object result) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println("the method "+ methodName+ " run with" +args+ "return with" + result);
}
public void afterThrowing(JoinPoint joinpoint, Exception e) {
String methodName = joinpoint.getSignature().getName();
System.out.println("the method" + methodName+ "occurs exception: with" + e);
}
public Object aroundMethod(ProceedingJoinPoint pjd) throws Throwable {
Object result = null;
//执行目标方法
try {
//前置通知
System.out.println("the message in around method--before main method runing");
result = pjd.proceed();
//返回通知
System.out.println("the message in around method--after main method run");
}catch(Exception e){
//异常通知
System.out.println("the occurs exception in around method : " + e);
}
//后置通知。。。
return result;
}
}
(vliDataAspect.jave)···························································
package spring.aop.xml;
import org.aspectj.lang.JoinPoint;
public class vliDataAspect {
public void loggingAspectJoinPointExpression() {}
public void validataArgs(JoinPoint joinpoint) {
System.out.println("valiData: " + joinpoint);
}
}
applicationContext-xml.xml)····························································
<!-- 配置bean -->
<bean id = "arithmeticCalculatorimpl" class = "spring.aop.xml.ArithmeticCalculatorlmp"></bean>
<!-- 配置切面的bean -->
<bean id = "loggingAspect" class = "spring.aop.xml.LoggingAspect"></bean>
<bean id = "vliDataAspect" class = "spring.aop.xml.vliDataAspect"></bean>
<!-- 配置 AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(public int spring.aop.xml.ArithmeticCalculator.*(..))" id="pointcut"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="2">
<!-- 前置通知 -->
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<!-- 后置通知 -->
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
</aop:aspect>
<aop:aspect ref = "vliDataAspect" order = "1">
<aop:before method="validataArgs" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>