Declaring an advisor

Spring provides another mechanism to define advice and aspect as a single unit. It's called an advisor. It's only available in Spring AOP and not in native AspectJ. In this model, you need to define advice as a class that implements one of the advice interfaces. An advisor can be defined with the <aop:advisor> element in the application context (XML) file as follows:

<aop:config>
<aop:pointcut id="loggingPointcut" expression="execution(*
com.packt.spring.aop.dao.*.*(..))" />
<aop:advisor advice-ref="loggingAdvice"
pointcut-ref="loggingPointcut" id="loggingInterceptorAdvisor" />
</aop:config>

<bean id="loggingAdvice" class="com.packt.spring.aop.advisor.LoggingAdvisor" />

You need to define an advisor with the <aop:advisor> element within <aop:config>. You can define a point-cut advisor with the point-cut-ref attribute. In the previous example, we have defined an in-lined point-cut. If you are following annotation-based AOP, you can refer to any public point-cut defined in the aspect class as follows:

<aop:advisor advice-ref="loggingAdvice"
pointcut-ref= "com.packt.spring.aop.aspects.PermissionCheck.checkReportPermission()" id="loggingInterceptorAdvisor" />

In this example, we are referring to the point-cut with its signature (checkReportPermission()) defined in the PermissionCheck aspect class. 

We also defined a bean with the  LoggingAdvisor class, which is an advisor class, and referred in the <aop:advisor> element with the advice-ref attribute. The LogginAdvisor class is defined as follows:

public class LoggingAdvisor implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws
Throwable {
System.out.println("****************** Starting "+method.getName()+" method
*****************");
}
}

This advisor class is implementing the before method of the MethodBeforeAdvise interface. It's equivalent to implementing before advice. Spring AOP provides other sets of advice interfaces, such as MethodInterceptor, ThrowsAdvice, and AfterReturningAdvice, which are equivalent to implementing around advice, after throwing advice, and after returning advice respectively. 

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

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