2013-02-05 43 views
7

Tôi đang cố chặn bất kỳ phương thức nào được gắn thẻ w/chú thích tùy chỉnh và lý do bạn đọc này là vì tôi không thể làm cho nó hoạt động. Tôi đã làm theo các ví dụ đơn giản nhưng không thể làm cho nó hoạt động được.Mùa xuân 3.2 AOP - Phương pháp chặn bằng chú thích

Đây là mã của tôi.

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(); 
    } 
} 

mùa xuân-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; 
    } 
} 

mã kiểm tra:

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

Là một lưu ý phụ tôi đã cố gắng để xác định pointcut nhiều cách khác nhau của tôi, tất cả có và không có việc sử dụng các anyPublic() phương pháp và pointcut thực hiện của nó, nhưng không ai trong số những người làm việc cho tôi:

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

  2. @Around(value="@annotation(myAnnotation)", argNames="myAnnotation") public Object process(ProceedingJoinPoint jointPoint, MyAnnotation myAnnotation) throws Throwable { .. }

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

Tôi đang làm gì sai?

+1

Bạn đang gọi 'mới', trừ khi bạn cụ thể hóa bytecode để cho phép instantiation qua 'new' thay vì trải qua ngữ cảnh ứng dụng hoặc dệt tại thời gian biên dịch, đối tượng instantiated hoàn toàn không biết gì về Spring- hoặc AOP liên quan. –

+0

Cảm ơn Dave! Tôi không thể tin rằng tôi lại rơi vào điều đó. Đôi khi tất cả chúng ta cần một cặp mắt khác để làm nổi bật điều hiển nhiên. – Lancelot

+0

Câu hỏi/câu trả lời này đã giúp chúng tôi rất nhiều! Ngoài ra, lưu ý rằng 'myAnnotation' trong @annotation (myAnnotation) thực sự bắt đầu bằng chữ thường để khớp với tên tham số trong quá trình khai báo. Chúng tôi đã dành một loạt thời gian nhìn vào "thông số chính thức không được chỉ định" trước khi nhận thấy điều đó. –

Trả lời

8

Trong mã thử nghiệm, bạn không cho phép Spring tạo MyComponent, nhưng thay vào đó, bạn đang sử dụng toán tử new. Bạn nên truy cập MyComponent từ Spring's ApplicationContext.

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... 
    } 
} 

Nếu bạn không nhận được thành phần của mình từ Spring, bạn mong đợi nó như thế nào?

Các vấn đề liên quan