Cannot locate BeanDefinitionDecorator for element [config]错误

本文详细解析了在配置Spring AOP时常见的错误,包括id大小写问题、aop:config标签位置不当以及切点配置冲突等,并提供了正确的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是在配置springAOP时候出的错,这个问题是在配置文件的时候

请看我的错误代码:


<bean id="AccountService" class="com.itheima.Service.Impl.AccountServiceImpl"></bean>
<!--配置logger类 -->
<bean id="Logger" class="com.itheima.logger.Logger">
    <aop:config>
 <!--配置切点-->
        <aop:pointcut id="pt" expression="execution(* com.itheima.Service.Impl.*.*(..))"></aop:pointcut>
         <!--配置切面-->
        <aop:aspect id="adviceLog" ref="Logger">
            <!--添加前置通知  后置通知  异常通知   最终通知-->
            <aop:before method="beforePrintLog" pointcut-ref="pt" pointcut="execution(* com.itheima.Service.Impl.*.*(..))"></aop:before>
            <aop:after-returning method="afterReturnPrintLog" pointcut-ref="pt" pointcut="execution(* com.itheima.Service.Impl.*.*(..))"></aop:after-returning>
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt" pointcut="execution(* com.itheima.Service.Impl.*.*(..))"></aop:after-throwing>
            <aop:after method="afterPrintLog" pointcut-ref="pt" pointcut="execution(* com.itheima.Service.Impl.*.*(..))"></aop:after>
        </aop:aspect>
    </aop:config>
</bean>

绿色错误1:此时id应该是小写,id="accountService"

红色错误2:<aop:config>标签应该是单独在外面,我把config标签放在了<bean>标签内部

蓝色错误3:已经在切面外面配置了切点id=“pt”,并且在切面内部引用pointcut=“pt” ,但是在切面内部有=又写了配置切点语句,冲突了。

下面是改正后的代码

<bean id="accountService" class="com.itheima.Service.Impl.AccountServiceImpl"></bean>
<!--配置logger类 -->
<bean id="Logger" class="com.itheima.logger.Logger"> </bean>
    <aop:config>
        <!--配置切点-->
        <aop:pointcut id="pt" expression="execution(* com.itheima.Service.Impl.*.*(..))"></aop:pointcut>
        <!--配置切面-->
        <aop:aspect id="adviceLog" ref="Logger">
            <aop:before method="beforePrintLog" pointcut-ref="pt" ></aop:before>
            <aop:after-returning method="afterReturnPrintLog" pointcut-ref="pt" ></aop:after-returning>
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt"></aop:after-throwing>
            <aop:after method="afterPrintLog" pointcut-ref="pt" ></aop:after>
        </aop:aspect>
    </aop:config>

添加知识点:有的时候我们把切点加在切面里面程序运行会报错,原因是我们的xml文件的约束中规定了切点必须写在切面前面,这个时候我们就必须按照约束的规定写语句。

错误总结:不细心,知识点记得不牢靠,要多练习多记忆

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值