參考:https://blog.csdn.net/lilizhou2008/article/details/106110401/
總結:
查看源碼,顯示catch中的代碼被spring 動態代理,原因是本方法和catch中的方法都被同一事務管理:@Transactional(rollbackFor = Exception.class)
示例:
@Transactional public class test156{ @Autowired UserCenterService userCenterService; public Object getUserName (){ try { Map<String, Object> userInfo = userCenterService.getInfoByIpa(String ipa); if (userInfo != null) { return userInfo.get("name"); } else { return null; } } catch (Exception ex) { log.info("user center has exception..."+ex); } return null; } } @Transactional public class UserCenterServiceImpl implements UserCenterService{ public String getInfoByIpa(String ipa) { // get result by other server return doPostMethod(requestType, url, params, headerMap) } }
以上情況,在 test156 中的catch並不會生效返回null, 而是會拋出異常,參考繼承性事務。