When learning to write XML transactions in spring, you can refer to ‘transactionmanager’ while setting bean property ‘transactionmanager’
Warning: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in file [D:\Codeworkstation\IDEAGuigu\Spring5\spring5_tedemo1\out\production\spring5_tedemo1\com\atgui\spring\service\UserService.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txadvice': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' available
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in file [D:\Codeworkstation\IDEAGuigu\Spring5\spring5_tedemo1\out\production\spring5_tedemo1\com\atgui\spring\service\UserService.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txadvice': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' available
XML configuration transaction code
<!-- Create the transaction manager - >
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<! -- Inject data source -- >
<property name="dataSource" ref="dataSource"></property>
</bean>
<! -2 Configuration Notifications-->
<tx:advice id="txadvice">
<! -- Configure transaction parameters -->
<tx:attributes>
<! -- Specify which rule's method to add a transaction on top of -->
<tx:method name="account" propagation="REQUIRED"/>
<! --<tx:method name="account*"/>-->
</tx:attributes>
</tx:advice>
<! -3 Configuring entry points and cut-planes -->
<aop:config>
<! -- Configure entry points -->
<aop:pointcut id="pt" expression="execution(* com.atgui.spring.service.UserService.*(...))" />
<! --configuration cutscene ->
<aop:advisor advice-ref="txadvice" pointcut-ref="pt"/>
</aop:config>
Service layer code
@Service
//@Transactional
public class UserService {
@Autowired
private UserDao userDao;
public void account(){
//Reduce money
userDao.reducemo();
int i=13/0;
//Increase money
userDao.addmo();
}
}
The above error appears when running
You can modify the error code section in XML
<!-- Creating a Transaction Manager-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- Injecting data sources-->
<property name="dataSource" ref="dataSource"></property>
</bean>
Change the above id = “datasourcetransactionmanager” to id = “transactionmanager”\
That’s it
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- Injecting data sources-->
<property name="dataSource" ref="dataSource"></property>
</bean>
``
This will solve the above problem