Pointcut expression Expressions parsing and matching multiple conditions in Spring AOP

Source: Internet
Author: User

The transaction control-related configuration in spring:

<bean id= "Txmanager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource"/>
</bean>

<tx:advice id= "Txadvice" transaction-manager= "Txmanager" >
<tx:attributes>
<tx:method name= "insert*" rollback-for= "Exception"/>
<tx:method name= "update*" rollback-for= "Exception"/>
<tx:method name= "delete*" rollback-for= "Exception"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id= "dbserviceoperation" expression= "Execution (* com.htt). *service.* (..)) " />
<aop:advisor advice-ref= "Txadvice" pointcut-ref= "Dbserviceoperation"/>
</aop:config>

The rules for "expression" in the "aop:pointcut" tab are as follows:

Execution (Modifiers-pattern ret-type-pattern declaring-type-pattern? Name-pattern (Param-pattern) Throws-pattern?)
Ret-type-pattern,name-pattern (Param-pattern) is a must.
Ret-type-pattern: Identifies the return value of the method, requires the use of the full path of the class name, such as java.lang.String, or any return value for *;
Name-pattern: Specifies the method name, * represents all, such as set*, representing all methods that begin with set.
Param-pattern: Specifies the method parameter (declared type), (..) Represents all parameters, (*) represents a parameter, (*,string) represents the first parameter as any value, and the second is a String type.

An example of an expression is as follows:

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.* (..)) ")

  Use the | |,or representation between multiple expressions or, using the &&,and representation with,! Indicates non. For example:

<aop:config>
<aop:pointcut id= "Pointcut" expression= "(Execution (* Com.ccboy.dao). *.find* (..))) or (Execution (* Com.ccboy.dao). *.query* (..))) " />
<aop:advisor advice-ref= "Jdbcinterceptor" pointcut-ref= "Pointcut"/>
</aop:config>

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.