前面练习正常,可执行
来到编程事务管理,注入事务管理器以及事务模板后一直提示
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'transactionTemplate' defined in class path resource [applicationContext.xml]: Error
setting property values; nested exception is
org.springframework.beans.NotWritablePropertyException: Invalid property
'dataSourceTransactionManager' of bean class [org.springframework.transaction.support.TransactionTemplate]: Bean property
'dataSourceTransactionManager' is not writable or has an invalid setter method. Does the
parameter type of the setter match the return type of the getter?
因是参照老师课堂练习,所以很疑惑
<!-- 配置事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务模板 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
看到这个https://blog.youkuaiyun.com/naughty610/article/details/5619092之后,考虑可能是事务模板注入事务管理器时名称不正确导致,于是翻看源码
/**
* Set the transaction management strategy to be used.
*/
public void setTransactionManager(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
也就是说注入名称其实是固定的 transactionManager,更改后测试通过。
因为前面随便就简写一下,老师也没有明确这一点才陷入这个坑中,究其原因是对于bean的原理理解不够