스프링 프레임워크 aspect AOP

미니멀리즘·2023년 1월 31일
0

참고
https://engkimbs.tistory.com/746
https://hirlawldo.tistory.com/31

Aspect auto proxy - 자동으로 프록시 구성
AOP(Aspect Oriented Programming)
spring aop를 이용한 메소드 추적

web-customer-tracker 파일에 이어서

aspectjweaver-1.8.10.jar - 파일 추가

spring-mvc-crud-demo-servlet.xml

xmlns:aop="http://www.springframework.org/schema/aop" 
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<aop:aspectj-autoproxy/>

LogginAspect.jsp

package com.code.springdemo.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {
	
	//Logger setting up
	private Logger myLogger = Logger.getLogger(getClass().getName());
	
	@Pointcut("execution(* com.code.springdemo.controller.*.*(..))")
	private void forContrtrollerPackage() {}
	
	@Pointcut("execution(* com.code.springdemo.service.*.*(..))")
	private void forServicePackage() {}
	
	@Pointcut("execution(* com.code.springdemo.dao.*.*(..))")
	private void forDaoPackage() {}
	
	@Pointcut("forContrtrollerPackage() || forServicePackage() || forDaoPackage()")
	private void forAppFlow() {}
	
	@Before("forAppFlow()")
	public void before(JoinPoint theJoinPoint) {
		String theMethod = theJoinPoint.getSignature().toShortString();	
		myLogger.info("========> in @Before:calling method: " + theMethod);
	}
	
	@AfterReturning(pointcut="forAppFlow()",returning="theResult")
	public void afterReturning(JoinPoint theJoinPoint, Object theResult) {
		String theMethod = theJoinPoint.getSignature().toShortString();
		myLogger.info("========>> in @AfterReturning:from method: " + theMethod);
		myLogger.info("========>> result: " + theResult);
	}
}
profile
웹 개발자

0개의 댓글

관련 채용 정보