Common notation for spring pointcut expressions

Source: Internet
Author: User

Common notation for spring pointcut expressions

Since the use of ASPECTJ-style facets configuration has made spring's facets configuration much easier, but ASPECTJ is another open source project, the syntax of its regular expressions is slightly more bizarre.

Here are some common examples of how to write:

For example, here is a slice configuration of all the methods on the service package
<aop:config>
<aop:pointcut id= "Serviceoperation"
expression= "Execution (* *). service*. *(..))" />
<aop:advisor pointcut-ref= "Serviceoperation"
advice-ref= "Txadvice"/>
</aop:config>

The position where the expression is located, such as the pointcut position.

Configure this to better control the transactions on the facets, here is a simple example of a thing configuration:
<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
<tx:attributes>
<tx:method name= "delete*" rollback-for= "Exception"/>
<tx:method name= "save*" rollback-for= "Exception"/>
<tx:method name= "update*" rollback-for= "Exception"/>
<tx:method name= "*" read-only= "true" rollback-for= "Exception"/>
</tx:attributes>
</tx:advice>

Through the configuration of facets and notifications, the methods that begin with all delete/save/update are added with a consistency transaction and a read-only transaction is added to the other methods.


This is not thin enough, if you want to write more detailed control, you need to study the syntax of ASPECTJ pointcut configuration, in fact, study these standards, rather than take a few examples to see, solve the actual problem on the line. Like writing regular expressions, the standard is obvious, but it's not easy to write well, so you can get started quickly and understand it from an example.

The following documents are from the Spring Chinese Development Guide 2.5 document, translated by the Azolla Open source organization:

Spring AOP users may often use the execution pointcut indicator. The execution expression is in the following format:
Execution (Modifiers-pattern? Ret-type-pattern Declaring-type-pattern? Name-pattern (Param-pattern)
Throws-pattern?)
All parts are optional except the return type pattern (Ret-type-pattern in the code snippet above), the name pattern, and the parameter pattern. The return type mode determines that the return type of the method must match one connection point in turn. The most frequent return type pattern you will use is *, which represents the matching of any return type. A fully qualified type name will only match the method that returns the given type. The name pattern matches the method name. You can use the * wildcard character as all or part of the naming pattern. The parameter pattern is slightly more complex: () matches a method that does not accept any arguments, and (..) Matches a method that accepts any number of parameters (0 or more). The pattern (*) matches a method that accepts a parameter of any type. Mode (*,string) matches a method that accepts two parameters, the first of which can be any type, and the second must be of type String. For more information, see the Language Semantics section of the ASPECTJ Programming Guide.
Some examples of common pointcut expressions are given below.
Execution of any public method:
Execution (Public * * (.. ))
The execution of a method whose name begins with "set":
Execution (* set* (.. ))
Accountservice the execution of any method defined by the interface:
Execution (* com.xyz.service.accountservice.* (.. ))
Execution of any method defined in the service package:
Execution (* com.xyz.service.*.* (.. ))
Execution of any method defined in a service package or its child packages:
Execution (* com.xyz.service). *.*(.. ))
Any connection point in the service package (only method execution in spring AOP):
Within (com.xyz.service.*)
Any connection point in the service package or its child package (only method execution in spring AOP):
Within (Com.xyz.service. *)
Any connection point that implements the proxy object for the Accountservice interface (only method execution in spring AOP):
This (Com.xyz.service.AccountService)
' This ' is more commonly used in binding forms:-See the following notification section for information on how to make a proxy object available in the notification body.
Any connection point that implements the target object of the Accountservice interface (only method execution in spring AOP):
Target (Com.xyz.service.AccountService)
' Target ' is more commonly used in binding forms:-See the following notification section for information on how to make a target object available in the notification body.
Either one takes only one parameter, and the arguments passed in at run time are the connection points of the Serializable interface (only method execution in spring AOP)
Args (java.io.Serializable)
' args ' is more commonly used in binding forms:-See the following notification section for information on how to make method parameters available in the notification body.
Note that the pointcut given in the example is different from execution (* * (java.io.Serializable)): The args version only matches when the parameter passed in is Serializable when it is run dynamically. The execution version, in the method signature, declares that only one parameter of the serializable type matches.
Any connection point in the target object that has a @Transactional annotation (only method execution in spring AOP)
@target (org.springframework.transaction.annotation.Transactional)
' @target ' is more commonly used in binding forms:-See the following notification section for information on how to make a callout object available in the notification body.
The type of any target object declaration has a connection point that @Transactional annotations (only method execution in spring AOP):
@within (org.springframework.transaction.annotation.Transactional)
' @within ' is more commonly used in binding forms:-See the following notification section for information on how to make a callout object available in the notification body.
Any one executed method has a connection point that @Transactional annotations (only method execution in spring AOP)
@annotation (org.springframework.transaction.annotation.Transactional)
' @annotation ' is more commonly used in binding forms:-See the following notification section for information on how to make a callout object available in the notification body.
Either one accepts only one parameter, and the parameter type passed in by the runtime has a connection point for the @classified annotation (only method execution in spring AOP)
@args (com.xyz.security.Classified)
' @args ' is more commonly used in binding forms:-See the following notification section for information on how to make a callout object available in the notification body.
Any connection point above the spring bean named ' TradeService ' (only method execution in spring AOP):
Bean (TradeService)
Any connection point above the spring bean whose name matches the wildcard expression ' *service ' (only method execution in spring AOP):
Bean (*service)

Common notation for spring pointcut expressions

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.