配置aop的xml形式,报错
错误信息:
1 Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'calcImpl' defined in class path resource [application_Aop_ByXML.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': 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: ssm.example.beAop_ByXML.impl [Xlint:invalidAbsoluteTypeName] 2 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:512) 3 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) 4 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 5 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) 6 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 7 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) 8 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) 9 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) 10 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) 11 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85) 12 at ssm.example.beAop_ByXML.test.TestAop_Order.main(TestAop_Order.java:14) 13 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': 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: ssm.example.beAop_ByXML.impl [Xlint:invalidAbsoluteTypeName] 14 at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:314) 15 at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:295) 16 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358) 17 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204) 18 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) 19 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) 20 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) 21 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 22 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) 23 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) 24 at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:91) 25 at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:109) 26 at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:101) 27 at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:251) 28 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:1141) 29 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:1114) 30 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:506) 31 ... 10 more 32 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: ssm.example.beAop_ByXML.impl [Xlint:invalidAbsoluteTypeName] 33 at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:217) 34 at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) 35 at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:310) 36 ... 26 more 37 Caused by: java.lang.IllegalArgumentException: warning no match for this type name: ssm.example.beAop_ByXML.impl [Xlint:invalidAbsoluteTypeName] 38 at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:302) 39 at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:227) 40 at org.springframework.aop.aspectj.AspectJExpressionPointcut.obtainPointcutExpression(AspectJExpressionPointcut.java:198) 41 at org.springframework.aop.aspectj.AspectJExpressionPointcut.getMethodMatcher(AspectJExpressionPointcut.java:183) 42 at org.springframework.aop.aspectj.AbstractAspectJAdvice.buildSafePointcut(AbstractAspectJAdvice.java:202) 43 at org.springframework.aop.aspectj.AspectJPointcutAdvisor.<init>(AspectJPointcutAdvisor.java:52) 44 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 45 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 46 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 47 at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 48 at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:204) 49 ... 28 more
xml配置源码:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 类似于@Component--> <!-- 装配bean--> <bean id="calcImpl" class="ssm.example.beAop_ByXML.impl.CalcImpl" /> <!-- 装配bean的切面部分--> <bean id="myLogging" class="ssm.example.beAop_ByXML.aop.MyLogging" /> <bean id="calcImpl_Verify" class="ssm.example.beAop_ByXML.aop.CalcImpl_Verify" /> <!-- 类似于@Aspect ①定义切入点表达式 ②定义切面 ③定义通知 --> <aop:config> <!-- 定义切入点表达式 注意,如果此处的表达式,写错了,编译不会报错,运行时,会出现--> <aop:pointcut id="myPoint" expression="execution(* ssm.example.beAop_ByXML.impl.*.*(..))"/> <!-- 定义切面--> <aop:aspect id="myLogging" ref="myLogging" order="0" > <!-- 前置通知--> <aop:before method="methodBefore" pointcut-ref="myPoint" /> <!-- 后置通知--> <aop:after method="methodAfter" pointcut-ref="myPoint"/> <!-- 返回通知--> <aop:after-returning method="methodAfterReturning" pointcut-ref="myPoint" returning="rs"/> <!-- 异常通知--> <aop:after-throwing method="methodAfterThrowing" pointcut-ref="myPoint" throwing="ex"/> <!-- 环绕通知--> <aop:around method="aroundMethod" pointcut-ref="myPoint"/> </aop:aspect> <aop:aspect id="calcImpl_Verify" ref="calcImpl_Verify" order="1"> <!-- 前置通知--> <aop:before method="methodBeforeV" pointcut="myPoint"/> </aop:aspect> </aop:config> </beans>
原因分析:
可以看到错误信息第一行,其实已经说明了
①Error creating bean with name 'calcImpl' defined in class path resource [application_Aop_ByXML.xml]
通过xml配置文件和byname方式calcImpl 没创建成功
②BeanPostProcessor before instantiation of bean failed
bean实例初始化之前BeanPostProcessor 失败了
③nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Bean instantiation via constructor failed;
嵌套的异常 是实例创建异常:在创建AspectJ的切入点Advisor时:通过构造函数实例化Bean失败;
④ nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.aop.aspectj.AspectJPointcutAdvisor]:
嵌套的异常 是实例初始化的异常:AspectJPointcutAdvisor初始化失败
⑤ Constructor threw exception; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: ssm.example.beAop_ByXML.impl [Xlint:invalidAbsoluteTypeName]
构造器抛出的异常:嵌套的异常是 不合法的参数异常:警告 没有匹配的类型名字:ssm.example.beAop_ByXML.impl[Xlint:绝对类型名无效]
……
通过这些分析,也就是发生在嵌套异常这一块,并且是在关于创建切入点的时候发生的异常-->绝对类型名无效
发现xml配置文件中,共出现4次绝对类型名:最终发现在最后一处的绝对类型名处,少加了.*
<aop:pointcut id="myPoint" expression="execution(* ssm.example.beAop_ByXML.impl.*.*(..))"/>
<!--
第一个*:表示匹配任何限定修饰符 和 任何返回值类型
第二个*:表示ssm.example.beAop_ByXML.impl包下的所有类
第三个*:表示这些类写的所有方法
括号里的..:表示匹配所有方法
-->
解决:
加上就好了
反思:
日志信息很全,多看看