---
title: "Spring2.0 Hibernate3 事务配置"
author: "iamlibo"
date: "2007-06-02T11:29:00Z"
tags: []
slug: "spring2-0-hibernate3"
draft: false
lastmod: "2007-06-02T11:29:00Z"
id: 274411
url: "https://iamlibo.iteye.com/blog/274411"
---

想在Service 层配置事务,Spring 好象是要求必须用接口,因为我没用接口时没配置成功.

一个IService 接口.声明了所有Service层公共的方法,比如save、delete 等。

**public** **interface** IService {

    **public** **int** count(FindCriteria fc);

 

    **public** List find(FindCriteria fc);

 

    **public** Serializable save(Object object) **throws** UnsupportedOperationException;

}

 

UserService 接口，声明UserService 的所有方法。还要 **extends IService**

**public** **interface** UserService **extends** IService {

 

    **public** User login(User user);

    

}

 

UserService接口的实现类.

**public** **class** UserServiceImpl **implements** UserService {

 

    /\**

     * 登录

     *

     * **@param** user

     * **@return** 校验成功的User实例

     \*/

    **public** User login(User u) **throws** UnsupportedOperationException {

        // throw new UnsupportedOperationException();

        User user = (User) userDao.get(User.**class**, u.getId());

        log.debug("get user is " + user);

        **if** (user != **null** && user.getPassword().equals(u.getPassword()))

            **return** user;

        **return** **null**;

    }

 

//其他实现省略…

}

完整的applicationContent.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:aop="http://www.springframework.org/schema/aop"

    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

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"&gt;

 

    &lt;!-- 连接属性 --&gt;

    &lt;bean id="propertyConfigurer"

        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;

        &lt;property name="location" value="WEB-INF/jdbc.properties" /&gt;

    &lt;/bean&gt;

 

    &lt;!-- 数据源 --&gt;

    &lt;bean id="dataSource"

        class="org.apache.commons.dbcp.BasicDataSource"&gt;

        &lt;property name="driverClassName" value="${jdbc.driver}" /&gt;

        &lt;property name="url" value="${jdbc.url}" /&gt;

        &lt;property name="username" value="${jdbc.user}" /&gt;

        &lt;property name="password" value="${jdbc.password}" /&gt;

    &lt;/bean&gt;

 

    &lt;!-- hibernate sessionFactory --&gt;

    &lt;bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;

        &lt;property name="dataSource" ref="dataSource" /&gt;

        &lt;property name="hibernateProperties"&gt;

            &lt;props&gt;

                &lt;prop key="hibernate.dialect"&gt;

                    ${hibernate.dialect}

                &lt;/prop&gt;

                &lt;prop key="hibernate.show\_sql"&gt;true&lt;/prop&gt;

                &lt;prop key="hibernate.query.substitutions"&gt;

                    true 1, false 0, yes 'Y', no 'N'

                &lt;/prop&gt;

                &lt;prop key="hibernate.jdbc.fetch\_size"&gt;50&lt;/prop&gt;

                &lt;prop key="hibernate.jdbc.batch\_size"&gt;25&lt;/prop&gt;

            &lt;/props&gt;

        &lt;/property&gt;

        &lt;property name="annotatedClasses"&gt;

            &lt;list&gt;

                &lt;value&gt;cn.xiangyunsoft.busniess.model.Rose&lt;/value&gt;

                &lt;value&gt;cn.xiangyunsoft.busniess.model.User&lt;/value&gt;

                &lt;value&gt;cn.xiangyunsoft.busniess.model.Department&lt;/value&gt;

            &lt;/list&gt;

        &lt;/property&gt;

    &lt;/bean&gt;

 

    &lt;!-- 配置事务管理 --&gt;

 

    &lt;!-- 事务通知类 --&gt;

    &lt;bean id="profiler"

        class="cn.xiangyunsoft.busniess.service.SimpleProfiler"&gt;

        &lt;!-- order 值可以决定通知的先后顺序 ,与后面的order的值的大小，决定了是先通知再执行，还是先执行再通知--&gt;

        &lt;property name="order" value="2" /&gt;

    &lt;/bean&gt;

 

    &lt;bean id="transactionManager"

        class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;

        &lt;property name="sessionFactory" ref="sessionFactory" /&gt;

    &lt;/bean&gt;

    &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;

        &lt;!-- the transactional semantics... --&gt;

        &lt;tx:attributes&gt;

            &lt;!-- 以get 、 find 开头的方法是只读事务 --&gt;

            &lt;tx:method name="get\*" read-only="true" /&gt;

            &lt;tx:method name="find\*" read-only="true" /&gt;

            &lt;!-- 其他方法是默认 --&gt;

            &lt;tx:method name="\*" /&gt;

        &lt;/tx:attributes&gt;

    &lt;/tx:advice&gt;

 

    &lt;aop:config&gt;

        &lt;!-- 此处的IService 是表示对所有实现IService接口的类管理事务 --&gt;

        &lt;aop:pointcut id="serviceOperation"

            expression="execution(* cn.xiangyunsoft.busniess.service.IService.\*(..))" /&gt;

        &lt;aop:advisor advice-ref="txAdvice"

            pointcut-ref="serviceOperation" order="1" /&gt;

        &lt;aop:aspect id="profilingAspect" ref="profiler"&gt;

            &lt;!-- --&gt;

            &lt;aop:pointcut id="serviceMethodWithReturnValue"

                expression="execution(!void cn.xiangyunsoft.busniess.service..\*Service.\*(..))" /&gt;

            &lt;!-- 通知类型为after-throwing 表示发生异常时通知，还有其他选择 --&gt;

            &lt;aop:after-throwing method="profile"

                pointcut-ref="serviceMethodWithReturnValue" /&gt;

        &lt;/aop:aspect&gt;

    &lt;/aop:config&gt;

 

