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
Read More:
- Cannot resolve reference to bean ‘sqlSessionFactory‘ while setting bean property ‘sqlSessionFactory‘
- [Solved] Springboot Error: Error creating bean with name ‘dataSource‘ defined in class path resource
- [Solved] Error creating bean with name ‘accountService‘: Unsatisfied dependency expressed through field
- [Solved] Rabbitmq injection failed, bean creation failed, error creating bean with name ‘rabbitconnectionfactory’‘
- [Solved] Error creating bean with name ‘fileVisitController‘ defined in file
- [Solved] Spring AOP Error creating bean with name
- Error creating bean with name ‘servletEndpointRegistrar‘ defined in class path resource
- [Solved] Error creating bean with name ‘mappingJackson2HttpMessageConverter‘ defined in class path resource
- [Solved] Error creating bean with name ‘redisConnectionFactory‘ defined in class path resource…
- [Solved] Error creating bean with name ‘configurationPropertiesBeans‘ defined in class path resource…
- [Solved] Error creating bean with name ‘entityManagerFactory‘ defined in class path resource
- [Solved] seata:Error creating bean with name ‘globalTransactionScanner‘ defined in class path resource
- [Solved] RabbitMQ Error: Error creating bean with name ‘rabbitConnectionFactory‘ defined in class path resource
- [Solved] ssm Error: Error creating bean with name ‘employeeController’: Injection of autowired dependencies failed
- [Solved] Spring jdbctemplate Error: ‘Java. SQL. Driver’ for property ‘driver’: no
- Error creating bean with name ‘adminMapper‘ Injection of resource dependencies failed
- [Solved] spring boot Startup Error: Error creating bean with name ‘requestMappingHandlerMapping‘ defined in class path
- springboot Integrate mybatis Error: Error creating bean with name ‘deptController‘: Unsatisfied dependency expresse
- [Solved] springCloud Startup Error: Error creating bean with name ‘defaultValidator‘ defined in class path resource
- Code case of XXL job executor