2014-11-06 2 views
1

AOP 조인 포인트에서 주석을 검색하고 싶습니다. 리플렉션을 통해 주석을 가져올 수 있지만 ProceedingJoinPoint에서는 주석을 얻을 수 없습니다.ProceedingJoinPoint를 통해 주석을 검색 할 수 없습니다.

메서드에 프로필 주석이없는 경우에는 조언이 호출되지 않지만 조언 내에서 주석을 찾을 수 없습니다. 반사 직접

public static void main(String[] args) throws NoSuchMethodException, SecurityException { 
    Method method = SampleServiceImpl.class.getMethod("getTicket", String.class); 
    System.out.println(method.getClass().toString()); 
    System.out.println(method.getAnnotation(Profile.class)); 
} 

내 주석으로 ProceedingJoinPoint으로

@Around("@annotation(com.annotation.Profile)") 
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { 
    Signature signature = joinPoint.getSignature(); 
    String methodName = signature.toString(); 
    Method method = ((MethodSignature) signature).getMethod(); 

    Profile profile = method.getAnnotation(Profile.class); 

    // profile is null here... 
    // ...... 

} 

@Documented 
@Retention(value = RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface Profile { 
    public long skipCount(); 
    public long sampleSize(); 
} 

내 주석 방법

@Override 
@Cacheable("test") 
@Transactional 
@Profile(skipCount = 100, sampleSize = 100) 
public Tickets getTicket(String ticketId) { 
    return sampleDao.getTicket(ticketId); 
} 

내 POM

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <jdk.version>1.8</jdk.version> 
    <spring.version>4.1.1.RELEASE</spring.version> 
    <aspectj.version>1.8.3</aspectj.version> 
</properties> 

<!-- Spring AOP + AspectJ --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>${aspectj.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjweaver</artifactId> 
      <version>${aspectj.version}</version> 
     </dependency> 

답변

2

반사에 대한 필요가 없습니다, 그것은 훨씬 더 쉽습니다. 당신은 조언 매개 변수에 주석을 결합 할 수 있습니다

execution(void de.scrum_master.app.Application.main(String[])) - @de.scrum_master.app.Profile(skipCount=5, sampleSize=11) 
+0

감사 :

@Around("@annotation(profileAnnotation)") public Object logAround(Profile profileAnnotation, ProceedingJoinPoint thisJoinPoint) throws Throwable { System.out.println(thisJoinPoint + " - " + profileAnnotation); // (...) Object result = thisJoinPoint.proceed(); // (...) return result; } 

콘솔 출력은 다음과 같이 될 수 있습니다. GCLIB를 켜면 이전에 표시된 코드에서 주석을 가져올 수 있습니다. GCLIB를 사용하기위한 단점이 있는지 알 수 있습니까? –

+0

Java 동적 프록시와 비교하여 의미가 있습니까? 일반적으로 Java 프록시는 인터페이스에서만 작동하며, CGLIB는 인터페이스없이 프록시 할 필요가있는 클래스에서도 작동합니다. 그렇지 않으면 나는 프록시를 전혀 필요로하지 않는 네이티브 AspectJ만을 사용하기 때문에 두 가지 중 어느 것에 대해서도 실용적인 경험이별로 없다. Spring AOP는 프록시 기반 "AOP lite"이며 제 선택이 아닙니다. – kriegaex

+0

메소드 인수의 순서가 바뀌어야합니다. 제공된 코드에 대한 수정안을 작성했습니다. –

관련 문제