2013-02-05 82 views
7

我试图拦截任何标记为w /自定义注释的方法,并且您读取这个的原因是因为我无法使其工作。我一直在遵循简单的例子,但不能让它工作。Spring 3.2 AOP - 通过注释拦截方法

这是我的代码。

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

弹簧-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; 
    } 
} 

测试代码:

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

作为一个方面说明我试图定义我的切入点很多不同的方式,所有使用和不使用anyPublic的()方法及其执行的切入点,但没有那些工作对我来说:

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

我在做什么错?

+1

你在调用'new',除非你特别指定字节码来允许通过'new'来实例化,而不是通过应用上下文,或者在编译时编织,实例化的对象完全不了解任何东西。Spring-或AOP相关。 –

+0

谢谢Dave就是这样!我不敢相信我再次为此而堕落。有时我们都需要另一双眼睛来突出显而易见的。 – Lancelot

+0

这个问题刚刚帮了我们大忙!另外,请注意@annotation(myAnnotation)中的'myAnnotation'确实以小写开头,以匹配进程声明中的参数名称。在注意到之前,我们花了很多时间看“未指定的形式参数”。 –

回答

8

在您的测试代码中,您不允许Spring创建MyComponent,而是使用new运算符。您应该访问Spring的ApplicationContext中的MyComponent

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

如果你没有从Spring那里得到你的组件,你如何期望它被代理?