How spring's AOP is configured in XML

Source: Internet
Author: User

AOP is Aspect Oriental program for slicing

First, a chestnut:

 <Aop:config>        <Aop:pointcutID= "Loggercutpoint"expression= "Execution (* com.how2java.service.productservice.* (..))"/>                     <Aop:aspectID= "Logaspect"ref= "Loggeraspect">            <Aop:afterPointcut-ref= "Loggercutpoint"Method= "Log"/>        </Aop:aspect>  </Aop:config>     

What does this configuration process mean?

<aop:pointcut id= "Loggercutpoint" expression= "Execution (* com.how2java.service.productservice.* (..)) "/>

This sentence is a declaration point of entry, the ID of the pointcut called Loggercutpoint, used to mark this entry point,

This expression means that when a method call in expression is satisfied, a slice operation is performed, similar to a cut plane:


The first * represents the return of any type
Com.how2java.service.productservice.* represents any method of a class whose package name begins with Com.how2java.service.ProductService (the second * denotes any method, the wildcard must know)
(..) The parameter representing the method is any number and type

Simply put, As long as any function of the Productservice class in this package is called, no matter what your return value is, it will trigger the switch, I will execute the Com.how2java.service, that is, the auxiliary function, but what is the auxiliary function, is the following two sentences:

<aop:aspect id= "Logaspect" ref= "Loggeraspect" >

<aop:after pointcut-ref= "loggercutpoint" method= "Log"/>

</aop:aspect>

These two sentences are defined by a section, which said that as long as the trigger switch, will be to execute the cut, that is, the section here, the so-called slice, is a class method just, make this tall on ...

The ID represents the name of the facet, and ref is the class in which the method is located, which means the name of the method,

pointcut-ref= "Loggercutpoint" This is to show me that this aspect is associated with the tangent point above (a tangent point can be associated with multiple facets, a slice can be associated with only one method), as long as the above tangent is triggered, I will be here to perform some auxiliary functions, The same as the single-chip interrupt,

After means to execute my interrupt after the pointcut is triggered, of course there are before, a total of five before,after,after-returning, After-throwing,around.

After the method parameter, you can also add a parameter list.

Body Start

The function of the website is divided into core function and auxiliary function, and auxiliary function is called slice

The process of AOP is divided into two steps: 1, inserting pointcuts in business classes, 2, associating pointcuts with tangent classes.

The business class is the core class, is the main function of the website, the aspect is the auxiliary function, the log, the statistic and so on

Through the configuration, can be implemented, in a method call, trigger other methods to execute, as if in the monitoring target method, you are executed, trigger me to execute.

AOP terminology

1, Notice:

The notification defines what the slice is going to do and when to do it, when to do the accessibility, what is the code for the function?

Five Types of

    1. before--call Notification before a method call

    2. after--call Notification when the method is complete, regardless of whether the method executes successfully or not

    3. after-returning--call notification after successful method execution

    4. after-throwing--notification after a method throws an exception

    5. The around--notification wraps the method that is notified, executes the custom behavior before the method call is notified and after the call

First four well understood, the last one

Around means that facets are executed before and after the monitored function is run.

The following is the function to be performed by the Log,log function has a formal parameter joinpoint this can be understood as a breakpoint, the middle sentence represents the program is being monitored to run, in the monitored program run, you can replace his formal parameters, this is the around of the powerful place, if the program is monitored, When you run the input is a haha string as an argument, but after the log method, this parameter is replaced by ABC.

 Public throws throwable {        System.out.println ("I am in the process of being watched ... ");         = Joinpoint.proceed (new object[]{"abc"});        System.out.println ("I'm behind the spy program ... " );         return object;    }

2, Connection point:

The point at which the slice can be inserted when performing normal functions. Points that can be used as pointcuts. Alternate points.

The connection point can be when the method is called, when an exception is thrown, or even when the field is modified, at which point the slice can be executed.

3, Facets:

Definition: A slice is a collection of notifications and pointcuts that together define the full functionality of a slice-what it is, and when and where it accomplishes its function.

To declare a slice:

In spring, a tangent is an object that contains a notification and a pointcut, a Bean,bean field and method that is the state and behavior of that facet, and also through configuration to specify pointcuts and notification implementations

In XML, facets are specified using the <aop:aspect> tag, and the ref attribute is used to refer to the slice support bean. This bean is used to perform the auxiliary functions to be done.

4, Tangent:

Definition: If the notification defines "what" and "when". Then the pointcut defines "where". The tangent point matches one or more connection points that the notification is to weave into. These pointcuts are typically specified using explicit classes or methods.

Role: Defines where the notification is applied (at which connection points)

Declaration of Pointcut:

The pointcut is also a bean in spring.

The declaration of a pointcut is defined in three ways:

1 ) in <aop:config> use <aop:pointcut> under tags declares a pointcut bean, which can be used by multiple slices, preferably in a pointcut that needs to be shared, which uses the id attribute to specify the bean name and, when notified, uses the Pointcut-ref property to refer to the pointcut using the ID. The expression property specifies pointcut expressions

<Aop:config>       <Aop:pointcutID= "Pointcut"expression= "Execution (* cn.javass). *.*(..))"/>       <Aop:aspectref= "Aspectsupportbean">          <Aop:beforePointcut-ref= "Pointcut"Method= "Before"/>  </Aop:aspect>  </Aop:config>  

2 ) in <aop:aspect> use <aop:pointcut> under tags declares a pointcut bean, which can be used by multiple slices, but generally the pointcut is used only by that slice, and of course it can be used by other facets, but it is best not to use that, which specifies the bean name using the id attribute. Use the Pointcut-ref property to refer to the pointcut with this ID at the time of the notification definition, and the expression property specifies Pointcut expressions:

<Aop:config>   <Aop:aspectref= "Aspectsupportbean">      <Aop:pointcutID= "Pointcut"expression= "Execution (* cn.javass). *.*(..))"/>      <Aop:beforePointcut-ref= "Pointcut"Method= "Before"/>   </Aop:aspect>  </Aop:config>  

3 ) anonymous pointcut Bean , You can specify a pointcut expression through the Pointcut property when declaring a notification, which is an anonymous pointcut that is used only by that notification:

 <  aop:config  >  <  aop:aspect  ref  = "Aspectsupportbean"  >  Span style= "color: #0000ff;" ><  aop:after  pointcut  = "Execution (* cn.javass). *.*(..))"  method  = "Afterfinallyadvice"    />  </ aop:aspect   >  </ aop:config  >  

5, introduced :

Introduction allows us to add a method or property to an existing class

6, woven into:

Weaving is a proxy object procedure that is applied to a target object to create a slice.

Facets are woven into the target object at the specified connection point, and there are multiple points in the life cycle of the target object that can be woven into

    1. Compile-time-facets are woven into the target class during compilation, which requires a special compiler. AspectJ's weaving compiler is the way in which the facets are woven in this manner.
    2. Class load period--facets are loaded into the class
    3. The JVM, which requires a special class loader, can enhance the byte code of the target class before the target class is introduced into the application. AspectJ5 's LTW support this weaving method
    4. Run time-the slice is woven at some point during the application run. In general, the AOP container creates proxy objects dynamically for the target object when it is woven into the plane. Spring AOP is the way in which the facets are woven in this manner.

How spring's AOP is configured in XML

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.