Declaring point-cut

Point-cuts are regular expressions or patterns to filter join-points where we want to apply advice. Since Spring AOP only supports method-level join-points, you can consider a point-cut as a matching of method execution on Spring beans. In the @AspectJ annotation style, a point-cut is declared by a method of an Aspect class (declared with the @Aspect annotation). Such methods are called point-cut signatures. The @Pointcut annotation is used to define such a method as follows:

@Aspect
public class SessionCheck {
@Pointcut("execution( * com.packt.spring.aop.service.*.*(..))") // Expression
private void validateSession() {// Point-cut signature
}
}

In this code, the validateSession method represents a point-cut signature, while the @Pointcut annotation is used to describe a point-cut expression. The preceding point-cut is applied to all public methods of all classes under the com.packt.spring.aop.service package that have any parameters and return values. A method that represents a point-cut signature must have void as a return type. The preceding annotation-based AOP is equivalent to XML-based AOP configuration, as follows:

<aop:config>
<!-- point cut declaration -->
<aop:pointcut id="checkValidUser"
expression="execution(* com.packet.spring.aop.service.*.*(..))" />

<!-- aspect configuration -->
<aop:aspect id="mySessionCheck" ref="checkSessionAspect">
//Advice declaration goes here.
</aop:aspect>

<!-- spring bean represents an aspect -->
<bean id="checkSessionAspect"
class="com.packet.spring.aop.aspects.SessionCheck">
</bean>
</aop:config>

Just like XML-based AOP, you can use various point-cut types (point-cut designators) such as within, this, target, args@annotation, @target, @withinand @args with annotation-style AOP. On top of these, Spring supports an additional point-cut type (PCD) called bean, which matches the method execution on a particular bean or set of beans. It can be declared as follows:

@Pointcut("bean(batchOperation)")
private void captureBatchProcess() {

}

This point-cut is applied to the bean with an ID or name of batchOperation defined in the application context (XML) file. If a wildcard (only * is allowed) is used, this point-cut can be applied on multiple beans.

Just like XML-based configuration, annotation-based AOP also supports combining point-cut with and, or, and negated operations. In @AspectJ-style AOP, you can refer point-cut with its name (signature) while combining with other point-cut, or referring them in Aspect declaration in XML schema. Multiple point-cuts can be combined as follows:

@Pointcut("execution( * com.packt.spring.aop.report.*.*(..))")
private void checkSession() {
}

@Pointcut("args(String)")
private void printUserName() {
}

@Pointcut("checkSession() && printUserName()")
private void userSession() {
}

The first point-cut, checkSession, will be applied on all public methods of any class under the com.packt.spring.aop.report package. The second point-cut, printUserName, will be applied on any public method with a single argument of type String, while the third point-cut, userSession, is applicable on all public methods that have a single argument of type String of any classes under the com.packt.spring.aop.report package. We have used the name (point-cut signature) of the first and second point-cuts to combine them in the third point-cut definition.

It's common practice to create smaller point-cuts with simple expressions, and build complex point-cuts by combining them with and , or, and negated operations. By referring to its name, point-cuts are so simple to define, and yet powerful when combining with other point-cuts.

Since point-cuts are referred by method name, the visibility of the Java method is applied to point-cuts. For example, private point-cuts are used in the same type, protected point-cuts in the same package, and public point-cuts can be applied anywhere (that is, can be referred to in other aspect classes in different hierarchies). This brings great flexibility when building an enterprise application with multiple modules, and when you want to share a set of operations between various aspects. You can make public point-cuts in common aspects that can be shared with other aspects in different modules.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.71.94