Spring AOP Around advise

Around advice:


Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It 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.

Let’s understand it with Example

Project structure


Student.java

 
package com.training;
 
public class Student 
{
 
	String name;
 
	public String getName() 
	{
		return name;
	}
 
	public void setName(String name) 
	{
		this.name = name;
	}
 
 
	public void get_info()
	{
		System.out.println("Hello "+name);	
	}
 
}

loggingAspect.java

package com.training;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
 
@Aspect
public class LoggingAspect 
{
 
	@Around("execution(public void get_info())")
	public void loggingadvise(ProceedingJoinPoint p)
	{
 
		System.out.println("Advise is runnning before get_info() method is called.....");
 
		try 
		{
			p.proceed();
		}
		catch (Throwable e) 
		{
			e.printStackTrace();
		}
 
		System.out.println("Advise is runnning after get_info() method is called.....");
 
    }
}

spring-config.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-2.0.xsd
				http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-5.0.0.xsd">
 
<aop:aspectj-autoproxy/>	
 
<bean name="stu" class="com.training.Student">
<property name="name"  value="bhavesh_lakhani" />
</bean>
 
<bean name="loggingAspect" class="com.training.LoggingAspect"/>
 
</beans>

TestAroundAdvise.java

package com.training;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestAroundAdvise 
{
 
	public static void main(String[] args) 
	{
 
	   ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
 
	   Student st=(Student)context.getBean("stu");
 
	   st.get_info();
 
	   System.out.println("********************");
 
	}
}

Output:

Download this project developed using eclipse
AroundAdvise

Spring AOP Before Advice

Before advice:

Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).

Let’s Understand using Example

Project Structure


Student.java

package com.training;
 
public class Student 
{
 
	String name;
 
	public String getName() 
	{
		return name;
	}
 
	public void setName(String name) 
	{
		this.name = name;
	}
 
 
	public void get_info()
	{
		System.out.println("Hello "+name);	
	}
 
}

LoggingAspect.java

package com.training;
 
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Aspect;
 
@Aspect
public class LoggingAspect 
{
 
	@Before("execution(public void get_info())")
	public void loggingadvise()
	{
 
		System.out.println("Advise is runnning before get_info() method is called.....");
 
	}
 
}

Spring-config.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-2.0.xsd
				http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-5.0.0.xsd">
 
<aop:aspectj-autoproxy/>	
 
<bean name="stu" class="com.training.Student">
<property name="name"  value="bhavesh_lakhani" />
</bean>
 
<bean name="loggingAspect" class="com.training.LoggingAspect"/>
 
</beans>

TestAfterAdvise.java

package com.training;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestBeforeAdvise 
{
 
	public static void main(String[] args) 
	{
 
	   ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
 
	   Student st=(Student)context.getBean("stu");
 
       st.get_info();
 
       System.out.println(st.getName());
 
	}
}

Output:

Download this project developed using eclipse

BeforeAdvise