dineshonjava

Spring AOP JoinPoints and Advice Arguments

Spring JoinPoints and Advice Arguments

Join point: A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution. Join point information is available in advice bodies by declaring a parameter of type org.aspectj.lang.JoinPoint.
We'll now learn about join points, and how we can use arguments in the advice methods to get information about join points.
package com.dineshonjava.sdnext.aop.tutorial.aspectJ;

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

import com.dineshonjava.sdnext.aop.tutorial.aspectJ.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().setName("Dummy Circle");
        System.out.println(service.getCircle().getName());
    }
}
package com.dineshonjava.sdnext.aop.tutorial.aspectJ;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import com.dineshonjava.sdnext.aop.tutorial.aspectJ.model.Circle;

@Aspect
public class LoggingAspect {
   
    @Before("allCircleMethods()")
    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());
    }
   
//    @Before("args(name)")
//    public void allStringMethodArguments(String name){
//       
//        System.out.println("A setter method has been executed............"+name);
//    }
   
    @Before("args(String)")
    public void allStringMethodArguments(){
   
    System.out.println("A setter method has been executed............");
    }
      
    @Pointcut("within(com.venkat.Spring.Aop.model.*)")
    public void allCircleMethods(){}
  }

package com.dineshonjava.sdnext.aop.tutorial.aspectJ.model;

public class Circle {

    private String name;

    public String getName() {
        return name;
    }

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

package com.dineshonjava.sdnext.aop.tutorial.aspectJ.model;

public class Triangle {

    private String name;

    public String getName() {
        return name;
    }

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

package com.dineshonjava.sdnext.aop.tutorial.aspectJ.service;

import com.dineshonjava.sdnext.aop.tutorial.aspectJ.model.Circle;
import com.dineshonjava.sdnext.aop.tutorial.aspectJ.model.Triangle;

public class ShapeService {

    private Triangle triangle;
    private Circle circle;
    public Triangle getTriangle() {
        return triangle;
    }
    public void setTriangle(Triangle triangle) {
        this.triangle = triangle;
    }
    public Circle getCircle() {
        return circle;
    }
    public void setCircle(Circle circle) {
        this.circle = circle;
    }
}

<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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 class="com.dineshonjava.sdnext.aop.tutorial.aspectJ.model.Triangle" name="triangle">
    <property name="name" value="triangle name">
    </property></bean>
    <bean class="com.dineshonjava.sdnext.aop.tutorial.aspectJ.model.Circle" name="circle">
    <property name="name" value="circle name">
    </property></bean>
    <bean class="com.dineshonjava.sdnext.aop.tutorial.aspectJ.service.ShapeService" name="shapeService">
    <property name="triangle" ref="triangle">
    <property name="circle" ref="circle">
    </property></property></bean>
    <bean class="com.dineshonjava.sdnext.aop.tutorial.aspectJ.Aspect.LoggingAspect" name="loggingAdvice">
</bean></aop:aspectj-autoproxy></beans>

Spring AOP Related Posts
  1. Spring AOP Interview Questions and Answers
  2. Spring AOP-Introduction to Aspect Oriented Programming
  3. @Aspect Annotation in Spring
  4. Advices in Spring AOP
  5. Spring AOP-Declaring pointcut Expressions with Examples
  6. Spring AOP XML configuration
  7. Spring AOP XML Schema based Example
  8. Spring AOP AspectJ @Before Annotation Advice Example
  9. Spring AOP Before Advice Example using XML Config
  10. Spring AOP AspectJ @After Annotation Advice Example
  11. Spring AOP After Advice Example using XML Config
  12. Spring AOP AspectJ @AfterReturning Annotation Advice Example
  13. Spring AOP After-Returning Advice Example using XML Config
  14. Spring AOP AspectJ @AfterThrowing Annotation Advice Example
  15. Spring AOP After Throwing Advice Example using XML Config
  16. Spring AOP AspectJ @Around Annotation Advice Example
  17. Spring AOP Around Advice Example using XML Config
  18. Spring AOP Writing First AspectJ Program in Spring
  19. Spring AOP Proxies in Spring
  20. Spring AOP Transaction Management in Hibernate
  21. Spring Transaction Management
  22. Spring Declarative Transaction Management Example
  23. Spring AOP-Ordering of Aspects with Example