본문 바로가기
개발/Spring Boot

7.aop 설정

by 카리3 2021. 2. 11.

1. pom.xml 설정

		<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-aop</artifactId>
		</dependency>	

 

2. LogAdvice 설정

package com.kangong.common.aop;

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import lombok.extern.log4j.Log4j2;

@Aspect
@Log4j2
@Component
public class LogAdvice {
	
	@Before("execution(* com.kangong.sample.controller.SampleController*.*(..))")
	public void logBefore() {
		log.info("logBefore=========================================");
	}
	
	@AfterThrowing(pointcut = "execution(* com.kangong.sample.controller.SampleController*.*(..))", throwing="exception")
	  public void logException(Exception exception) {
	    
	    log.info("Exception....!!!!");
	    log.info("exception: "+ exception);
	  
	  }
	  
	  
	  @Around("execution(* com.kangong.sample.controller.SampleController*.*(..))")
	  public Object logTime( ProceedingJoinPoint pjp) {
	    
	    long start = System.currentTimeMillis();
	    log.info("Target: " + pjp.getTarget());
	    log.info("Param: " + Arrays.toString(pjp.getArgs()));
	    
	    
	    //invoke method 
	    Object result = null;
	    
	    try {
	      result = pjp.proceed();
	    } catch (Throwable e) {
	      // TODO Auto-generated catch block
	      e.printStackTrace();
	    }
	    
	    long end = System.currentTimeMillis();
	    
	    log.info("TIME: "  + (end - start));
	    
	    return result;
	  }

}

 

 

 

'개발 > Spring Boot' 카테고리의 다른 글

9. Annotation 설정  (0) 2021.02.12
8. Spring Security 설정  (0) 2021.02.12
6.thymeleaf 설정  (0) 2021.02.11
5. Lombok 설정  (0) 2021.02.11
4. log4j2 설정  (0) 2021.02.07