Common Spring entry point expressions

Source: Internet
Author: User

Since the use of AspectJ style aspect configuration, the Spring aspect configuration is greatly simplified, but AspectJ is another open-source project, and its rule expression syntax is slightly odd.

Below are some common examples:

For example, the following is a configuration of all 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 expression is located at the position of the above pointcut.

To better control the transaction on the aspect, the following is a simple example of transaction 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 cut and notification, consistency transactions are added for all methods starting with delete/save/update, and read-only transactions are added for other methods.


This is not very detailed enough. If you want to write more detailed control, you need to study the AspectJ cut point configuration syntax. In fact, to study these standards, it is better to take a few examples, solve the problem. Just like writing regular expressions, the standard is clear, but it is not easy to write a regular expression. You can get started and understand it quickly from the example.

The following documents are from Spring Chinese Development Guide 2.5 and are translated by manjianghong Open Source:

Spring AOP users may frequently use execution Entry Point Indicators. The execution expression format is as follows:
Execution (modifiers-pattern? Ret-type-pattern declaring-type-pattern? Name-pattern (param-pattern)
Throws-pattern ?)
Except for the return type mode (ret-type-pattern in the code snippet above), the name mode and parameter mode are all optional. The return type mode determines that the return type of the method must match a connection point in sequence. The most frequent return type pattern you will use is *, which indicates matching 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 as the all or part of the naming mode. The parameter mode is a little complicated: () matches a method that does not accept any parameters, and (...) matches a method that accepts any number of parameters (zero or more ). The mode (*) matches a method that accepts any type of parameters. The mode (*, String) matches a method that accepts two parameters. The first method can be of any type, and the second method must be of the String type. For more information, see the language semantics section in the AspectJ programming guide.
Below are some examples of General pointcut expressions.
Execution of any public method:
Execution (public **(..))
Execution of any method whose name starts with "set:
Execution (* set *(..))
Execute any method defined by the AccountService 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 the service package or its sub-package:
Execution (* com. xyz. service ..*.*(..))
Any connection points in the service package (only method execution in Spring AOP ):
Within (com. xyz. service .*)
Any connection points in the service package or its sub-packages (only method execution in Spring AOP ):
Within (com. xyz. service ..*)
Any connection point of the proxy object that implements the AccountService interface (in Spring AOP, only method execution is performed ):
This (com. xyz. service. AccountService)
This is more common in binding forms:-see the notification section to learn how to make proxy objects available in the notification body.
Any connection point of the target object implementing the AccountService interface (in Spring AOP, only the method is executed ):
Target (com. xyz. service. AccountService)
'Target' is more commonly used in binding forms:-see the notification section to learn how to make the target object available in the notification body.
Each parameter accepts only one parameter, and the input parameter during runtime is the connection point of the Serializable interface (in Spring AOP, only method execution is required)
Args (java. io. Serializable)
'Args 'is more commonly used in binding forms:-see the notification section to learn how to make method parameters available in the notification body.
Note that the starting point in the example is different from execution (** (java. io. serializable): The args version matches only when the input parameter is Serializable during dynamic running, while the execution version matches when declaring only one Serializable parameter in the method signature.
The target object has an arbitrary connection point with the @ Transactional annotation (in Spring AOP, only method execution is performed)
@ Target (org. springframework. transaction. annotation. Transactional)
'@ Target' is more common in binding forms:-see the notification section to learn how to make annotation objects available in the notification body.
The Declaration type of any target object has a connection point with @ Transactional annotation (in Spring AOP, only method execution is performed ):
@ Within (org. springframework. transaction. annotation. Transactional)
'@ Within' is more common in binding forms:-see the notification section to learn how to make annotation objects available in the notification body.
Any method to be executed has a connection point with @ Transactional annotation (in Spring AOP, only method execution is performed)
@ Annotation (org. springframework. transaction. annotation. Transactional)
'@ Annotation' is more common in binding forms:-see the notification section to learn how to make annotation objects available in the notification body.
Each parameter accepts only one parameter, and the input parameter type at runtime has a connection point with the @ Classified annotation (in Spring AOP, only method execution is performed)
@ Args (com. xyz. security. Classified)
'@ Args' is more common in binding forms:-see the notification section to learn how to make annotation objects available in the notification body.
Any connection point on a Spring bean named 'tradeservice '(only method execution in Spring AOP ):
Bean (tradeService)
Any connection point above Spring bean whose name matches the wildcard expression '* Service' (in Spring AOP, only method execution is performed ):
Bean (* Service)
 
Author: ERDP Technical Architecture"

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.