스프링(AOP) 어노테이션

서울IT코드정리 /kyChoi·2021년 11월 12일
0

스프링

목록 보기
10/17
<aop:aspect id="logger" ref="logAop">
<!--within(com.javalec.ex.*) 이런 문법을 AspectJ 이라고 합니다  pom.xml 에서 AOP 로 dependecy 로 확인가능합니다  -->
	<aop:pointcut expression="within(com.javalec.ex.*)" id="publicM"/> 
	<aop:around pointcut-ref="publicM" method="loggerAop"/>
</aop:aspect>
</aop:config>

<!--LogAop 를 어노테이션으로 실행하겠다  -->
<aop:aspectj-autoproxy/>
<bean id ="logAop" class="com.javalec.ex.LogAop"/>
//<aop> 로직이 굉장히 짧아졌다
package com.javalec.ex;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect//이 클라스가 Aspect 역할을 합니다 ,공통 클래스
public class LogAop {
	
	@Pointcut("within(com.javalec.ex.*)") //이 범위에서 공통기능이 적용됩니다 , 메소드 안의 내용은 필요 없음
	private void pointcutMethod() {}

	@Around("pointcutMethod()") //xml 에선 <aop:pointcut >했습니다 위에서 정해준 Pointcut 메소드를 사용합니다, 이 메소드가 pointcutMethod 를 찾아가고 Pointcut Method 가 지정한 범위에서 실행됩니다
	public Object loggerAop(ProceedingJoinPoint joinpoint) throws Throwable{
		String signatureStr = joinpoint.getSignature().toShortString();
		System.out.println(signatureStr + "is start.");
		long st = System.currentTimeMillis();
		
		try {
			Object obj = joinpoint.proceed();
			return obj;
		}
		finally {
			long et = System.currentTimeMillis();
			System.out.println(signatureStr + "is finished");
			System.out.println(signatureStr + "경과시간 : " +(et - st));
		}
	}
	@Before("within(com.javalec.ex.*)") //핵심기능이 실행되기 전에 이 메소드가 실행됩니다
	public void beforeAdvice() {
		System.out.println("beforeAdvice()");
	}
}

profile
건물주가 되는 그날까지

0개의 댓글