AOP思想控制事务

1.自已实现编程式事务(可以自定义将某些方法放入事务中)
public class BeanFactory {
    private static Set<String> includeMethod = new HashSet<String>();//需要控制事务的方法
    
    static{
        includeMethod.add("transfer");
    }
    
    public static BusinessService getBusinessSerivce(){
        final BusinessService s = new BusinessServiceImpl();
        BusinessService proxyS = (BusinessService)Proxy.newProxyInstance(s.getClass().getClassLoader(), 
                s.getClass().getInterfaces(), 
                new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable {
                        
                        String methodName = method.getName();
                        if(includeMethod.contains(methodName)){
                        
                            Object rtValue = null;
                            try{
                                TransactionManager.startTransaction();
                                rtValue = method.invoke(s, args);
                                TransactionManager.commit();
                            }catch(Exception e){
                                TransactionManager.rollback();
                                e.printStackTrace();
                            }finally{
                                TransactionManager.release();
                            }
                            return rtValue;
                        }else{
                            return method.invoke(s, args);
                        }
                    }
                });
        return proxyS;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 缘起 因为组织了个到其他同业学习的活动,所以临时成立了个微信群。在完成了这个群的历史使命之后,@所有人宣布群准备解...
    彭玄霏阅读 1,440评论 0 0
  • 前天去南山区科技园的一栋大厦,发现这栋大厦除了快递柜、自动售卖机、自动售卡机之外,与其他大厦不同的是还多了一排白颜...
    阿渡SZ阅读 742评论 0 0
  • 新年了, 小时候的新年早晨,妈妈很早起床,蹑手蹑脚地走到我和弟弟的床边,往我们的嘴巴里塞一颗果子,才允许开口说话。...
    芳与华阅读 456评论 1 3

友情链接更多精彩内容