Getting Started with Spring (10) using ASPECTJ for AOP development

Source: Internet
Author: User

Adding class Libraries: Aspectjrt.jar and Aspectjweaver.jar

Add an AOP schema.

Define XML element:<aop:aspectj-autoproxy>

Write Java classes and use @aspect annotations as notifications

AspectJ supports 5 types of notification annotations:

@Before: Pre-notification, executing before method execution

@After: Post notification, executing after the method is executed

@AfterReturning: Returns a notification that executes after the method returns the result

@AfterThrowing: Exception notification, after the method throws an exception

@Around: Surround notifications, executing around methods

You can configure it as a normal bean element.

Two

Post notification: @After

@After ("Execution" (* *). welcomeservice.* (..)) ")

public void Applaud () {.}

The post notification is executed after the target method execution completes. A slice aspect contains many notices.

@After

A post notification indicates that the target method is finished, and the notification is woven, regardless of whether the exception is thrown or not.

@AfterReturning

After the method returns, the notification executes only after the target method returns, if the exception is not executed.

@AfterReturning (pointcut= "", returning= "res")

public void xxx (joinput jp,object res)

In afterreturning

The return value can be received in the notification. Res is the object used to receive the return value.

Three

Surround Notification: @Around

@Around ("Execution" (* *). welcomeservice.* (..)) ")

public void Around (Proceedingpointcut JP) {.}

Note: You can control whether the target method is invoked and return a completely different object, and use caution.

Specify the priority level:

@Aspect

@Order (0)

public class xxx{...}

Add a @order annotation to specify the priority of the slice to be added (in order, the smaller the value, the higher the priority)

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.