本文主要是介绍Spring AOP AspectJ方式配置含有参数切点及通知时报错,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
错误信息
在学习Spring AOP AspectJ方式配置含有参数切点及通知时,切点没有参数时一切正常,加上参数后总是报错,详见下面这一大串。
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#2': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: msg [Xlint:invalidAbsoluteTypeName]at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:275)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1139)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)at com.xs.test.Test.main(Test.java:9)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: msg [Xlint:invalidAbsoluteTypeName]at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:122)at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:267)... 14 more
Caused by: java.lang.IllegalArgumentException: warning no match for this type name: msg [Xlint:invalidAbsoluteTypeName]at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301)at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:206)at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:192)at org.springframework.aop.aspectj.AspectJExpressionPointcut.getMethodMatcher(AspectJExpressionPointcut.java:175)at org.springframework.aop.aspectj.AbstractAspectJAdvice.buildSafePointcut(AbstractAspectJAdvice.java:187)at org.springframework.aop.aspectj.AspectJPointcutAdvisor.<init>(AspectJPointcutAdvisor.java:51)at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)at java.lang.reflect.Constructor.newInstance(Constructor.java:423)at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)... 16 more
xml相关配置如下
<aop:config><aop:aspect ref="myadvice"><aop:pointcut expression="execution(* demo.Demo.demo01(String,String)) and args(msg,msg2) " id="mypoint"/><aop:before method="mybefore1" pointcut-ref="mypoint" arg-names="msg,msg2"></aop:before><aop:after method="myafter" pointcut-ref="mypoint" ></aop:after></aop:aspect></aop:config><bean id="demo" class="demo.Demo"></bean><bean id="myadvice" class="com.xs.advice.MyAdvice"></bean>
解决方案
偶然一次尝试将 <aop:after method="myafter" pointcut-ref="mypoint" ></aop:after>
注释,发现竟然成功运行,意识到该错误信息问题所在——切点有参数,对应的 所有 通知方法也必须带有对应参数
public void mybefore1(String msg,String msg2){System.out.println("1:"+msg);System.out.println("2"+msg2);}public void myafter(String msg,String msg2){System.out.println("after");}
于是在后置通知方法中加入对应名称形参,解决了问题。
你可以不使用切点的参数但必须为通知方法配置对应形参。
这篇关于Spring AOP AspectJ方式配置含有参数切点及通知时报错的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!