This example covers the Around advice type. We'll learn how to use it, and we'll also look at some of the unique and powerful features that are specific to this advice type.
package com.venkat.Spring.Aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.venkat.Spring.Aop.service.ShapeService;
public class AopTest {
public static void main(String args[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ShapeService service=context.getBean("shapeService",ShapeService.class);
service.getCircle();
}
}
**************************************************************************
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.venkat.Spring.Aop.service.ShapeService;
public class AopTest {
public static void main(String args[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ShapeService service=context.getBean("shapeService",ShapeService.class);
service.getCircle();
}
}
**************************************************************************
package com.venkat.Spring.Aop.Aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import com.venkat.Spring.Aop.model.Circle;
@Aspect
public class LoggingAspect {
public void loggingAdvice(JoinPoint joinpoint)
{
System.out.println("Run Advice..... get method Executed");
System.out.println(joinpoint.toString());
Circle c=(Circle)joinpoint.getTarget();
System.out.println(c.getName());
}
@AfterReturning(pointcut="args(name)",returning="returnString")
public void allStringMethodArguments(String name, String returnString){
System.out.println("A setter method has been executed............"+name);
}
@AfterThrowing(pointcut="args(name)",throwing="ex")
public void exceptionAdvice(String name, Exception ex){
System.out.println("Exception is thrown ............"+ex);
}
@After("args(String)")
public void afterAdvice(){
System.out.println("After Advice is executed...........");
}
@Pointcut("execution(* get*())")
public void allGetters(){}
@Around("allGetters()")
public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint)
{
Object returnValue=null;
try {
System.out.println("Before method Executed");
returnValue=proceedingJoinPoint.proceed();
System.out.println("After Method Executed....");
} catch (Throwable e) {
System.out.println("Around Advice throws exception");
}
System.out.println("After Finally executed........");
return returnValue;
}
}
***************************************************************************
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import com.venkat.Spring.Aop.model.Circle;
@Aspect
public class LoggingAspect {
public void loggingAdvice(JoinPoint joinpoint)
{
System.out.println("Run Advice..... get method Executed");
System.out.println(joinpoint.toString());
Circle c=(Circle)joinpoint.getTarget();
System.out.println(c.getName());
}
@AfterReturning(pointcut="args(name)",returning="returnString")
public void allStringMethodArguments(String name, String returnString){
System.out.println("A setter method has been executed............"+name);
}
@AfterThrowing(pointcut="args(name)",throwing="ex")
public void exceptionAdvice(String name, Exception ex){
System.out.println("Exception is thrown ............"+ex);
}
@After("args(String)")
public void afterAdvice(){
System.out.println("After Advice is executed...........");
}
@Pointcut("execution(* get*())")
public void allGetters(){}
@Around("allGetters()")
public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint)
{
Object returnValue=null;
try {
System.out.println("Before method Executed");
returnValue=proceedingJoinPoint.proceed();
System.out.println("After Method Executed....");
} catch (Throwable e) {
System.out.println("Around Advice throws exception");
}
System.out.println("After Finally executed........");
return returnValue;
}
}
***************************************************************************
package com.venkat.Spring.Aop.model;
public class Circle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("Setter method is called.....");
throw(new RuntimeException());
}
public String setNameAndReturning(String name) {
this.name = name;
System.out.println("Setter Method and return the value method executed....");
return name;
}
}
****************************************************************************
public class Circle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("Setter method is called.....");
throw(new RuntimeException());
}
public String setNameAndReturning(String name) {
this.name = name;
System.out.println("Setter Method and return the value method executed....");
return name;
}
}
****************************************************************************
package com.venkat.Spring.Aop.model;
public class Triangle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
*****************************************************************************
public class Triangle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
*****************************************************************************
<?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-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean name="triangle" class="com.venkat.Spring.Aop.model.Triangle">
<property name="name" value="triangle name"/>
</bean>
<bean name="circle" class="com.venkat.Spring.Aop.model.Circle">
<!-- <property name="name" value="circle name"/> -->
</bean>
<bean name="shapeService" class="com.venkat.Spring.Aop.service.ShapeService">
<property name="triangle" ref="triangle"/>
<property name="circle" ref="circle"/>
</bean>
<bean name="loggingAdvice" class="com.venkat.Spring.Aop.Aspect.LoggingAspect"/>
</beans>
<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-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean name="triangle" class="com.venkat.Spring.Aop.model.Triangle">
<property name="name" value="triangle name"/>
</bean>
<bean name="circle" class="com.venkat.Spring.Aop.model.Circle">
<!-- <property name="name" value="circle name"/> -->
</bean>
<bean name="shapeService" class="com.venkat.Spring.Aop.service.ShapeService">
<property name="triangle" ref="triangle"/>
<property name="circle" ref="circle"/>
</bean>
<bean name="loggingAdvice" class="com.venkat.Spring.Aop.Aspect.LoggingAspect"/>
</beans>
No comments:
Post a Comment