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");
}
问题是我的春季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
代替
如 frant.hartm 所指出的, -
JsonSchemaAnnotation jsonSchemaAnnotation
参数应出现在您所用的validateJson
方法中.
- 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屋!
查看全文