jave ee 5下spring 事务的简化配置

在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>

你可能感兴趣的:(jave ee 5下spring 事务的简化配置)