Configuration problem: Cannot locate BeanDefinitionDecorator for element [config] 或 [advice]
我今天在网上学搭框架,我在spring.xml配置的时候,我不是直接复制的,我是手把手敲了一遍,当然会有小错误。我把这个错误也记录一下。
其实很简单,你仔细对比你自己亲手敲得和别人测试通过的。你会发现很多地方不是位置放错了,就是字符错了。
不说多了,标题的错误已经告诉我们答案了。我先把错误配置代码贴出来:
1 <bean id="transactionManager" 2 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 3 <property name="dataSource" ref="dataSource" /> 4 这里的</bean>去哪里了? 5 <aop:config> 6 <aop:pointcut id="transactionPointcut" 7 expression="execution(* com.zou.test.service..*Impl.*(..))" /> 8 <aop:advisor pointcut-ref="transactionPointcut" 9 advice-ref="transactionAdvice" /> 10 </aop:config> 11 12 <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> 13 <tx:attributes> 14 <tx:method name="add*" propagation="REQUIRED" /> 15 <tx:method name="append*" propagation="REQUIRED" /> 16 <tx:method name="insert*" propagation="REQUIRED" /> 17 <tx:method name="save*" propagation="REQUIRED" /> 18 <tx:method name="update*" propagation="REQUIRED" /> 19 <tx:method name="modify*" propagation="REQUIRED" /> 20 <tx:method name="edit*" propagation="REQUIRED" /> 21 <tx:method name="delete*" propagation="REQUIRED" /> 22 <tx:method name="remove*" propagation="REQUIRED" /> 23 <tx:method name="repair" propagation="REQUIRED" /> 24 <tx:method name="delAndRepair" propagation="REQUIRED" /> 25 26 27 <tx:method name="get*" propagation="SUPPORTS" /> 28 <tx:method name="find*" propagation="SUPPORTS" /> 29 <tx:method name="load*" propagation="SUPPORTS" /> 30 <tx:method name="search*" propagation="SUPPORTS" /> 31 <tx:method name="datagrid*" propagation="SUPPORTS" /> 32 33 34 <tx:method name="*" propagation="SUPPORTS" /> 35 </tx:attributes> 36 </tx:advice> 37 38 </bean> 原来跑这里来了
不知道大家看出什么原因没有,似乎都很正确,没毛病。
说重点,我说过不是位置错了就是字符错了。
其实就是我方的位置错误了,我不小心写<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">这个的时候忘记及时把结束</bean>加上,然后等我把接下来的
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(* com.zou.test.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="delAndRepair" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
这些代码写完才加上这个</bean>,当然这样也没有报错,所以我没注意,等启动时候报错了,我当时网上疯查,没找到我想要的。后来我就把原版的复制过来,启动就没报错了,我于是不服,我就是要弄个明白,后来这样仔细一对比,才发现我的位置放得不对。要把中间那些代码从<aop:config>开始放到<bean>外面,不要包在里面。希望我这个低级错误能帮到你们。
正确配置代码:
1 <bean id="transactionManager" 2 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 3 <property name="dataSource" ref="dataSource" /> 4 </bean>---结束的</bean> 之前错误的原因</bean>在最下面。 5 <aop:config> 6 <aop:pointcut id="transactionPointcut" 7 expression="execution(* com.zou.test.service..*Impl.*(..))" /> 8 <aop:advisor pointcut-ref="transactionPointcut" 9 advice-ref="transactionAdvice" /> 10 </aop:config> 11 12 <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> 13 <tx:attributes> 14 <tx:method name="add*" propagation="REQUIRED" /> 15 <tx:method name="append*" propagation="REQUIRED" /> 16 <tx:method name="insert*" propagation="REQUIRED" /> 17 <tx:method name="save*" propagation="REQUIRED" /> 18 <tx:method name="update*" propagation="REQUIRED" /> 19 <tx:method name="modify*" propagation="REQUIRED" /> 20 <tx:method name="edit*" propagation="REQUIRED" /> 21 <tx:method name="delete*" propagation="REQUIRED" /> 22 <tx:method name="remove*" propagation="REQUIRED" /> 23 <tx:method name="repair" propagation="REQUIRED" /> 24 <tx:method name="delAndRepair" propagation="REQUIRED" /> 25 26 <tx:method name="get*" propagation="SUPPORTS" /> 27 <tx:method name="find*" propagation="SUPPORTS" /> 28 <tx:method name="load*" propagation="SUPPORTS" /> 29 <tx:method name="search*" propagation="SUPPORTS" /> 30 <tx:method name="datagrid*" propagation="SUPPORTS" /> 31 32 <tx:method name="*" propagation="SUPPORTS" /> 33 </tx:attributes> 34 </tx:advice>