기록해! 정리해!

JoinPoint 메소드 사용하기 본문

Spring

JoinPoint 메소드 사용하기

zsuling 2022. 9. 22. 09:59

[ Before 어드바이스 ] : 비즈니스 로직 메소드 이름과 매개변수 값 을 사용 할 수 있다. 

package com.springbook.biz.common;

import org.aspectj.lang.JoinPoint;

public class BeforeAdvice {
	
   void  beforeLog(JoinPoint  jp) {
	   
	String  method = jp.getSignature().getName(); // 비즈니스 로직 메소드 이름 불러오기 
	Object[] args = jp.getArgs(); // 비즈니스 로직 메소드의 매개변수에 들어오는 args 값 읽어오기 
	
   System.out.println("[사전처리]" + method + "()메소드 ARGS 정보: " + args[0].toString() );	      
   }
}
=====================================================================

​

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

<context:component-scan base-package="com.springbook.biz" />

<bean id="beforeAdvice" class="com.springbook.biz.common.BeforeAdvice" />
<aop:config>
  
   <aop:pointcut id="allPointcut" 
       expression="execution(* com.springbook.biz..*ServiceImpl.*(..))"  />
  
  <aop:aspect ref="beforeAdvice">
    <aop:before method="beforeLog" pointcut-ref="allPointcut" />                
  </aop:aspect>
  
</aop:config>


</beans>

[ After Returning 어드바이스 ]

1) 비즈니스 로직 메소드 이름과 매개변수 값 을 사용 할 수 있다.

2) 리턴되는 값을 사용 할 수 있다.

package com.springbook.biz.common;

import org.aspectj.lang.JoinPoint;

import com.springbook.biz.user.UserVo;

public class AfterReturningAdvice {
   void  afterLog(JoinPoint  jp, Object  returnObj) {
	   
	 String method=jp.getSignature().getName();
	 
	
	 if ( returnObj  instanceof  UserVo) {
		 System.out.println("===> returnObj  instanceof  UserVo  확인");
		 UserVo user =(UserVo) returnObj;
		 if(user.getRole().equals("User") ) {
			System.out.println(user.getName() + "로그인(Admin)");
		} 
	 }
	
	 
	 System.out.println("[ 사후처리 ]" + method +
			 "() 메소드 리턴값:" + returnObj.toString()  );
	   
   }
}
=======================================================================

​

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

<context:component-scan base-package="com.springbook.biz" />

<bean id="beforeAdvice" class="com.springbook.biz.common.BeforeAdvice" />
<bean id="afterReturningAdvice" class="com.springbook.biz.common.AfterReturningAdvice" />

<aop:config>
  
   <aop:pointcut id="allPointcut" 
       expression="execution(* com.springbook.biz..*ServiceImpl.*(..))"  />
 
  <aop:aspect ref="beforeAdvice">
    <aop:before method="beforeLog" pointcut-ref="allPointcut" />                
  </aop:aspect>
  
  <aop:aspect ref="afterReturningAdvice">
    <aop:after-returning method="afterLog" 
         returning="returnObj" pointcut-ref="allPointcut" />                
  </aop:aspect>

</aop:config>

</beans>



[ Around 어드바이스 ]

package com.springbook.biz.common;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;

public class AroundAdvice {
  
	public  Object aroundLog( ProceedingJoinPoint  pjp) throws  Throwable {
		
		String method = pjp.getSignature().getName();
		
		StopWatch stopWatch = new StopWatch(); 
		stopWatch.start();
		
		Object returnObj =pjp.proceed();
		stopWatch.stop();
		
		System.out.println( method + "() 메서드 수행에 걸린 시간 : " + stopWatch.getTotalTimeMillis() + "(ms)초" );
		
		return returnObj;		
	}	
}
==========================================================================

​

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

<context:component-scan base-package="com.springbook.biz" />

<bean id="beforeAdvice" class="com.springbook.biz.common.BeforeAdvice" />
<bean id="afterReturningAdvice" class="com.springbook.biz.common.AfterReturningAdvice" />
<bean id="aroundAdvice" class="com.springbook.biz.common.AroundAdvice" />

<aop:config>
  
   <aop:pointcut id="allPointcut" 
       expression="execution(* com.springbook.biz..*ServiceImpl.*(..))"  />
 
 <!-- 
  <aop:aspect ref="beforeAdvice">
    <aop:before method="beforeLog" pointcut-ref="allPointcut" />                
  </aop:aspect>
  
  <aop:aspect ref="afterReturningAdvice">
    <aop:after-returning method="afterLog" 
         returning="returnObj" pointcut-ref="allPointcut" />                
  </aop:aspect>
  
   -->
   
 <aop:aspect ref="aroundAdvice">
    <aop:around  method="aroundLog" 
         pointcut-ref="allPointcut" />                
  </aop:aspect>

</aop:config>

</beans>
Comments