2013-02-05 24 views
7

Özel bir ek açıklama ile etiketlenmiş herhangi bir yöntemi engellemeye çalışıyorum ve bunu okumak için nedenini kullanamam çünkü. Basit örnekleri takip ediyorum ama işe yaramayacağım.Spring 3.2 AOP - Açıklamalama yöntemleri ek açıklamalarla

İşte kodum.

MyAnnotation.java:

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface MyAnnotation { 
    String value() default ""; 
    String key() default ""; 
    String condition() default ""; 
} 

MyAspect.java:

@Aspect 
public class MyAspect { 

    @Pointcut(value="execution(public * *(..))") 
    public void anyPublicMethod() { } 

    @Around("anyPublicMethod() && @annotation(myAnnotation)") 
    public Object process(ProceedingJoinPoint jointPoint, MyAnnotation myAnnotation) throws Throwable { 
    System.out.println("In AOP process"); 
    return 5; //jointPoint.proceed(); 
    } 
} 

yay config.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:mvc="http://www.springframework.org/schema/mvc" 
      xmlns:c="http://www.springframework.org/schema/c" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xmlns:tx="http://www.springframework.org/schema/tx" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:aop="http://www.springframework.org/schema/aop" 
      xmlns:p="http://www.springframework.org/schema/p" 
      xmlns:cache="http://www.springframework.org/schema/cache" 
      xmlns:task="http://www.springframework.org/schema/task" 
      xsi:schemaLocation=" 
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.2.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
      http://www.springframework.org/schema/cache 
      http://www.springframework.org/schema/cache/spring-cache-3.2.xsd 
      http://www.springframework.org/schema/task 
      http://www.springframework.org/schema/task/spring-task-3.2.xsd"> 


    ... 

    <context:component-scan base-package="com.myapp"> 
    <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/> 
    </context:component-scan> 


    <aop:aspectj-autoproxy proxy-target-class="true" /> 
    <bean id="myAspect" class="com.myapp.annotation.MyAspect" /> 

    ... 

MyComponent.java:

@Component 
public class MyComponent { 
    @MyAnnotation(value="valtest", key="keytest", condition="contest") 
    public int add(int i, int j) { 
    System.out.println("Executing annotation.add"); 
    return i+j; 
    } 
} 

Testi kodu: Birlikte ve anyPublic kullanmadan tüm pointcut birçok farklı şekilde tanımlamak için denedim bir yan not olarak

final MyComponent m = new MyComponent(); 
assertTrue(5 == m.add(0, 1)); // Here m.add(...) always returns 1 instead of 5. 

() yöntemi ve yürütme pointcut, ama bunların hiçbiri benim için çalıştı:

  1. @Around("@annotation(com.myapp.annotation.MyAnnotation)") public Object process(ProceedingJoinPoint jointPoint) throws Throwable { .. }

  2. Yanlış yapıyorum
  3. @Around(value="@annotation(myAnnotation)", argNames="myAnnotation") public Object process(ProceedingJoinPoint jointPoint, MyAnnotation myAnnotation) throws Throwable { .. }

  4. @Around("execution(* com.myapp.*.*(..)) && @annotation(com.myapp.annotation.MyAnnotation)") public Object process(ProceedingJoinPoint jointPoint) throws Throwable { .. }

?

+1

Özel olarak, uygulama bağlamından geçmek yerine "yeni" ile veya yeni derleme zamanında dokuma işlemine izin vermek için bayt kodu çalmadığınız sürece, yeni nesneyi çağırıyorsunuz. veya AOP ile ilgili. –

+0

Teşekkürler Dave bu kadardı! Bunun için tekrar düştüğüne inanamıyorum. Bazen bariz olanı vurgulamak için hepimizin bir çift gözüne ihtiyacımız var. – Lancelot

+0

Bu soru/cevap sadece bize bir ton yardımcı oldu! Ayrıca, @annotation (myAnnotation) öğesindeki "myAnnotation" öğesinin gerçekten de, işlemin bildirgesinde parametre adıyla eşleşmesi için küçük harflerle başladığını unutmayın. Bunu fark etmeden önce "resmi parametre belirtilmemiş" bir grup zaman geçirdik. –

cevap

8

Test kodunuzda, Spring'in MyComponent numaralı ürününüzünüzü oluşturmasına izin vermiyorsunuz ancak bunun yerine, new numaralı telefonu kullanıyorsunuzdur. Spring'in numarasından MyComponent'a erişiyor olmalısınız. Eğer Spring adresinin bileşeni alamazsanız

public class SomeTest { 
    public static void main(String[] args) throws Exception { 
     final ApplicationContext appContext = new ClassPathXmlApplicationContext("spring-config.xml"); 
     final MyComponent myComponent = appContext.getBean(MyComponent.class); 
     //your test here... 
    } 
} 

, nasıl o proxy uygulanacak bekliyorsunuz?

İlgili konular