`
iamlibo
  • 浏览: 68806 次
社区版块
存档分类
最新评论

Spring2.0 Hibernate3 事务配置

阅读更多

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

<?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: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">

 

    <!-- 连接属性 -->

    <bean id="propertyConfigurer"

        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="location" value="WEB-INF/jdbc.properties" />

    </bean>

 

    <!-- 数据源 -->

    <bean id="dataSource"

        class="org.apache.commons.dbcp.BasicDataSource">

        <property name="driverClassName" value="${jdbc.driver}" />

        <property name="url" value="${jdbc.url}" />

        <property name="username" value="${jdbc.user}" />

        <property name="password" value="${jdbc.password}" />

    </bean>

 

    <!-- hibernate sessionFactory -->

    <bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />

        <property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">

                    ${hibernate.dialect}

                </prop>

                <prop key="hibernate.show_sql">true</prop>

                <prop key="hibernate.query.substitutions">

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

                </prop>

                <prop key="hibernate.jdbc.fetch_size">50</prop>

                <prop key="hibernate.jdbc.batch_size">25</prop>

            </props>

        </property>

        <property name="annotatedClasses">

            <list>

                <value>cn.xiangyunsoft.busniess.model.Rose</value>

                <value>cn.xiangyunsoft.busniess.model.User</value>

                <value>cn.xiangyunsoft.busniess.model.Department</value>

            </list>

        </property>

    </bean>

 

    <!-- 配置事务管理 -->

 

    <!-- 事务通知类 -->

    <bean id="profiler"

        class="cn.xiangyunsoft.busniess.service.SimpleProfiler">

        <!-- order 值可以决定通知的先后顺序 ,与后面的order的值的大小,决定了是先通知再执行,还是先执行再通知-->

        <property name="order" value="2" />

    </bean>

 

    <bean id="transactionManager"

        class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory" />

    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">

        <!-- the transactional semantics... -->

        <tx:attributes>

            <!-- get find 开头的方法是只读事务 -->

            <tx:method name="get*" read-only="true" />

            <tx:method name="find*" read-only="true" />

            <!-- 其他方法是默认 -->

            <tx:method name="*" />

        </tx:attributes>

    </tx:advice>

 

    <aop:config>

        <!-- 此处的IService 是表示对所有实现IService接口的类管理事务 -->

        <aop:pointcut id="serviceOperation"

            expression="execution(* cn.xiangyunsoft.busniess.service.IService.*(..))" />

        <aop:advisor advice-ref="txAdvice"

            pointcut-ref="serviceOperation" order="1" />

        <aop:aspect id="profilingAspect" ref="profiler">

            <!-- -->

            <aop:pointcut id="serviceMethodWithReturnValue"

                expression="execution(!void cn.xiangyunsoft.busniess.service..*Service.*(..))" />

            <!-- 通知类型为after-throwing 表示发生异常时通知,还有其他选择 -->

            <aop:after-throwing method="profile"

                pointcut-ref="serviceMethodWithReturnValue" />

        </aop:aspect>

    </aop:config>

 

</beans>

Service 层的bean 在另一个beans.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"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="userService"

        class="cn.xiangyunsoft.busniess.service.UserServiceImpl">

        <property name="userDao" ref="userDao"></property>

    </bean>

    <bean id="userDao"

        class="cn.xiangyunsoft.busniess.dao.UserDao">

        <property name="sessionFactory" ref="sessionFactory"></property>

    </bean>

</beans>

还有一个执行通知的类

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手册时不够认真,这个事务配置了好几天,都是不成功,在网上也没有找到合适的解决方法,最后又仔细的读了一次手册,终于成功!教训~

分享到:
评论

相关推荐

    struts2.0 spring2.0 hibernate3.0整合开发快速入门-3

    ### Struts2.0、Spring2.0与Hibernate3.0整合开发快速入门知识点解析 #### 一、Struts2.0与Hibernate3.0整合基础 **知识点1:Struts2.0简介** - **定义**: Struts2是Apache基金会下的一个开源项目,它是一个基于...

    spring2.0hibernate3.1

    提供的两个CHM文件——hibernate3.1.CHM和Spring2.0.chm,是官方的英文帮助文档,对于深入理解Spring 2.0和Hibernate 3.1的细节、API以及最佳实践具有极大价值。通过阅读这些文档,开发者可以掌握这两个框架的核心...

    spring2.0 hibernate 3.0 struts1.1 xfire1.2 整合

    这里我们关注的是一个整合了Spring 2.0、Hibernate 3.0、Struts 1.1以及XFire 1.2的项目。这些技术都是Java Web开发中的重要组件,各自在应用程序的不同层面提供服务。 Spring 2.0是Java企业级应用中的一个核心框架...

    Spring2.0+Hibernate3.1的事务管理

    本文将详细探讨"Spring2.0+Hibernate3.1的事务管理"这一主题,以及如何结合这两个框架来实现高效、可靠的事务处理。 首先,Spring 2.0是一个全面的Java企业级应用开发框架,它提供了依赖注入(DI)和面向切面编程...

    struts2.0 hibernate3.1 spring2.0 整合应用

    Struts2.0、Hibernate3.1 和 Spring2.0 是经典的Java企业级开发框架,它们的整合应用是构建高效、可维护的企业级Web应用程序的关键。这个实例旨在帮助那些已经有一定基础的开发者提升技能,通过结合这些技术,实现对...

    Spring2.0宝典源代码

    3. **数据访问集成**:Spring 2.0加强了对各种持久层技术的支持,包括JDBC、Hibernate、JPA等,通过声明式事务管理,使得数据库操作更加便捷和安全。 4. **Web MVC框架**:Spring 2.0的Web MVC框架有了显著提升,...

    Spring2.0的配置

    本文将详细介绍Spring 2.0的声明式事务配置以及如何简化这一过程。 首先,Spring提供了多种事务管理器,以适应不同的持久层技术和环境。对于单一资源,可以选择如DataSourceTransactionManager(适用于JDBC)、...

    spring2.0中文手册及使用指南 chm

    再者,Spring 2.0还包含了一个强大的数据访问层,支持JDBC、ORM框架(如Hibernate和JPA)以及模板类,如JdbcTemplate和HibernateTemplate。这些工具使得数据库操作更加便捷和安全,同时降低了与数据库交互的复杂性。...

    SPRING2.0中文文档

    Spring 2.0强化了与Java EE规范的集成,包括JTA事务管理、EJB3集成以及对JavaServer Faces的支持,使得开发者可以在不牺牲Spring优点的前提下,充分利用Java EE的功能。 总结,这份中文版的Spring 2.0技术文档全面...

    Spring2.0中文教程

    Spring 2.0是Spring框架的一个重要版本,它在Java企业级应用开发中扮演着核心角色。本教程将深入探讨...文档`spring2.0-reference_final_zh_cn.chm`将详细阐述这些概念和技术,帮助你成为一名熟练的Spring开发者。

    详尽的Spring2.0学习提纲

    2. Spring与Hibernate集成:理解Spring对Hibernate的支持,包括SessionFactory和SessionFactoryBean的配置。 3. Spring与Spring Data JPA:使用Spring Data JPA简化JPA操作,通过Repository接口实现CRUD。 七、实战...

    spring2.0 中文教程

    3. **数据访问集成**:Spring 2.0增强了对各种持久层技术的支持,包括JDBC、Hibernate、iBatis等。它提供了一致的编程模型和事务管理策略,简化了数据库操作。 4. **Web MVC框架**:Spring 2.0的Web MVC框架提供了...

    SPRING2.0开发详解

    ### SPRING2.0开发详解 #### 一、Spring框架简介 Spring框架是一个开源的Java平台,用于构建企业级应用程序和服务。它最初由Rod Johnson在2004年创建,并随着时间的发展不断壮大和完善。Spring 2.0版本是Spring...

    spring2.0 jar包

    2. **AOP(Aspect-Oriented Programming,面向切面编程)增强**:Spring 2.0增强了对AOP的支持,允许开发者定义和实现切面,从而更好地分离关注点,比如事务管理、日志记录等,可以独立于业务逻辑进行处理。...

    spring2.0技术手册_源代码(全十章)

    同时,还可以了解基于XML的事务配置和基于注解的事务配置的区别。 第四,Spring 2.0在数据访问层也有所提升,如JDBC抽象层的增强,使得数据库操作更加简洁和安全。源码中会展示如何使用`JdbcTemplate`和`...

    Spring 2.0 源代码

    3. **数据访问抽象(Data Access Abstraction)**:Spring 2.0加强了对各种数据访问技术的支持,如JDBC、Hibernate和iBatis。它提供了一组通用的抽象层,简化了数据库操作,同时支持声明式事务管理,降低了事务处理...

    Struts2.1.6+Spring2.0+Hibernate3.1

    根据给定文件的信息,本文将详细介绍如何配置Struts2.1.6、Spring2.0与Hibernate3.1这三个框架的整合开发环境。这是一套经典的MVC(Model-View-Controller)架构组合,适用于构建复杂的Java Web应用程序。 ### 一、...

    spring2.0技术手册.pdf

    3. **事务管理**:Spring 提供了一个一致性的事务管理接口,无论是在本地事务还是全局事务中,都可以提供统一的事务管理策略。 4. **数据访问/集成**:Spring 包含了大量的 JDBC、ORM(如 Hibernate)、JPA 和 O/R...

Global site tag (gtag.js) - Google Analytics