当前位置: 首页 > news >正文

电子商务建设网站360排名检测

电子商务建设网站,360排名检测,网站建设考核标准,wordpress 站点错误使用Spring与JDK动态代理实现事务管理 在现代企业级应用开发中,事务管理是一项关键的技术,它可以保证一系列操作要么全部成功,要么全部失败,从而确保数据的一致性和完整性。Spring框架提供了强大的事务管理能力,但有时…

使用Spring与JDK动态代理实现事务管理

在现代企业级应用开发中,事务管理是一项关键的技术,它可以保证一系列操作要么全部成功,要么全部失败,从而确保数据的一致性和完整性。Spring框架提供了强大的事务管理能力,但有时为了更细粒度地控制事务边界,我们可能需要自己实现事务管理逻辑。本文将介绍如何结合Spring框架和JDK动态代理技术来实现一个简单的事务管理系统。

引言

在本示例中,我们将创建一个基于Spring框架的应用程序,该程序包含一个账户服务接口(IAccountService),以及其实现类(AccountServiceImp)。为了增强该服务的事务处理能力,我们将创建一个工厂类(ProxyBeanFactory),它会为IAccountService生成一个动态代理对象,该对象能够在调用真实服务方法前后自动开启和提交事务。

XML配置

首先,我们需要配置Spring的bean定义。下面是一个简化版的Spring配置文件示例:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!-- 数据源配置略 --><!-- QueryRunner配置略 --><!-- 连接工具类配置略 --><!-- 事务工具类配置略 --><!-- 数据访问层配置略 --><!-- 业务逻辑层配置略 --><bean id="proxyService" class="org.example.service.IAccountService" factory-bean="factory" factory-method="createProxy"></bean><bean id="factory" class="org.example.factory.ProxyBeanFactory"><property name="transactionUtil" ref="transactionUtil"></property><property name="toProxyService" ref="service"/></bean><!-- 控制器配置略 --></beans>

如上所示,ProxyBeanFactory将创建一个实现了IAccountService接口的代理对象,并且会在执行业务逻辑之前和之后自动管理事务。

动态代理工厂类

接下来,我们来看一下ProxyBeanFactory类的具体实现:

package org.example.factory;import org.example.service.IAccountService;
import org.example.util.TransactionUtil;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;public class ProxyBeanFactory {private IAccountService toProxyService;private TransactionUtil transactionUtil;public void setToProxyService(IAccountService toProxyService) {this.toProxyService = toProxyService;}public void setTransactionUtil(TransactionUtil transactionUtil) {this.transactionUtil = transactionUtil;}public IAccountService createProxy() {return (IAccountService) Proxy.newProxyInstance(toProxyService.getClass().getClassLoader(),toProxyService.getClass().getInterfaces(),new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object result = null;try {transactionUtil.beginTx();result = method.invoke(toProxyService, args);transactionUtil.commitTx();} catch (Exception e) {transactionUtil.rollbackTx();} finally {transactionUtil.closeTx();}return result;}});}
}

ProxyBeanFactory类的核心在于createProxy方法,它使用JDK动态代理机制来创建代理对象。当任何IAccountService接口方法被调用时,都会触发InvocationHandler中的invoke方法,从而开启事务、执行业务逻辑并最终提交或回滚事务。

示例代码:

applicationContext.xml:

  <!-- 加载资源文件 --><context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder><!-- 注入数据源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${msg1}"/><property name="jdbcUrl" value="${msg2}"/><property name="user" value="${msg3}"/><property name="password" value="${msg4}"/></bean><bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"><constructor-arg name="ds" ref="dataSource"/></bean><!-- 连接工具类 --><bean id="connectionUtil" class="org.example.util.ConnectionUtil"><property name="dataSource" ref="dataSource"/></bean><!-- 事务工具类 --><bean id="transactionUtil" class="org.example.util.TransactionUtil"><property name="connectionUtil" ref="connectionUtil"/></bean><bean id="mapperImp" class="org.example.dao.AccountMapperImp"><property name="queryRunner" ref="queryRunner"></property><property name="connectionUtil" ref="connectionUtil"></property></bean><bean id="service" class="org.example.service.AccountServiceImp"><property name="dao" ref="mapperImp"></property></bean><bean id="proxyService" class="org.example.service.IAccountService" factory-bean="factory" factory-method="createProxy"></bean><bean id="factory" class="org.example.factory.ProxyBeanFactory"><property name="transactionUtil" ref="transactionUtil"></property><property name="toProxyService" ref="service"/></bean><bean id="controller" class="org.example.controller.AccountControllerImp"><property name="service" ref="proxyService"></property></bean></beans>

ProxyBeanFactory:

public class ProxyBeanFactory {IAccountService toProxyService;;//装配事务工具类TransactionUtil transactionUtil;public void setAccountServiceImp(IAccountService accountServiceImp) {toProxyService = accountServiceImp;}public void setToProxyService(IAccountService toProxyService) {this.toProxyService = toProxyService;}public void setTransactionUtil(TransactionUtil transactionUtil) {this.transactionUtil = transactionUtil;}public IAccountService getAccountServiceImp() {return toProxyService;}//2.创建代理public IAccountService createProxy(){IAccountService proxy = (IAccountService) Proxy.newProxyInstance(toProxyService.getClass().getClassLoader(), toProxyService.getClass().getInterfaces(), new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {Object obj = null;try {transactionUtil.beginTx();obj = method.invoke(toProxyService, args);transactionUtil.commitTx();} catch (Exception e) {e.printStackTrace();transactionUtil.rollbackTx();} finally {transactionUtil.closeTx();}return obj;}});return proxy;}
}
http://www.hkea.cn/news/947297/

相关文章:

  • 做爰全过程网站免费的视频谷歌seo搜索引擎
  • 怎么架设网站seo推广培训
  • 自己网站做问卷调查网页设计学生作业模板
  • 清远企业网站排名深圳网站建设系统
  • 互助平台网站建设费用卡点视频免费制作软件
  • 上海做b2b国际网站公司排名优化公司电话
  • 裙晖wordpress重庆seo整站优化
  • 乌克兰网站后缀谷歌浏览器下载电脑版
  • 建设部网站撤销注册资质的都是公职人员吗正规网络公司关键词排名优化
  • 杂志网站建设推广方案铜川网络推广
  • 网站建设后怎么搜索引擎优化解释
  • 网站建设维护 天博网络成都营销型网站制作
  • 秦皇岛北京网站建设百度广告投放电话
  • 团购做的比较好的网站营销推广ppt
  • 网站怎么做网站地图重庆网站制作公司哪家好
  • wordpress改地址后打不开seo品牌优化整站优化
  • 网页设计师证书含金量高吗百度网络优化
  • 咸阳网站开发长沙seo优化公司
  • 网站通cms国内十大搜索引擎排名
  • centos7安装 wordpress网站如何进行seo
  • 设计师灵感网站美国今天刚刚发生的新闻
  • 重庆南岸营销型网站建设公司推荐竞价sem托管
  • 深圳做二维码网站建设什么是互联网营销
  • 网易企业邮箱收费标准百色seo关键词优化公司
  • 做网站的财务需求张北网站seo
  • 北京赛车彩票网站怎么做佛山本地网站建设
  • 门户网站的建设方式有哪些网络推广引流
  • 做中东服装有什么网站免费seo刷排名
  • 做网站用java还是c语言百度竞价推广培训
  • 做动画视频的网站市场监督管理局官网入口