gpt4 book ai didi

java - 将注释对象绑定(bind)到通知主体

转载 作者:行者123 更新时间:2023-11-30 06:20:42 24 4
gpt4 key购买 nike

我能够使用@annotation 切入点来满足我的基本需求。

@Pointcut ("@annotation(path.to.my.CustomAnnotation)")
public void actionAnnotatedPointCut() {}

但是当我尝试将它绑定(bind)到通知主体时,如下所示,我得到了 IllegalArgumentException

@Pointcut ("@annotation(customAnnotation)")
public void actionAnnotatedPointCut(CustomAnnotation customAnnotation) {}

异常:

Caused by: java.lang.IllegalArgumentException: error at ::0 incompatible number of arguments to pointcut, expected 1 found 0

完整跟踪:

Jan 29, 2014 9:45:29 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Jan 29, 2014 9:45:29 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 incompatible number of arguments to pointcut, expected 1 found 0
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3934)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4429)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:526)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:850)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:724)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:493)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1274)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:296)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalArgumentException: error at ::0 incompatible number of arguments to pointcut, expected 1 found 0
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:211)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:197)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:186)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:166)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:208)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262)
at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:372)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:335)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:421)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1558)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
... 27 more
Jan 29, 2014 9:45:29 PM org.apache.catalina.core.StandardContext start
SEVERE: Error listenerStart
Jan 29, 2014 9:45:29 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [/myApp] startup failed due to previous errors

在这种情况下我是否不恰本地使用了切入点?

最佳答案

似乎错误在于您尝试组合切入点并引用 actionAnnotatedPointCut

例如,你可能有过

@Pointcut ("@annotation(path.to.my.CustomAnnotation)")
public void actionAnnotatedPointCut() {}

@Around("actionAnnotatedPointCut()")
public Object doAround(ProceedingJoinPoint pjp) {
...
}

这会很好用。

如果您将切入点更改为

@Pointcut ("@annotation(customAnnotation)")
public void actionAnnotatedPointCut(CustomAnnotation customAnnotation) {}

@annotation(..) 中的表达式通过名称引用方法中的参数。该参数的类型将用于建议使用该类型注释的方法。

现在,结合此切入点的切入点必须在其对 @Pointcut 方法的引用中具有相同的参数名称,并且必须具有适当类型的方法参数。

@Around("actionAnnotatedPointCut(customAnnotation)")
public Object doAround(ProceedingJoinPoint pjp, CustomAnnotation customAnnotation) {
...
}

这就是错误的意思

incompatible number of arguments to pointcut, expected 1 found 0

它需要一个参数,在我们的例子中是 customAnnotation,但它丢失了。

关于java - 将注释对象绑定(bind)到通知主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21443191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com