애노테이션에 AOP를 걸려고 검색했더니 애노테이션 이름만 달랑 걸어긴거 따라했더니 안된다.

aspectJ 표현식도 import랑 마찬가지라 package 찾아서 간다.

같은 패키지면 이름만 걸어도 찾지만 다를 경우 패키지 명까지 풀네임 적어줘야 한다.


예) @Around("@annotation(test.annotation.ConfigureProperties)")

XML설정

advice 클래스

public class Advisor {

public String returner(ProceedingJoinPoint joinPoint, HttpServletRequest req) throws Throwable  {

String URI = req.getRequestURI();

String contextPath = req.getContextPath();

joinPoint.proceed();

System.out.println("after");

System.out.println(URI);

System.out.println(contextPath);

String returnURI = URI.substring(contextPath.length()); 

return returnURI;

}

}

advice를 등록한다.

<bean id="myAdvice" class="com.zxy.Advisor" >

AOP-Config설정을 한다

<aop:config>

<aop:aspect ref="myAdvice">

<aop:pointcut id="myPointCut" expression="execution(* test..*Controller.*(..)) and args(req)" />

<aop:around method="returner" pointcut-ref="myPointCut" />

</aop:aspect>

<aop:aspect ref="myAdvice">

<aop:pointcut id="myPointCut2" 

expression="execution(* test..*(javax.servlet.http.HttpServletRequest, String)) and args(req, ctg)" />

<aop:around method="returner3" pointcut-ref="myPointCut2" />

</aop:aspect>

</aop:config>

<aop:aspect>태그의 ref는 참조할 advice bean id

<aop:pointcut> aspect적용 지점
     expression : 적용할 메서드 경로
execution은 메서드 전체 형식으로 지정한다.
접근제한자 반환타입 패키지를 포함한 메서드명 (인자)
<aop:around> method는 aspect로 동작할 메서드
   pointcut-ref는 참조할 pointcut id


HttpServletRequest를 받아서 URI를 받아 쓰고 싶은 경우가 생길 것이다.

파라메터를 받아 쓰고 싶은 경우는

execution에서 지정한 메서드 인자를 (..) 대신 타입을 패키지를 포함해서 써주고 args()도 추가해 주어야 한다.

(javax.servlet.http.HttpServletRequest, String) and args(req, ctg)

args()의 인자는 변수명을 쓴다.


+ Recent posts