2010-01-18 4 views
1

AOP 조언을 사용하여 주석 처리 된 방법으로 콩을 프록시하는 방법을 알아 내려고했습니다. 봄 3 AOP anotated 권고

나는 내가

@Target({ ElementType.METHOD, ElementType.TYPE }) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface MonitorTimer { 
} 

을 실행 시간을 감시하고 지금

public class MonitorTimerAdvice implements MethodInterceptor { 
    public Object invoke(MethodInvocation invocation) throws Throwable{ 
     try { 
      long start = System.currentTimeMillis(); 
      Object retVal = invocation.proceed(); 
      long end = System.currentTimeMillis(); 
      long differenceMs = end - start; 
      System.out.println("\ncall took " + differenceMs + " ms "); 
      return retVal; 
     } catch(Throwable t){ 
      System.out.println("\nerror occured"); 
      throw t; 
     } 
    } 
} 

내가 사용할 수있는 몇 가지 가짜 모니터링을 할 조언에 대한 사용자 정의 주석을 만들었습니다

@Service 
public class RestSampleDao { 

    @MonitorTimer 
    public Collection<User> getUsers(){ 
       .... 
     return users; 
    } 
} 

간단한 클래스를 가지고 만약 내가 수동 으로이 같은 DAO의 인스턴스를 프록시

AnnotationMatchingPointcut pc = new AnnotationMatchingPointcut(null, MonitorTimer.class); 
    Advisor advisor = new DefaultPointcutAdvisor(pc, new MonitorTimerAdvice()); 

    ProxyFactory pf = new ProxyFactory(); 
    pf.setTarget(sampleDao); 
    pf.addAdvisor(advisor); 

    RestSampleDao proxy = (RestSampleDao) pf.getProxy(); 
    mv.addObject(proxy.getUsers()); 

내 맞춤 주석 메소드가이 인터셉터에 의해 자동으로 프록시되도록 스프링에서 어떻게 설정해야합니까? 나는 진짜 대신에 프록시 된 samepleDao를 주입하고 싶다. xml 구성없이이 작업을 수행 할 수 있습니까?

나는 그냥 가로 채기를 원하는 메소드에 주석을다는 것이 가능해야하고 스프링 DI가 필요한 것을 프록시 처리해야한다고 생각합니다.

아니면 aspectj를 사용해야합니까? 가장 간단한 해결책을 선호합니다 : -)

도움을 많이 주셔서 감사합니다!

답변

3

(7.2 @AspectJ support 참조) 당신은 AspectJ를를 사용하지 않은,하지만 당신은 봄과 AspectJ의 주석을 사용할 수 있습니다 문제를 해결

@Aspect 
public class AroundExample { 
    @Around("@annotation(...)") 
    public Object invoke(ProceedingJoinPoint pjp) throws Throwable { 
     ... 
    } 
} 
+0

감사 : -) – Art79