Declaring a pointcut by R4R Team

Declaring a pointcut:-
A pointcut helps in determining the join points (ie methods) of interest to be executed with different advices. While working with @AspectJ based configuration,Spring AOP only supports method execution join points for Spring beans, so you can think of a pointcut as matching the execution of methods on Spring beans. A pointcut declaration has two parts: a signature comprising a name and any parameters, and a pointcut expression that determines exactly which method executions we are interested in. In the @AspectJ annotation-style of AOP, a pointcut signature is provided by a regular method definition, and the pointcut expression is indicated using the @Pointcut annotation .

The set of pointcut designators supported by Spring AOP may be extended in future releases to support more of the AspectJ pointcut designators.

execution - for matching method execution join points, this is the primary pointcut designator you will use when working with Spring AOP

within - limits matching to join points within certain types (simply the execution of a method declared within a matching type when using Spring AOP)

this - limits matching to join points where the bean reference (Spring AOP proxy) is an instance of the given type

target - limits matching to join pointswhere the target object (application object being proxied) is an instance of the given type

args - limits matching to join points where the arguments are instances of the given types

@target - limits matching to join points where the class of the executing object has an annotation of the given type

@args - limits matching to join points where the runtime type of the actual arguments passed have annotations of the given type(s)

@within - limits matching to join points within types that have the given annotation (the execution of methods declared in types with the given annotation when using Spring AOP)

@annotation - limits matching to join points where the subject of the join point (method being executed in Spring AOP) has the given annotation.

The following example defines a pointcut named 'loginService' that will match the execution of every method available in the classes under the package com.xyz.myapp.service:

import org.aspectj.lang.annotation.Pointcut;

@Pointcut("execution(* com.xyz.in.myapp.service *(..))")

//expression

private void loginService() {} // signature

The following example defines a pointcut named 'getname' that will match the execution of getName() method available in Employee class under the package com.r4r.in:

import org.aspectj.lang.annotation.Pointcut;

@Pointcut("execution(* com.r4r.in.Employee.getName(..))")

private void getname() {}

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!