Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Around Advice

Around advice is the most powerful advice in Spring AOP. The reason is, this advice can perform custom behavior before and after the method invocation. This is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

The kinds of advise available in Spring AOP are BEFORE, AFTER, AFTER RETURNING, AFTER THROWING and AROUND. The first four are simple and get called in aspects as their name suggest. The last one 'around' is a different one. Well why this one is different from others is, the spring framework basically wraps the target join point(or target method) into the around advise. How is it done? The spring framework provides a ProceedingJoinPoint parameter which helps to weave the target method into around advice.

Rose.java

package org.vtechnology.model;

public class Rose {

private String name;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Lotus.java

package org.vtechnology.model;

public class Lotus {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Flower.java

package org.vtechnology.service;

import org.vtechnology.model.Lotus;
import org.vtechnology.model.Rose;

public class Flower {

private Rose rose;
private Lotus lotus;
public Rose getRose() {
return rose;
}
public void setRose(Rose rose) {
this.rose = rose;
}
public Lotus getLotus() {
return lotus;
}
public void setLotus(Lotus lotus) {
this.lotus = lotus;
}
}

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context">

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<aop:aspectj-autoproxy/>

<bean id="rose" class="org.vtechnology.model.Rose">
<property name="name" value="Flower Name is Rose"/>
</bean>

<bean id="lotus" class="org.vtechnology.model.Lotus">
<property name="name" value="Flower Name is Lotus"/>
</bean>

<bean name="flower" class="org.vtechnology.service.Flower"
autowire="byName">

<bean class="org.vtechnology.aop.LoggingAOP"/>

</beans>

LoggingAOP.java

package org.vtechnology.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Around;

@Aspect
public class LoggingAOP {

@Pointcut("execution(public String get*())")
public void pointcutGetter() {
}

@Around("pointcutGetter()")
public Object invoke(ProceedingJoinPoint proceedingJoinPoint){
Object returnValue = null;
try {
System.out.println("Before Throwing");
returnValue = proceedingJoinPoint.proceed();
System.out.println("After Returning");
} catch (Throwable e) {
System.out.println("After Throwing");
}
System.out.println("After Finally");
return returnValue;
}

}

Here is the Aspect class example. The invoke() method will be called before executing runMyBusinessLogic() method. Also you have to define joinPoint.proceed() to control when should the interceptor return the control to the original calling method runMyBusinessLogic().

AopMain.java

package org.vtechnology.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopMain {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext
("spring.xml");

Flower flower = context.getBean("flower", Flower.class);
flower.getLotus();
}
}

Around advice is a combination of before, after-returning and after-throwing advices. Around advice is wrapped around a method call and called both before and after of a method execution. The difference is that in before and after advices there are two separate advices but here there is only one advice containing logic of all three (before, after-returning and after-throwing) advices.


     << Previous
Next >>