2016-11-17 3 views
0

내 봄 부팅 응용 프로그램의 컨트롤러 클래스에 전화를 감사 클래스를 만드는 오전 : 내 컨트롤러 클래스의스프링 AOP와 RequestMapping 주석

@Aspect 
@Component 
public class ServiceAudit { //RequestMappingInterceptor { 
@Pointcut("@annotation(requestMapping) && execution(* *(..))") 
public void controller(RequestMapping requestMapping) {} 

@Before("controller(requestMapping)") 
public void advice(JoinPoint thisJoinPoint, RequestMapping requestMapping) { 
    String url = requestMapping.value()[0]; 
    String httpMethod = requestMapping.method()[0].toString(); 
... 
... 

하나는 다음과 같다 - 클래스와 메소드 레벨에서 주석으로 (이 나는이 시점에서 바꿀 수 없다) :

나는 methodtype 매개 변수를 잘 가져올 수있다. 그러나, 나는 매우 힘든 시간을 보내고 컨트롤러 테이블 '주석 ('/ 응용 프로그램 ') 당겨 그래서 내 감사 테이블에 대한 전체 URL을 작성할 수 있습니다.

감사를위한 다른 옵션 (예 : Spring Boot Actuator)이 있지만 여러 가지 이유로이 접근법을 사용해야하고 여기에 막혀 있습니다. Spring의 AnnotationUtils는 도움이 될 것 같지만 클래스 레벨 주석을 얻기 위해 코드에 붙어 있습니다. 아무도 이것을 한 적이 없습니까?

업데이트 : 감사합니다. 그것은 작동하지 않았다. 당신 말이 맞아요. 그것은 불필요한 부분이었고 '실행'부분을 제거했지만 여전히 같은 문제였습니다. 본질적으로이 문제는 클래스 레벨 주석을 얻는 방법입니다. 그래서, 내 ApplicationsController 클래스에 대해 클래스 수준에서 정의 된 '/ applications'경로를 검색하는 방법은 무엇입니까? 나는 거기에 있다고 생각하지만 requestMapping 안에 너무 깊게 중첩되어있어서 추출하는 방법을 알 수 없다. 지금은 유일한 솔루션은 내 컨트롤러 (ApplicationsController -> '/ applications') 용 HashMap을 만든 다음 methodInvocation.targetClass를 해당 맵의 키로 사용하는 것입니다. 컨트롤러를 추가 할 때 매우 우아하고 유지 보수가 필요합니다.

+0

시도, 실행 (* * (..)) 지점에서 – kuhajeyan

+0

감사를 잘라 제거. 그것은 작동하지 않았다. 당신 말이 맞아요. 그건 불필요한 부분이었고 그 부분을 제거했지만 여전히 같은 문제였습니다. – Mike

답변

0

이 같은 클래스 레벨의 주석을 캡처 할 수 있습니다 :

@Pointcut("@target(classRequestMapping) && @annotation(requestMapping) && execution(* *(..))") 
public void controller(RequestMapping classRequestMapping, RequestMapping requestMapping) {} 

@Before("controller(classRequestMapping, requestMapping)") 
public void advice(JoinPoint thisJoinPoint, RequestMapping classRequestMapping, RequestMapping requestMapping) { 
    // do whatever you want with the value 
} 
+0

감사합니다. 도움이되었습니다. – Mike

관련 문제