spring中事物回滚与异常try catch相关
来源:互联网 发布:你曾是少年 知乎 编辑:程序博客网 时间:2024/06/06 00:32
再次简单说一下 try catch / throw new exception /throws exception的区别
try catch是在方法内部对异常进行处理,并且可以选择将功能继续执行. throw new exception是在方法内部,将异常抛出,使其上一级对异常进行处理. 而throws exception则是标识该方法内部可能有异常 使上一级对该异常进行处理.增加代码的健全性.
Propagation.REQUIRED ->事物传播机制->
当方法A调用方法B,方法B需要有独立的事物时,需要使用此事物传播机制.然而.当A与B在同一类时,此时B的独立事物是不生效的.解决方式一是:需要将A和B独立于两个不同的类中;二是如果两者处于同一业务层,则需要获取spring中的该对象的代理对象,然后调用方法B.如springApplicationUtils.getApplication().getBean(name)获取该业务类,然后进行调用.
- /**
- * 获取spring容器,以访问容器中定义的其他bean
- */
- @Component
- public class SpringContextUtil implements ApplicationContextAware {
- // Spring应用上下文环境
- @Autowired
- private ApplicationContext applicationContext;
- /**
- * 实现ApplicationContextAware接口的回调方法,设置上下文环境
- *
- * @param applicationContext
- */
- public void setApplicationContext(ApplicationContext applicationContext) {
- this.applicationContext = applicationContext;
- }
- /**
- * @return ApplicationContext
- */
- public ApplicationContext getApplicationContext() {
- return this.applicationContext;
- }
- /**
- * 获取对象 这里重写了bean方法,起主要作用
- *
- * @param name
- * @return Object 一个以所给名字注册的bean的实例
- * @throws BeansException
- */
- public Object getBean(String name) throws BeansException {
- return applicationContext.getBean(name);
- }
- }
/** * 获取spring容器,以访问容器中定义的其他bean */@Componentpublic class SpringContextUtil implements ApplicationContextAware { // Spring应用上下文环境 @Autowired private ApplicationContext applicationContext; /** * 实现ApplicationContextAware接口的回调方法,设置上下文环境 * * @param applicationContext */ public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } /** * @return ApplicationContext */ public ApplicationContext getApplicationContext() { return this.applicationContext; } /** * 获取对象 这里重写了bean方法,起主要作用 * * @param name * @return Object 一个以所给名字注册的bean的实例 * @throws BeansException */ public Object getBean(String name) throws BeansException { return applicationContext.getBean(name); }}
使用
- @Autowired
- private SpringContextUtil springContextUtil;
- springContextUtil.getApplicationContext().getBean(this.getClass());
@Autowiredprivate SpringContextUtil springContextUtil;springContextUtil.getApplicationContext().getBean(this.getClass());
阅读全文
0 0
- spring中事物回滚与异常try catch相关
- try catch异常抛出与spring事务回滚策略相关
- try catch异常抛出与spring事务回滚策略相关
- try{} catch(Exception e){}异常抛出与Spring事务回滚策略
- spring事物异常回滚
- PHP中PDO连接进行事物回滚及try{}catch(){}
- PHP中PDO连接进行事物回滚及try{}catch(){}
- sql serverl 事物回滚三(try...catch事务回滚)
- 事物回滚与异常
- spring学习----spring事物异常回滚
- JAVA异常与事物回滚探究
- Java异常与事物回滚探究
- spring事物的提交与回滚
- spring事物回滚
- Spring事物回滚
- try catch后spring 事物不显式提交
- Spring 事物抛出Exception 异常时事物没有回滚
- Spring管理的事务:如何配置回滚及try catch情况下的回滚
- 理解RESTful架构
- 苹果iOS系统最新市占率统计:iOS 11份额达到52%
- Node快速入门
- 静态html文件怎么在手机上访问
- element ui 里的表单验证说明
- spring中事物回滚与异常try catch相关
- 面试-年初面试问题精选
- 开启Tomcat的cgi解析python脚本
- 图形用户界面GUI_1
- CodeForces
- eclipse+selenium谷歌浏览器驱动配置
- 11月7日云栖精选夜读:阿里巴巴风鸣:做技术Leader要有危机意识
- java 基本类型
- CSS居中布局