SpringBoot and transaction control @Transactional (detail)

As we all know, the operation of ensuring database consistency is the control of transactions. Spring transaction management can be divided into two types: programmatic (writing code, that is, xml configuration files) and declarative (programming, or AOP injection) (specific configuration can be found in the blog).

For Spring Boot, the recommended operation is to use @Transactional annotation to declare the transaction (@Transactional annotation details can be found in the blog).

Let's use @Transactional together to add transaction control.

1. Guide package

To support transactions in Spring boot, first import the JDBC or JPA dependencies provided by Spring boot:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
   <scope>test</scope>
</dependency>
 
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
   <scope>test</scope>
</dependency>

When we import these two packages, SpringBoot will automatically inject DataSourceTransactionManager or JpaTransactionManager by default.

2. Add @EnableTransactionManagement annotation to the startup class

Because SpringBoot will automatically configure transactions, this annotation can be added or not added. The specific implementation can be viewed in the org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration class.

@SpringBootApplication
 // Enable transaction management (can be omitted) 
@EnableTransactionManagement
@MapperScan("mapper路径")
public class KxlApplication {
    public static void main(String[] args) {
        SpringApplication.run(KxlApplication.class, args);
    }
}

Third, add @Transactional annotation in the service layer

The @Transactional annotation can be added to the class or method. The specific usage details of @Transactional can be found in the blog.

If you write code style specifications, generally @Transactional is added to the Service layer.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class TestService implements ITestService {
    @Autowired
    private TestMapper testMapper;

}
@Scope (proxyMode = ScopedProxyMode.TARGET_CLASS), the purpose of this annotation is to indicate that all transactions on this method are proxied by CGLib. 
See the blog for details.

The above configuration I run without errors, but some small partners may encounter errors similar to the following when starting the project.
Description:

The bean 'testService' could not be injected as a 'com.pk.kxl.service.impl.TestService' because it is a JDK dynamic proxy that implements:
    com.pk.kxl.service.ITestService


Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

This reason is caused by the difference between the two proxy methods of jdk automatic proxy and CGlib proxy. If found, you can completely follow my configuration, or you can refer to the blog.

 

Guess you like

Origin www.cnblogs.com/pengpengdeyuan/p/12736799.html