这是在配置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文件的约束中规定了切点必须写在切面前面,这个时候我们就必须按照约束的规定写语句。
错误总结:不细心,知识点记得不牢靠,要多练习多记忆