Spring Strategy Study Notes (3.04) -- specify the aspect priority

Source: Internet
Author: User

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 ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.