Joinpoint VS ProceedingJoinPoint in AOP using aspectJ

If you use before,after-throwing,after-returning and after use Jointpoint.

@Pointcut("execution(* com.pointel.aop.test1.AopTest.beforeAspect(..))")  
public void adviceChild(){}  

@Before("adviceChild()")  
public void beforeAdvicing(JoinPoint joinPoint /*,ProceedingJoinPoint pjp - used refer book marks of AOP*/){ 

    //Used to get the parameters of the method !
    Object[] arguments = joinPoint.getArgs();
    for (Object object : arguments) {
        System.out.println("List of parameters : " + object);
    }

    System.out.println("Method name : " + joinPoint.getSignature().getName());
    log.info("beforeAdvicing...........****************...........");
    log.info("Method name : " + joinPoint.getSignature().getName());
    System.out.println("************************"); 
}

If you use around use Proceedingjoinpoint .

An around advice is a special advice that can control when and if a method (or other join point) is executed. This is true for around advices only, so they require an argument of type ProceedingJoinPoint, whereas other advices just use a plain JoinPoint. A sample use case is to cache return values:

private SomeCache cache;

@Around("some.signature.pattern.*(*)")
public Object cacheMethodReturn(ProceedingJoinPoint pjp){
    Object cached = cache.get(pjp.getArgs());
    if(cached != null) return cached; // method is never executed at all
    else{
        Object result = pjp.proceed();
        cache.put(pjp.getArgs(), result);
        return result;
    }
}

In this code (using a non-existent cache technology to illustrate a point) the actual method is only called if the cache doesn’t return a result. This is the exact way the Spring EHCache Annotations project works, for example.

Another specialty of around advices is that they must have a return value, whereas other advice types must not have one.

use xml config

Note the pjp.proceed() will call the real method it arounds. 

	<aop:config>
		<!-- ***** Pointcut Definitions ***** -->
		<aop:pointcut id="allServicePointcut"
			expression="bean(*Service) or bean(*ServiceExternal) and not within(org.xxx.xxx.server.ws..*)" />

		<!-- ***** Aspects ***** -->
		<!--
			Aspect to handle transaction management for all internal and external
			service API's.
		-->
		<aop:aspect id="hibernateProxyRemovalAspect" ref="hibernateProxyRemovalAdvice" order="3">
			<aop:around pointcut-ref="allServicePointcut" method="removeProxy" />
		</aop:aspect>
	</aop:config>

	<bean id="hibernateProxyRemovalAdvice"
		class="org.xxx.framework.dao.HibernateProxyRemovalAdvice">
		<property name="hibernateProxyRemover" ref="hibernateProxyRemover" />
	</bean>

	<bean id="hibernateProxyRemover"
		class="org.xxx.framework.dao.HibernateProxyRemover" />
// Imports
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * Advice that removes hibernate proxy objects.
 */
public class HibernateProxyRemovalAdvice
{
    private HibernateProxyRemover hibernateProxyRemover;

    public Object removeProxy(ProceedingJoinPoint pjp) throws Throwable
    {
        return hibernateProxyRemover.removeProxy(pjp.proceed());
    }

    public HibernateProxyRemover getHibernateProxyRemover()
    {
        return hibernateProxyRemover;
    }

    public void setHibernateProxyRemover(HibernateProxyRemover hibernateProxyRemover)
    {
        this.hibernateProxyRemover = hibernateProxyRemover;
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s