5.3 AOP's 3 Key Concepts & 5.4 Spring's 3 pointcuts (Pointcut) implementations

Source: Internet
Author: User
Tags aop

3 Key concepts of 5.3 AOP

Because the concept of AOP is difficult to understand, the first step is to explain the Java dynamic Proxy mechanism, so that readers can gradually understand the idea of AOP.

The key to learning AOP is understanding AOP and being able to use AOP. For many of the concepts of AOP, readers can only understand 3 important concepts. These 3 concepts are pointcut, advice and advisor. 5.3.1 pointcut (Pointcut)

Before introducing Pointcut, it is necessary to introduce the concept of Join Point (Junction) first. Join point refers to a stage in the program's operation, such as a method call, an exception thrown, and so on. The Doauditing () method in the previous example is a join point indicating that the program is to join advice in this place.

Pointcut is a collection of join points, which is a set of locations in the program where the advice needs to be injected, indicating what conditions the advice will be triggered.

The Org.springframework.aop.Pointcut interface is used to specify notifications to specific classes and methods. View the source file Pointcut.java in the Spring download package, the path is SPRING-FRAMEWORK-2.0-M1/SRC/ORG/SPRINGFRAMEWORK/AOP and you can see Pointcut.java. The source code is as follows:

pointcut.java**************

Package ORG.SPRINGFRAMEWORK.AOP;

Public interface Pointcut {

Used to qualify a pointcut in a given target class

Classfilter Getclassfilter ();

A method used to determine whether a pointcut matches a given target class

Methodmatcher Getmethodmatcher ();

Pointcut TRUE = truepointcut.instance;

}

Code Description:

Interface Classfilter, which is used to qualify the pointcut in the given target class.

Interface Methodmatcher to determine if the Pointcut matches the method given by the target class.

As can be seen from the above, in the interface Pointcut, mainly contains two interfaces: Classfilter and methodmatcher, facilitate the reuse of code. 5.3.2 Notice (Advice)

Advice is the processing logic used by a connection point, which is the code injected into the connection point. The code extracted from the previous example to output log information is a advice that indicates that you want to join this code at join point. 5.3.3 Advisor

The advisor is the Pointcut and advice Configurator, which includes pointcut and advice, and is the code that will advice inject pointcut into the program.

This is a cursory description of the 3 concepts of AOP, designed to allow readers to quickly get into AOP, and then to explain the 3 concepts in more detail.

5.4 Spring's 3 pointcuts (Pointcut) implementations

As mentioned in the previous section, Pointcut is a collection of join points, which is a set of locations in the program where you need to inject advice. Spring offers the implementation of 3 pointcuts (Pointcut): Static pointcuts, dynamic pointcuts, and custom pointcuts, which are explained separately below. 5.4.1 Static Pointcut

A static pointcut is limited to a given method and target class, regardless of the parameters of the method. When spring calls a static pointcut, it calculates the position of the static pointcut only the first time, and then caches it, and then does not need to compute. Static pointcuts can be implemented using Org.springframework.aop.support.RegexpMethodPointcut, Regexpmethodpointcut is a generic regular expression pointcut that is passed through Jakarta Oro to achieve, you need to add Jakarta-oro-2.0.8.jar to the classpath, its regular expression syntax and Jakarta oro Regular expression syntax is the same. An example code that uses Regexpmethodpointcut is as follows:

<bean id= "Settersandabsquatulatepointcut"

class= "Org.springframework.aop.support.RegexpMethodPointcut" >

<property name= "Patterns" >

<!--set entry point-->

<list>

<value>.*save.*</value>

<value>.*do.* </value>

</list>

</property>

</bean>

Code Description:

.*save.*, which means that all methods that start with Save are pointcuts.

.* do.*, which means that all methods that start with Do are pointcuts. 5.4.2 Dynamic Entry point

The difference between a dynamic pointcut and a static pointcut is that it is limited not only to the method and class of the point, but also to the parameters of the method. Because of the variability of parameters, dynamic pointcut can not be cached, need to be calculated every time, so the use of dynamic pointcut has a great performance loss.

Dynamic pointcuts are required when pointcuts need to invoke notifications based on parameter values at execution time. Spring provides a built-in dynamic pointcut: A control flow pointcut. This pointcut matches the call stack based on the current thread. The developer returns true only if a specific class and a specific method are found when the current thread executes.

In fact, most pointcuts can use static pointcuts, so there is little chance of creating dynamic pointcuts. 5.4.3 Custom Pointcut

Because the pointcut in spring is a Java class, rather than a language attribute such as ASPECTJ, you can define a custom pointcut. Since AOP is not fully fledged, the documentation provided by spring does not provide a more detailed explanation in this regard, so there will be no more detailed introduction to Dynamic pointcuts and custom pointcuts.

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.