Spring AOP切入点不触发 [英] Spring AOP Pointcut does not trigger

查看:41
本文介绍了Spring AOP切入点不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring 和 AOP 的新手.我正在尝试这个简单的事情,我创建了一个自定义注释,当放置在任何方法应该执行一些代码之前.这是我创建的注释

I am new to Spring and AOP. I am trying this simple thing where I have created a custom annotation which when placed before any method should execute some code. This is the annotation I created

    // Declares a custom annotation that validates json
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface JsonSchemaAnnotation {
    }

接下来我创建了包含逻辑的 Spring Aspect 类

Next I created the Spring Aspect class which holds the logic

@Aspect
public class UpdateUIMetadataInterceptor {

@Pointcut("execution(public * com.fico.cardinal.cm.*.*(..))")
public void anyPublicMethod() {
    System.out.println("Running");
}

@Before("anyPublicMethod() && @annotation(jsonSchemaAnnotation)")
public void validateJson(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("Running");  
}

}

这是我的简单测试类

public class ValidationTest {

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring/configuration.xml");
    String jsondata = "{\"id\": \"EXPENSE_REPORT\",\"properties\": {\"transactionType\": \"EXPENSE_REPORT\"},\"sections\": []} ]}";
    ValidationTest test = new ValidationTest();
    test.jsonValidationTest("dummy", jsondata);
    ((AbstractApplicationContext) context).close();


}

@JsonSchemaAnnotation
public void jsonValidationTest(String dummy, String jsondata) {
    System.out.println("Success");

}

问题是我的 spring aop 永远不会被触发.我在 configuration.xml

The problem is my spring aop never gets triggered. I have included a bean in my configuration.xml

<aop:aspectj-autoproxy>
    <aop:include name="UpdateUIMetadataInterceptor" />
</aop:aspectj-autoproxy>
<bean id="updateUI"      class="com.fico.cardinal.cm.interceptor.UpdateUIMetadataInterceptor" />

谁能指出我遗漏了什么?

Can anyone point out what I am missing?

推荐答案

您的代码有几个问题:

  1. 您应该将 ValidationTest 对象创建为 Spring 管理的 bean,而不是使用 new
  2. <aop:include name="UpdateUIMetadataInterceptor"/> 应该是 <aop:include name="updateUI"/>;为了简单起见,您实际上可以在这里使用 <aop:aspectj-autoproxy/>
  3. ProceedingJoinPoint 不支持之前的方面,所以删除它;如果需要访问参数,可以改用 JoinPoint
  4. JsonSchemaAnnotation jsonSchemaAnnotation 参数应该存在于您的方面的 validateJson 方法中,正如 frant.hartm
  1. You should create your ValidationTest object as a bean managed by Spring and not using new
  2. <aop:include name="UpdateUIMetadataInterceptor" /> should be <aop:include name="updateUI"/>; you can actually just stick with <aop:aspectj-autoproxy/> for simplicity here
  3. ProceedingJoinPoint is not supported for before aspects, so remove it; you can use JoinPoint instead if you need access to arguments
  4. JsonSchemaAnnotation jsonSchemaAnnotation parameter should be present for validateJson method of your aspect, as pointed out by frant.hartm

这篇关于Spring AOP切入点不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