The @Transactional annotation will not work when the first Transactional annotation method modifier is not public. For example, the following code.

@Component
public class TestServiceImpl {
    @Resource
    TestMapper testMapper;
    
    @Transactional
    void insertTestWrongModifier() {
        int re = testMapper.insert(new Test(10.20.30));
        if (re > 0) {
            throw new NeedToInterceptException("need intercept");
        }
        testMapper.insert(new Test(210.20.30)); }}Copy the code

Within the same package, create a new call object for access

@Component
public class InvokcationService {
    @Resource
    private TestServiceImpl testService;
    public void invokeInsertTestWrongModifier(){
        Call the default accessor method of the @Transactional annotationtestService.insertTestWrongModifier(); }}Copy the code

@test Test case

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
   @Resource
   InvokcationService invokcationService;

   @Test
   public void  testInvoke(){ invokcationService.invokeInsertTestWrongModifier(); }}Copy the code

Testmapper. insert(new Test(10,20,30)); insert(new Test(10,20,30)); Action is not rolled back. Testmapper. insert(new Test(10,20,30)); A rollback will take place