jave ee 5下spring 事務的簡化配置

來源:互聯網
上載者:User
   在java ee 5的環境下,spring的配置可以大大簡化事務的配置,要spring 2.0的了,比如

import org.springframework.transaction.annotation.Transactional;

@Transactional
public interface BookDao {

    @Transactional(readOnly=true)
    Book query(String id);

    @Transactional(readOnly=true)
    List<Book> queryAll();

    void insert(Book book);

    void update(Book book);

    void delete(Book book);

}

  要注意的是只能注釋到public類裡,不能注釋到private裡的,

另外,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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
>
    <!-- 定義DataSource -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:mem:bookstore" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>

    <!-- 定義TransactionManager -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 定義BookDao -->

    <bean id="bookDao" class="example.chapter6.BookDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 將所有具有@Transactional註解的Bean自動設定為聲明式事務支援 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.