Spring AOP切入点不触发 [英] Spring AOP Pointcut does not trigger
本文介绍了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?
推荐答案
您的代码有几个问题:
- 您应该将
ValidationTest
对象创建为 Spring 管理的 bean,而不是使用 new <aop:include name="UpdateUIMetadataInterceptor"/>
应该是<aop:include name="updateUI"/>
;为了简单起见,您实际上可以在这里使用<aop:aspectj-autoproxy/>
ProceedingJoinPoint
不支持之前的方面,所以删除它;如果需要访问参数,可以改用JoinPoint
JsonSchemaAnnotation jsonSchemaAnnotation
参数应该存在于您的方面的validateJson
方法中,正如 frant.hartm
- You should create your
ValidationTest
object as a bean managed by Spring and not using new <aop:include name="UpdateUIMetadataInterceptor" />
should be<aop:include name="updateUI"/>
; you can actually just stick with<aop:aspectj-autoproxy/>
for simplicity hereProceedingJoinPoint
is not supported for before aspects, so remove it; you can useJoinPoint
instead if you need access to argumentsJsonSchemaAnnotation jsonSchemaAnnotation
parameter should be present forvalidateJson
method of your aspect, as pointed out by frant.hartm
这篇关于Spring AOP切入点不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文