• 动态代理在转账中的运用


    package com.hope.factory;

    import com.hope.service.IAccountService;
    import com.hope.utils.TransactionManager;

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;

    /**
    * @author newcityman
    * @date 2019/11/22 - 20:33
    * 用于创建service的代理对象的工厂
    */
    public class BeanFactory {
    private IAccountService accountService;
    private TransactionManager txManager;

    public void setTxManager(TransactionManager txManager) {
    this.txManager = txManager;
    }

    public final void setAccountService(IAccountService accountService) {
    this.accountService = accountService;
    }

    public IAccountService getAccountService() {
    return (IAccountService) Proxy.newProxyInstance(accountService.getClass().getClassLoader(), accountService.getClass().getInterfaces(),
    new InvocationHandler() {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Object rtValue = null;
    try {
    // 1、开启事务
    txManager.beainTransaction();
    // 2、执行方法
    rtValue = method.invoke(accountService, args);
    // 3、提交事务
    txManager.commit();
    // 4、返回结果
    return rtValue;
    } catch (Exception e) {
    // 5、回滚事务
    txManager.rollback();
    e.printStackTrace();
    throw new RuntimeException("错误信息如下:");
    } finally {
    // 6、释放资源
    txManager.release();
    }
    }
    });
    }
    }



    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置代理的service-->
    <bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"/>
    <!--配置beanfactory-->
    <bean id="beanFactory" class="com.hope.factory.BeanFactory">
    <property name="accountService" ref="accountService"></property>
    <property name="txManager" ref="txManager"></property>
    </bean>
    <!--配置service-->
    <bean id="accountService" class="com.hope.service.impl.AccountServiceImpl">
    <!--注入dao-->
    <property name="accountDao" ref="accountDao"/>
    </bean>
    <!--配置dao-->
    <bean id="accountDao" class="com.hope.dao.impl.AccountDaoImpl">
    <!--注入runner-->
    <property name="runner" ref="runner"/>
    <!--注入connectionUtils-->
    <property name="connectionUtils" ref="connctionUtils"/>
    </bean>
    <!--配置QueryRunner-->
    <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
    </bean>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!--连接数据库的必备信息-->
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/easy"/>
    <property name="user" value="root"/>
    <property name="password" value="123"/>
    </bean>

    <bean name="connctionUtils" class="com.hope.utils.ConnectionUtils">
    <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean name="txManager" class="com.hope.utils.TransactionManager">
    <property name="connectionUtils" ref="connctionUtils"/>
    </bean>
    </beans>
  • 相关阅读:
    一件重要的事想要告诉大家 AlexCool
    经典永不过时!重温设计模式 AlexCool
    腾讯云网络核心技术演进实践|好文推荐 AlexCool
    经典永驻,重温设计模式 |硬核! AlexCool
    linux网络全景指南 AlexCool
    Linux调度系统全景指南(终结篇) AlexCool
    大厂的面试官是如何挑人的? AlexCool
    中国大学MOOC数据结构基础习题集、023、求前缀表达式的值
    中国大学MOOC数据结构基础习题集、041、Root of AVL Tree
    中国大学MOOC数据结构基础习题集、052、Saving James Bond Easy Version
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11914603.html
Copyright © 2020-2023  润新知