Spring 3.2 AOP-注释拦截方法
我试图截取任何带有自定义注释的标记方法,您之所以阅读这篇文章,是因为我无法让它工作。我一直在遵循简单的示例,但无法使其发挥作用 这是我的密码 MyAnnotation.java:Spring 3.2 AOP-注释拦截方法,spring,annotations,aop,Spring,Annotations,Aop,我试图截取任何带有自定义注释的标记方法,您之所以阅读这篇文章,是因为我无法让它工作。我一直在遵循简单的示例,但无法使其发挥作用 这是我的密码 MyAnnotation.java: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String value() default ""; String key() default ""; Strin
@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();
}
}
<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" />
...
@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;
}
}
spring config.xml:
@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();
}
}
<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" />
...
@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()方法及其执行切入点,但这些方法都不适用于我:
@Around(@annotation(com.myapp.annotation.MyAnnotation)”)
公共对象进程(ProceedingJointPoint jointPoint)抛出可丢弃的{..}
@Around(value=“@annotation(myAnnotation)”,argNames=“myAnnotation”)
公共对象进程(ProceedingJointPoint jointPoint,MyAnnotation MyAnnotation)抛出可丢弃的{..}
@Around(“执行(*com.myapp.*.*(..)&&&&@annotation(com.myapp.annotation.MyAnnotation)”)
公共对象进程(ProceedingJointPoint jointPoint)抛出可丢弃的{..}
我做错了什么?在测试代码中,您不允许Spring创建您的
MyComponent
,而是使用新的操作符。您应该从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获取组件,您希望如何代理它?您正在调用new
,除非您专门插入字节码,以允许通过new
进行实例化,而不是通过应用程序上下文,或在编译时编织,实例化的对象完全不知道任何与Spring或AOP相关的内容。谢谢Dave,就是这样!我不敢相信我又爱上了那个。有时我们都需要另一双眼睛来突出显而易见的东西。这个问题/答案帮了我们大忙!另外,请注意@annotation(myAnnotation)中的“myAnnotation”确实以小写开头,以匹配进程声明中的参数名称。在注意到这一点之前,我们花了很多时间研究“未指定形式参数”。