Spring's AOP configuration in XML

I.e. AOP Aspect Oriented Programming Aspect Oriental Program

First to a chestnut:

 <aop:config>
        <aop:pointcut id="loggerCutpoint"
            expression=
            "execution(* com.how2java.service.ProductService.*(..)) "/>
         
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:after pointcut-ref="loggerCutpoint" method="log"/>
        </aop:aspect>
  </aop:config> 

The configuration process is what is the meaning of it?

<aop:pointcut id="loggerCutpoint" expression="execution(* com.how2java.service.ProductService.*(..)) "/>

This sentence is a statement entry point, entry point id called loggerCutPoint, used to mark the starting point, this expression says: meet later in the method of expression called, to carry out the operation section, similar to that triggered the cut:
the first * representatives return any type
com.how2java.service.ProductService. * denotes any method name com.how2java.service.ProductService package at the beginning of class (second * means any method, wildcards certainly know it) (...) representation of parameters any number and type

It simply is, as long as any of a function com.how2java.service this package ProductService class is called, no matter what your return value that will trigger the switch, I would have to carry out section, which is the auxiliary function, but accessibility What is it, that is, the following two sentences:

<aop:aspect id="logAspect" ref="loggerAspect">
  <aop:after pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>

These two is the definition of a section, it says that as long as the trigger switch will be to carry out section, refers to the section where the so-called section, is a class method only, on do this tall. . .
id behalf of the section's name, ref is the class refers to methods where the name is the way of the method represented,
pointcut-ref = "loggerCutpoint" This is expressed (a cut I this section is above the cut-off point associating point can be associated with multiple facets of a cut can only be associated with one method), just above the cut-off point is triggered, I would have come here to perform some auxiliary functions, and interrupt made as microcontroller, after triggering represents the starting point later execution of the interrupt me, of course, there before, a total of five before, after, After-returning, After-throwing, Around.

Behind the method parameters, you can also add the parameter list.

Start of text
functionality of the site is divided into core and secondary functions, auxiliary functions called the cut
process AOP two steps:
1, insert an entry point in the business class, 2 to associate the entry point and cut class

Business class is the core class, is the main function of the site, accessibility is cut, logs, statistics and the like

By the configuration can be achieved, when a method call, triggering the implementation of other methods, like monitoring target method, you are executed, it triggered my execution.

AOP terminology

1, the notification:
notification section defines the work to be done and when the content of the job done, is what to do when an auxiliary function, function code is specifically what
the five types
Before-- call notification prior to the method call
After-- complete method after calling notice, regardless of the success of the implementation of the method
call notification after the successful implementation of the method after-returning--
notification method throws an exception after-throwing-- the after
Around-- notice wrapped method notified in the notice of before and after calling method calls execute custom behavior
before the four well-understood, the last
around a cut face execution will be monitored before and after the function is run,
the following is a function of log section to be executed, there is a log function of this parameter joinPoint can be understood as a breakpoint, the middle one is the representative of the monitored program is running, the monitored program is running, the parameter can replace him, this is a powerful place around, if the monitored program, run time input haha is a string as an argument, but after log method, the The number will be replaced with the abc

public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("我在被监视程序之前。。。");
        Object object = joinPoint.proceed(new Object[]{"abc"});
        System.out.println("我在被监视程序之后。。。" );
        return object;
    }

2, the connection point:
while performing a normal function, can be inserted into the cut point. It can be used as entry point for point. Candidate points.
Connection point may be a method call, an exception is thrown, even when modifying fields at these points, it is possible to perform the cut.

3, section:
Definition: section is to inform and cut-off point of collection, notification and cut points together define a section of the full functionality - what it is, when and where to perform its function.
Disclaimer section:
In the Spring, the section is a notification containing the object and tangent point, is a Bean, Bean fields and methods is the section of the state and behavior, but also through configuration to achieve the specified entry points and notification
in xml , cut using aop: aspect tag specifies, ref attribute is used to reference section to support Bean. This bean which is used to perform auxiliary functions to do.

4, the cut-off point:
Definition: If you notice defines the "what" and "when." Then the cut-off point to define the "where." It matches the tangent point to notice or weaving of a plurality of connection points. Usually explicit class or method to specify the tangent point.
Role: Location define notify applications (which connection point)
declare entry points:
the starting point is a Bean in Spring.
Statement tangent point is defined in three ways:
1) in aop: config using the tags aop: pointcut declare an entry point Bean, the starting point may be a plurality of facets used for the starting point need to share the use of the best mode to use, the entry point is specified using the id attribute Bean name, use pointcut-ref attribute is defined in the notice referenced entry point through the id, expression attribute specifies the pointcut expression

<aop:config>  
   <aop:pointcut id="pointcut" expression="execution(* cn.javass..*.*(..))"/>  
   <aop:aspect ref="aspectSupportBean">  
      <aop:before pointcut-ref="pointcut" method="before"/>  
   </aop:aspect>  
</aop:config>  

2) In aop: aspect using the tags aop: pointcut declare an entry point Bean, the starting point may be a plurality of facets, but generally are only the starting point of the section used, of course, also be used other section, but preferably Do not use as the entry point using the id attribute to specify the name of Bean, use pointcut-ref attribute is defined in the notice referenced entry point through the id, expression attribute specifies the pointcut expression:

<aop:config>  
 <aop:aspect ref="aspectSupportBean">  
    <aop:pointcut id=" pointcut" expression="execution(* cn.javass..*.*(..))"/>  
    <aop:before pointcut-ref="pointcut" method="before"/>  
 </aop:aspect>  
</aop:config>  

3) anonymous entry point Bean, by pointcut attribute specifies the entry point notification expression in the declaration, the entry point is anonymous entry point, only to be used in the notice:

<aop:config>  
 <aop:aspect ref="aspectSupportBean">  
     <aop:after pointcut="execution(* cn.javass..*.*(..))" method="afterFinallyAdvice"/>  
 </aop:aspect>  
</aop:config>  

5, the introduction of: introducing method allows us to add attributes to existing classes

6, weaving:
weaving section is applied to the target object to create a proxy object process. Section is woven into the target object at the specified point of connection, can be woven into a plurality of points in the life cycle of the target object
compile - section is woven in the target class compile time, this approach requires special compiler. The compiler AspectJ weaving is woven into cut in this manner.
Load Class - The class is loaded into the JVM in section, this approach requires special class loader before he can use the enhanced target class bytecode is introduced in the target class. AspectJ5 of LTW supports this way of weaving
run - the aspect is woven at some point during the application run. Under normal circumstances, when the weaving section, AOP container will target objects dynamically create proxy object. Spring AOP is in this way weaving section.

Published 444 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/zt2650693774/article/details/103785766