&lt;/beans&gt;

Service 层的bean 在另一个beans.xml文件配置

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;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-2.0.xsd"&gt;

    &lt;bean id="userService"

        class="cn.xiangyunsoft.busniess.service.UserServiceImpl"&gt;

        &lt;property name="userDao" ref="userDao"&gt;&lt;/property&gt;

    &lt;/bean&gt;

    &lt;bean id="userDao"

        class="cn.xiangyunsoft.busniess.dao.UserDao"&gt;

        &lt;property name="sessionFactory" ref="sessionFactory"&gt;&lt;/property&gt;

    &lt;/bean&gt;

&lt;/beans&gt;

还有一个执行通知的类

**public** **class** SimpleProfiler **implements** Ordered {

 

    **private** **int** order;

// allows us to control the ordering of advice

**public** **int** getOrder() {

**return** **this**.order;

}

**public** **void** setOrder(**int** order) {

**this**.order = order;

}

 

// this method is the around advice

**public** Object profile(ProceedingJoinPoint call) **throws** Throwable {

    System.*out*.println("事务执行完成");

Object returnValue;

StopWatch clock = **new** StopWatch(getClass().getName());

**try** {

clock.start(call.toShortString());

returnValue = call.proceed();

} **finally** {

clock.stop();

System.*out*.println(clock.prettyPrint());

}

**return** returnValue;

}

}

 

测试类：

**public** **class** BaseServiceTest **extends** AbstractTransactionalSpringContextTests {

    **protected** String\[] getConfigLocations() {

        **return** **new** String\[] { "classpath:/applicationContext\_Test.xml",

                "beans.xml" };

    }

}

**public** **class** UserServiceTest **extends** BaseServiceTest {

 

    **private** UserService userService;

 

    **public** **void** setUserService(UserService userService) {

        **this**.userService = userService;

    }

 

    **public** UserService getUserService() {

        **return** userService;

    }

 

    @Test

    **public** **void** testLogin() {

        User user = **new** User("adm");

        user.setPassword("admin");

        userService.login(user);

    }

 

    @Test

    **public** **void** testSave() {

        User user = **new** User("122");

        user.setName("abc");

        userService.save(user);

    }

}

这两个测试方法一个成功，一个不成功就可以测试出通知发生在不同的时间了。

 

一点感想：

就是在读Spring手册时不够认真，这个事务配置了好几天，都是不成功，在网上也没有找到合适的解决方法，最后又仔细的读了一次手册，终于成功！教训~！