2016-09-23 15 views
4

컨트롤러, 서비스 및 DAO 계층에 대한 감사를 수행하고 있습니다. 나는 컨트롤러, 서비스 및 Dao에 대해 각각 3 개의 Around aspect 기능을 가지고있다. Controller 메소드에 있으면 Around aspect 함수를 호출하는 커스텀 어노테이션을 사용한다. 주석 내에서 Controller Around 함수에서 Aspect 클래스 내부의 Service around 함수로 전달하고자하는 속성을 설정했습니다.두 개의 Around 함수 사이에 객체 전달 - AOP

public @interface Audit{ 
    String getType(); 
} 

인터페이스에서이 getType 값을 설정합니다.

@Around("execution(* com.abc.controller..*.*(..)) && @annotation(audit)") 
public Object controllerAround(ProceedingJoinPoint pjp, Audit audit){ 
    //read value from getType property of Audit annotation and pass it to service around function 
} 

@Around("execution(* com.abc.service..*.*(..))") 
public Object serviceAround(ProceedingJoinPoint pjp){ 
    // receive the getType property from Audit annotation and execute business logic 
} 

어떻게 두 개의 Around 함수간에 객체를 전달할 수 있습니까?

답변

4

기본적으로 애스펙트는 싱글 톤 객체입니다. 그러나 다른 인스턴스화 모델이 있으므로 사용 사례와 같이 유용 할 수 있습니다. percflow(pointcut) 인스턴스 모델을 사용하면 컨트롤러에 주석 값을 저장하고 조언을 중심으로 서비스에서 검색 할 수 있습니다. 다음은 그냥 같을 것이다 방법에 대한 예입니다 : 대답은 정확

@Aspect("percflow(controllerPointcut())") 
public class Aspect39653654 { 

    private Audit currentAuditValue; 

    @Pointcut("execution(* com.abc.controller..*.*(..))") 
    private void controllerPointcut() {} 

    @Around("controllerPointcut() && @annotation(audit)") 
    public Object controllerAround(ProceedingJoinPoint pjp, Audit audit) throws Throwable { 
     Audit previousAuditValue = this.currentAuditValue; 
     this.currentAuditValue = audit; 
     try { 
      return pjp.proceed(); 
     } finally { 
      this.currentAuditValue = previousAuditValue; 
     } 
    } 

    @Around("execution(* com.abc.service..*.*(..))") 
    public Object serviceAround(ProceedingJoinPoint pjp) throws Throwable { 
     System.out.println("current audit value=" + currentAuditValue); 
     return pjp.proceed(); 
    } 

} 
+0

하고 [웜홀 패턴]의 변형을 설명 (http://stackoverflow.com/a/12130175/1082681). 'percflow()'인스턴스화가 아닌'cflow()'포인트 컷을 사용하여 싱글 톤 aspect를 구현하는 방법을 알고 싶다면 링크를 확인하십시오. 패턴은 동일하게 유지되지만 aspect 인스턴스 수가 적을 것이다. – kriegaex

+0

@ Nandor Elod Fekete - 고맙습니다. 새로운 것을 배웠고 당신의 제안은 매력처럼 작동했습니다. –

관련 문제