spring-使用XML配置聲明式事務

來源:互聯網
上載者:User

標籤:spring-使用xml配置聲明式事務

一、建立spring項目
    項目名稱:spring101501
二、在項目中添加spring支援
    1.在項目中建立lib目錄
        /lib
    2.在lib目錄下添加jar包
        com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
        com.springsource.org.aopalliance-1.0.0.jar
        com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
        commons-logging.jar
        junit-4.10.jar
        log4j.jar
        mysql-connector-java-5.1.18-bin.jar
        spring-aop-3.2.0.RELEASE.jar
        spring-aspects-3.2.0.RELEASE.jar
        spring-beans-3.2.0.RELEASE.jar
        spring-context-3.2.0.RELEASE.jar
        spring-core-3.2.0.RELEASE.jar
        spring-expression-3.2.0.RELEASE.jar
        spring-jdbc-3.2.0.RELEASE.jar
        spring-tx-3.2.0.RELEASE.jar
三、在項目中添加屬性檔案和設定檔
    1.在項目中建立conf目錄
        /conf
    2.在conf目錄下添加屬性檔案
        屬性檔案名稱:jdbc.properties
        屬性檔案內容:
        jdbc.driver = com.mysql.jdbc.Driver
        jdbc.url = jdbc:mysql://localhost:3306/spring
        jdbc.user = root
        jdbc.password =root
    3.在conf目錄下添加核心設定檔
        設定檔名稱:applicationContext.xml
        設定檔內容:
        <?xml version="1.0" encoding="UTF-8"?>
        <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"
               xmlns:tx="http://www.springframework.org/schema/tx"
               xmlns:aop="http://www.springframework.org/schema/aop"
               xsi:schemaLocation="
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
            
        </beans>
四、持久層設計
    1.介面設計
        在src目錄下建立介面所在的包
            包名:cn.jbit.spring101501.dao
        在包建立介面
            介面名:AccountDao.java
            介面內容:
            public interface AccountDao {
                public void outMoney(int outAccount,double money);
                public void inMoney(int inAccount,double money);
            }
    2.實作類別設計
        類名:AccountDaoImpl.java
        類內容:
        public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
            @Override
            public void inMoney(int inAccount, double money) {
                String sql = "UPDATE Account SET money = money + ? WHERE id = ?";
                this.getJdbcTemplate().update(sql,money,inAccount);
            }
        
            @Override
            public void outMoney(int outAccount, double money) {
                String sql = "UPDATE Account SET money = money - ? WHERE id = ?";
                this.getJdbcTemplate().update(sql,money,outAccount);
            }
        }
五、業務層設計
    1.業務介面設計
        包名:cn.jbit.spring101501.service
        介面名:AccountService.java
        介面內容:
        public interface AccountService {
            public void transfer(int outAccount,int inAccount,double money);
        }
    2.業務實現設計
        實作類別名:AccountServiceImpl.java
        實作類別內容:
        public class AccountServiceImpl implements AccountService {
        
            private AccountDao accountDao;
            @Override
            public void transfer(int outAccount, int inAccount, double money) {
                accountDao.outMoney(outAccount, money);
                int a = 1/0;
                accountDao.inMoney(inAccount, money);
            }
            
            public void setAccountDao(AccountDao accountDao) {
                this.accountDao = accountDao;
            }
            public AccountDao getAccountDao() {
                return accountDao;
            }
        }
六、在核心設定檔中添加相關配置
    <!-- 1.載入屬性檔案 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    <!-- 2.設定資料庫串連池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!-- 3.配置jdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 4.配置交易管理員 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 5.配置DAO -->
    <bean id="accountDao" class="cn.jbit.spring101501.dao.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    
    <!-- 6.配置Service -->
    <bean id="accountService" class="cn.jbit.spring101501.service.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    
    <!-- 7.定義通知(Advice) -->
    <tx:advice id="accountAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 8.定義切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* cn.jbit.spring101501.service.*ServiceImpl.*(..))" id="accountpintcut"/>
        <aop:advisor advice-ref="accountAdvice" pointcut-ref="accountpintcut"/>
    </aop:config>
七、測試
    1.在項目中建立test目錄
        /test
    2.在test目錄下添加測試包
        包名:cn.jbit.spring101501.service
    3.在測試包下建立測試類別
        類名:AccountServiceTest.java
        類內容:
        public class AccountServiceTest {
            @Test
            public void testTranser(){
                ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
                AccountService accountService = (AccountService) context.getBean("accountService");
                accountService.transfer(1, 2, 200);
            }
        }

本文出自 “素顏” 部落格,請務必保留此出處http://suyanzhu.blog.51cto.com/8050189/1564315

spring-使用XML配置聲明式事務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.