Home  >  Spring Core

Spring ProceedingJoinPoint Example

By Arvind Rai, August 13, 2013
org.aspectj.lang.ProceedingJoinPoint in spring is used for the method which advices to method in AOP. ProceedingJoinPoint is used as an argument of the methods which hints for before, after, after throwing and around. ProceedingJoinPoint has the methods like getKind, getTarget, proceed etc. Find the use of these methods below with complete example.
UserAspect.java
package com.concretepage;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class UserAspect {
	@Around("execution(* com.concretepage.bean.UserService.doTask(..))")
	public void userAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("find my advice before the method...");
		System.out.println(joinPoint.getKind());
		System.out.println(joinPoint.getTarget());
		Object[] intA = joinPoint.getArgs();
		for(int i=0;i<intA.length;i++){
			System.out.println(intA[i]);
		}
		joinPoint.proceed();
		System.out.println(joinPoint.getSignature().getName());
		System.out.println("find my advice after the method...");
	}
} 




AspectConfig.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.concretepage.bean.UserService;
@Configuration
@EnableAspectJAutoProxy
public class AspectConfig {
  @Bean
  public UserService userService(){
	 return new UserService();
  }
  @Bean	
  public UserAspect userAspect(){
	 return new UserAspect();
  }
} 

UserService.java
package com.concretepage.bean;
public class UserService {
	public void doTask(int a, int b)  {
		System.out.println("Multiply a and b:"+a*b);
	}
} 

pom.xml
<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
      <optional>true</optional>
    </dependency>
    
    <dependency>
	  <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
	  <version>2.1_3</version>
	  <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.5.2-Final</version>
     <optional>true</optional>
    </dependency>
    
    <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>3.6.0.Final</version>
     <optional>true</optional>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>2.0.8</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-tx</artifactId>
	  <version>3.1.0.RELEASE</version>
	  <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp-all</artifactId>
      <version>1.3</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-aop</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    	
    <dependency>
	  <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>3.1.0.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>3.1.0.RELEASE</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-expression</artifactId>
     <version>3.1.0.RELEASE</version>
     <scope>compile</scope>
    </dependency>
    
    <dependency>
	 <groupId>org.springframework</groupId>
     <artifactId>spring-asm</artifactId>
	  <version>3.1.0.RELEASE</version>
	 <scope>compile</scope>
    </dependency>	
    
    <dependency>
	 <groupId>mysql</groupId>
	 <artifactId>mysql-connector-java</artifactId>
	 <version>5.1.17</version>
    </dependency>
    
    <dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
    </dependency>

  </dependencies> 

SpringAOPTest.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.concretepage.bean.UserService;
public class SpringAOPTest {
  public static void main(String[] args) {
	   AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
       ctx.register(AspectConfig.class);
       ctx.refresh();
       UserService userService = ctx.getBean(UserService.class);
       userService.doTask(2,3);
   }
}  

Output
find my advice before the method...
method-execution
com.concretepage.bean.UserService@1b1756a4
2
3
Multiply a and b:6
doTask
find my advice after the method... 
Download Source Code
spring-proceedingjoinpoint-example.zip
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us