Spring AOP Annotation-based "0 Configuration" mode

Source: Internet
Author: User

Spring AOP is based on the annotated "0 configuration" approach:

In the beans.xml of spring

<!--specify automatic search for bean components, automatic search of slice class---<context:component-scan base-package= "Org.crazyit.app.service, Org.crazyit.app.aspect "> <context:include-filter type=" annotation "expression=" Org.aspectj.lang.annotation.Aspect "/> </context:component-scan>

In the main method of Beantest

 // 1. Create a spring IOC container New  Classpathxmlapplicationcontext ("Beans.xml");  ///2, get bean Hello hello = Ctx.getbean ("Hello", hello.) from the IOC container.   Class);   //3, method of calling Bean hello.foo (); Hello.adduser ("Monkey King", "7788");

Use the @aspect to declare the authaspect as a slice, and the slices to be placed in the container, with the @component

@Before ("Execution (public int org.crazyit.app.service.Hello.addUser (string)")
Tells the method in this section to execute before the start of which methods of the class (that is, the target method AddUser), adduser execution as a pointcut.
Make @before work: Call a target method, and if a target method is consistent with the method declared by before this annotation, the AOP framework automatically generates a proxy object for the class in which the target method resides, and then invokes the method specified by the @before annotation before calling the target method

Add in Beans.xml:
<!--start @aspectj Support--
<!--makes ASPECTJ work, automatically generates proxy objects for matching classes--
  <aop:aspectj-autoproxy/>
Enable spring support for @aspect facets configuration

ASPECTJ supports 5 types of notification annotations
The simplest way to access the target method is to define an enhanced processing method that defines the first parameter as the Jointpoint type

The Jointpoint parameter represents the connection point, which indicates the connection details and contains several common methods:
1, object[] Getargs (): Returns the parameter when the target method is executed
2, Signaure getsignature (): Returns information about the enhanced method
3. Object Gettarget () returns the target object that is being implanted for enhanced processing
4. Object Getthis (): Returns the proxy object generated by the AOP framework for the target object

pre-notification (@Before): Notification of execution before the target method executes
post notification (@After): After the target method executes, the notification is executed, and the post notification cannot access the execution result of the target method, regardless of whether the target method has an exception
return notification (@AfterReturning): is executed after the target method has ended normally and can access the return value of the method
                                      @AfterReturning ( Returning= "rvt", pointcut= "Execution (* org.crazyit.app.service.impl.*.* (..))")

                                    pointcut (or written as value) to specify the Pointcut Corresponding cut-in expression
                              &NBS P The parameter name specified by the     returning property must correspond to a formal parameter name in the enhanced processing, and when the target method execution returns as the corresponding parameter is passed in the enhanced processing method, returning also has a                                       Additional role: can be used to limit A fixed pointcut matches only the method that has the corresponding return value type
Exception notification (@AfterThrowing): code that executes when an exception occurs on the target method, can access the exception object, and can specify that the notification code is executed when a specific exception occurs
Surround Notification (@Round): Surround notifications need to carry parameters of type Proceedingjointpoint, similar to the whole process of dynamic agents, Proceedingjointpoint type parameters can decide whether to execute the target method, and the ring                                   &NBSP; around notification must have a return value, which is the return value of the target method

Spring AOP Annotation-based "0 Configuration" method

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.