I. knowledge points
When multiple aspect instances are applied at the same connection point, the priority of the aspect instances is ambiguous unless their priority is explicitly specified.
The aspect priority can be implemented through the ordered interface or the @ order annotation.
Ii. Sample Code
(1) Implement the ordered Interface
package com.codeproject.jackie.springrecipesnote.springaop;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.Ordered;/** * @author jackie * */@Aspectpublic class CalculatorValidationAspect implements Ordered{private Log log = LogFactory.getLog(this.getClass());@Before("execution(* *.*(double, double))")public void validateBefore(JoinPoint joinPoint) {for (Object arg : joinPoint.getArgs()) {log.info("validate begins...");validate((Double) arg);}}private void validate(Double arg) {if (arg < 0) {throw new IllegalArgumentException("Positive numbers only");}}@Overridepublic int getOrder() {return 0;}}
Package COM. codeproject. jackie. springrecipesnote. springaop; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import Org. aspectj. lang. annotation. aspect; import Org. aspectj. lang. annotation. before; import Org. springframework. core. ordered;/*** @ author Jackie **/@ aspectpublic class calculatorloggingaspect implements ordered {private log = logfactory. getlog (this. getcia SS ();/*** the entry point expression matches the execution of the add () method of the arithmeticcalculator interface. * The asterisks before the expression match any modifier (public, protected, and private) and any return type. * The two vertices in the parameter list match any number of parameters. */@ Before ("execution (* arithmeticcalculator. add (..)) ") Public void logbefore () {log.info (" The Method Add () begins ") ;}@ overridepublic int getorder () {return 1 ;}}
If you declare a bean instance of this aspect in the bean configuration file, you can register this aspect in Spring:
<bean class="com.codeproject.jackie.springrecipesnote.springaop.CalculatorLoggingAspect" /><bean class="com.codeproject.jackie.springrecipesnote.springaop.CalculatorValidationAspect" />
Note: the priority of aspect does not depend on the bean Declaration Order. The lower the value returned by the getorder () method, the higher the priority.
(2) @ order Annotation
Another method to specify the priority is through the @ order annotation. The sequence value appears in the annotation value.
package com.codeproject.jackie.springrecipesnote.springaop;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;/** * @author jackie * */@Aspect@Order(0)public class CalculatorValidationAspect {private Log log = LogFactory.getLog(this.getClass());@Before("execution(* *.*(double, double))")public void validateBefore(JoinPoint joinPoint) {for (Object arg : joinPoint.getArgs()) {log.info("validate begins...");validate((Double) arg);}}private void validate(Double arg) {if (arg < 0) {throw new IllegalArgumentException("Positive numbers only");}}}
Package COM. codeproject. jackie. springrecipesnote. springaop; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import Org. aspectj. lang. annotation. aspect; import Org. aspectj. lang. annotation. before; import Org. springframework. core. annotation. order;/*** @ author Jackie **/@ aspect @ Order (1) public class calculatorloggingaspect {private log = logfactory. getlog (this. getclas S ();/*** the entry point expression matches the execution of the add () method of the arithmeticcalculator interface. * The asterisks before the expression match any modifier (public, protected, and private) and any return type. * The two vertices in the parameter list match any number of parameters. */@ Before ("execution (* arithmeticcalculator. Add (...)") Public void logbefore () {log.info ("The Method Add () begins ");}}