@Transactional可在抽象类上定义,且能生效
2020-07-28 20:05:23 1325
定义一个抽象类
import com.shopping.base.repository.sync.ipos.coupon.IposCzklbRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
@Transactional(rollbackFor = Exception.class)
public abstract class AbstractHandler {
@Autowired
HandlerRepository HandlerRepository;
public abstract void test();
}
继承抽象类
import org.springframework.stereotype.Component;
@Component
public class AHandler extends AbstractHandler {
@Override
public void test() {
System.out.println(super.iposCzklbRepository.findAll());
}
}
在其他地方注入
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private AbstractHandler handler;
@GetMapping("/handler")
public String handler() throws Exception {
handler.test();
return "SUCCESS";
}
}
能够调用执行
更改一下test()方法,
super.iposCzklbRepository.deleteById(97);
int i = 1 / 0;
事务也能生效