기록해! 정리해!

어노데이션 기본 AOP - Before 어드바이스 본문

Spring

어노데이션 기본 AOP - Before 어드바이스

zsuling 2022. 9. 22. 10:00

1.

<?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" />

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
 

</beans>
========================================================================

​

package com.springbook.biz.common;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

@Service
@Aspect
public class LogAdvice {
   @Pointcut("execution(* com.springbook.biz..*ServiceImpl.*(..))")
   public  void  allPointcut() {}
   
   @Before("allPointcut()")
   public  void  printLog() {
	  System.out.println("[공통로그] 비즈니스 로직 수행 전 동작"); 
   }
   
   @Pointcut("execution(* com.springbook.biz..*ServiceImpl.get*(..))")
   public  void  getPointcut() {}
   
}

 

2.

<?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" />

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
 

</beans>
=========================================================================

​

package com.springbook.biz.common;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

@Service
@Aspect
public class BeforeAdvice {
	
   // 포인트 컷	
   @Pointcut("execution(* com.springbook.biz..*ServiceImpl.*(..))")
   public  void  allPointcut() {}	
	
   // 어드바이스
   @Before("allPointcut()")     
   void  beforeLog(JoinPoint  jp) {
	   
	String  method = jp.getSignature().getName(); // 비즈니스 로직 메소드 이름 불러오기 
	Object[] args = jp.getArgs(); // 비즈니스 로직 메소드의 매개변수에 들어오는 args 값 읽어오기 
	
	   System.out.println("[사전처리]" + method + "()메소드 ARGS 정보: " + args[0].toString() );
	   
   }
}
=========================================================================

​

package com.springbook.biz.common;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

@Service
@Aspect
public class BeforeAdvice {
	
   // 포인트 컷	
   @Pointcut("execution(* com.springbook.biz..*ServiceImpl.*(..))")
   public  void  allPointcut() {}	
	
   // 어드바이스
   @Before("allPointcut()")     
   void  beforeLog(JoinPoint  jp) {
	   
	String  method = jp.getSignature().getName(); // 비즈니스 로직 메소드 이름 불러오기 
	Object[] args = jp.getArgs(); // 비즈니스 로직 메소드의 매개변수에 들어오는 args 값 읽어오기 
	
	   System.out.println("[사전처리]" + method + "()메소드 ARGS 정보: " + args[0].toString() );
	   
   }

}

'Spring' 카테고리의 다른 글

Spring JDBC 방법 2  (0) 2022.09.22
Spring JDBC 방법  (0) 2022.09.22
JoinPoint 메소드 사용하기  (0) 2022.09.22
AOP 사용하기  (0) 2022.09.22
비즈니스 컴포넌트 실습 II ( 어노테이션으로 수정 )  (0) 2022.09.22
Comments