Pointcut expression Expressions parsing and matching multiple conditions in Spring AOP

Source: Internet
Author: User

Pointcut refers to those methods that need to be executed "AOP", which is described by "Pointcut Expression".

Pointcut can be defined in the following ways or by && | | and! The way to combine.

Args ()

@args ()

Execution ()

This ()

Target ()

@target ()

Within ()

@within ()

@annotation

Where execution is the most used, the format is:

Execution (Modifiers-pattern ret-type-pattern declaring-type-pattern? Name-pattern (Param-pattern) Throws-pattern?)

Returning type Pattern,name pattern, and parameters pattern is required.

Ret-type-pattern: can represent any return value, the class name of the full path, and so on.

Name-pattern: Specifies the method name, * represents so, set*, which represents all methods that begin with set.

Parameters Pattern: Specifies the method parameter (the declared type), (..) Represents all parameters, (*) represents a parameter, (*,string) represents the first parameter as any value, and the second is a String type.

To illustrate:

Execution of any public method:

Execution (Public * * (..))

Any execution of a method that begins with "set":

Execution (* set* (..))

Execution of any method of the Accountservice interface:

Execution (* com.xyz.service.accountservice.* (..))

The execution of any method defined in the service package:

Execution (* com.xyz.service.*.* (..))

The execution of any method that defines any class in the service package and all child packages:

Execution (* com.xyz.service). *.*(..))

The execution of any method that defines the JoinPointObjP2 class in the Pointcutexp package and all the child packages:

Execution (* com.test.spring.aop.pointcutexp). joinpointobjp2.* (..)) ")

> Closest to (..) For the method name, near. * (..)) is the class name or interface name, as in the example above joinpointobjp2.* (..))

<aop:config>

<aop:pointcut expression= "Execution (* Com.travelsky.ccboy.dao. *.find* (..)) ||

Execution (* Com.travelsky.ccboy.dao). *.query* (..)) "

Id= "Findcachepointcut"/>

<aop:advisor advice-ref= "Jdbcinterceptor" pointcut-ref= "Findcachepointcut"/>

</aop:config>

Use between multiple expressions | | , or represents or, uses &&, and represents with,! Said

Non -

.

The above code can also be changed to write

<aop:config>

<aop:pointcut expression= "(Execution (* Com.travelsky.ccboy.dao).  *.find* (..))) or (Execution (* Com.travelsky.ccboy.dao). *.query* (..))

)

"

Id= "Findcachepointcut"/>

<aop:advisor advice-ref= "Jdbcinterceptor" pointcut-ref= "Findcachepointcut"/>

</aop:config>

Note that the differences between the two methods above will be | | Changed to or, and each execution is included, it is recommended that in order to differentiate between different expressions it is best to use () packaging.

Any class in the Pointcutexp package.

Within (com.test.spring.aop.pointcutexp.*)

Any class in the Pointcutexp package and all child packages.

Within (com.test.spring.aop.pointcutexp. *)

All classes that implement the Intf interface, if INTF is not an interface, are qualified intf a single class.

This (com.test.spring.aop.pointcutexp.Intf)

> When a class that implements an interface is AOP, the Getbean method must be cast as the interface type, not the type of the class.

Any method of all classes with @transactional callouts.

@within (org.springframework.transaction.annotation.Transactional)

@target (org.springframework.transaction.annotation.Transactional)

Any method with a @transactional callout.

@annotation (org.springframework.transaction.annotation.Transactional)

> @within and @target Annotations for classes, @annotation are annotations to methods

The parameter has a method with the @transactional callout.

@args (org.springframework.transaction.annotation.Transactional)

The parameter is a method of type string (run is a decision).

Args (String)

The Pointcut can be configured in Java annotations and XML two ways, as follows:

<BeanId="Bravemandao"class="Com.test.dao.BraveManDao" >  <PropertyName="Barveman"ref="Braveman" ></Property> </Bean> <BeanId="Braveman"class="Com.test.bean.BraveMan" >   </Bean> <BeanId="Minstrel"class="Com.test.bean.Minstrel" ></Bean> <Aop:config>  <Aop:aspectref="Minstrel" >   <Aop:pointcutexpression= "Execution (* *.test (..))" id= "say"/>   <aop:before pointcut-ref=  "say" method= "Singbeforesay"/>    <AOP: After pointcut-ref= "say" method=  "Singaftersay"/>  </aop:aspect> </aop:config>  
package com.test.bean; Public class minstrel { public void singbeforesay () {  System.out.println ( "before say!!!"); } public void singaftersay ( {  System.out.println ( "after say!!!"); }              /span>      
Bravemandao {  braveman Braveman;  Public Braveman Getbarveman () {return braveman; } void Setbarveman (Braveman braveman) {This.braveman = Braveman; } void Test () { braveman.say (); }}         

Pointcut expression Expressions parsing and matching multiple conditions in Spring AOP

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.