Spring AOP异常:Error creating bean with name ‘org.springframework.aop.aspectj.AspectJPointcutAdvisor#0
Posted 小猪快跑22
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring AOP异常:Error creating bean with name ‘org.springframework.aop.aspectj.AspectJPointcutAdvisor#0相关的知识,希望对你有一定的参考价值。
刚开始学 Spring 的时候,我在 xml 配置文件中配置 aop 就报错了,配置如下:
<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"
xmlns:tx="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userId" class="com.example.user.User">
<property name="userName" value="pig"/>
<property name="passWord" value="123456"/>
</bean>
<bean id="userDaoImpl" class="com.example.user.UserDaoImpl"/>
<bean id="logInterceptor" class="com.example.user.LogInterceptor"/>
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointerCut" expression="execution(* com.example.user.UserDao.saveUser(..))"/>
<!-- 配置切面以及通知(指向切面,比如错误日志) -->
<aop:aspect id="logAspect" ref="logInterceptor">
<!-- method写切面中(比如错误日志)的方法名称 -->
<aop:before method="before" pointcut-ref="servicePointerCut"/>
<aop:after method="after" pointcut-ref="servicePointerCut"/>
</aop:aspect>
</aop:config>
</beans>
报错具体日志如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userId' defined in class path resource [aop.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': Cannot create inner bean '(inner bean)#704d6e83' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#704d6e83': Cannot resolve reference to bean 'servicePointerCut' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servicePointerCut': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:472)
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)
解决方法就是导入以下2个依赖,maven 依赖如下:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.9</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>
添加依赖后,找到项目的 pom.xml 然后右击 选择 maven,然后 reload project ,如下图:
最后我的项目就能正常运行了。
以上是关于Spring AOP异常:Error creating bean with name ‘org.springframework.aop.aspectj.AspectJPointcutAdvisor#0的主要内容,如果未能解决你的问题,请参考以下文章
spring AOP使用中Error creating bean with name ‘…’defined in class path resource..问题及其解决方法
运行spring aop例子时,出现下面的错误,不知如何解决,看起来又不想异常?