2016-04-02 15 views
0

Ben sping3 bazı aop kod yazıyorum. İşte benim ek açıklama.NedenAnnotation veya getAnnotation öğesi null değerini döndürüyor?

@Target({ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface DataSource { 
    String name() default "foo" 
} 

Ve yukarıdaki ek nota bir pointcut ayarlayın.

<aop:pointcut id="service" expression="@annotation(com.foo.datasource.DataSource)" /> 
<aop:advisor advice-ref="dataSourceExchange" pointcut-ref="service" order="1"/> 
<bean id="dataSourceExchange" class="com.foo.datasource.DataSourceExchange"/> 

Bir hizmet yöntemi yazıp yukarıdaki açıklamayı ekledim. Hizmetten önce çağrılacak olan DataSourceExchange sınıfında, ek açıklama almaya çalışıyorum.

class DataSourceExchange implements MethodInterceptor { 
    @Override 
    public Object invoke(MethodInvocation invocation) throws Throwable { 
     System.out.println("Method name : " 
       + invocation.getMethod().getName()); 
     System.out.println("Method arguments : " 
       + Arrays.toString(invocation.getArguments())); 

     DataSource dataSource = AnnotationUtils.findAnnotation(invocation.getMethod(), DataSource.class); 
     System.out.println(dataSource); 

Yöntemin adını doğru şekilde alıyorum. Ama açıklama dataSource sadece null döndürür. Sorun nedir? Ben aksi takdirde pointcut tetiklemez aradım hizmet metodu açıklama-ed kesinlikle olduğunu düşünüyorum.

cevap

0

Sonunda Method realMethod = invocation.getThis().getClass().getDeclaredMethod(proxyedMethod.getName(), proxyedMethod.getParameterTypes()) kullanarak soruyu çözdü; gerçek yöntemi öğrenmek için.

Ayrıntılar için, aşağıdaki adresi ziyaret edin: Alter dataSource in Spring By AOP And Annotation

İlgili konular