練習Spring模板類,注入事務管理器以及事務模板後一直org.springframework.beans.factory.BeanCreationException

前面練習正常,可執行

來到編程事務管理,注入事務管理器以及事務模板後一直提示

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.csdn.net/naughty610/article/details/5619092之後,考慮可能是事務模板注入事務管理器時名稱不正確導致,於是翻看源碼

	/**
	 * Set the transaction management strategy to be used.
	 */
	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}

也就是說注入名稱其實是固定的  transactionManager,更改後測試通過。

因爲前面隨便就簡寫一下,老師也沒有明確這一點才陷入這個坑中,究其原因是對於bean的原理理解不夠

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章