关闭

spring 声明式事务处理及测试用类,其他未列出。

612人阅读 评论(0) 收藏 举报

1、首先给出配置文件:

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
        </property>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl">
        </property>
        <property name="username" value="report"></property>
        <property name="password" value="neusoft"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle9Dialect
                </prop>
                <prop key="hibernate.show_sql">
                    true
                </prop>
            </props>
        </property>
        <property name="mappingLocations">
            <list>
                <value>classpath:com/models/*.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!-- 配置事务管理器,需要依赖注入一个sessionFactory. -->  
     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
      <property name="sessionFactory"><ref local="sessionFactory"/></property>  
    </bean> 
    <!--  配置事务拦截器,需要依赖注入一个事务管理器,指明事务的传播属性。-->  
   <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">   
       <property name="transactionManager" ref="transactionManager"/>  
       <property name="transactionAttributes">  
          <props>  
               <prop key="save*">PROPAGATION_REQUIRED</prop>  
               <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
              <prop key="*">PROPAGATION_REQUIRED</prop>  
            </props>  
       </property>  
   </bean>  
   <!-- 定义BeanNameAutoProxyCreator,该bean是个bean后处理器,无需被引用,因此没有id属性,指定对满足哪些bean name的bean自动生成业务代理-->  
   <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
        <property name="beanNames">  
           <list>  
               <value>welcome</value>  
          </list>   
       </property>   
       <property name="interceptorNames">  
          <list>  
               <value>transactionInterceptor</value>   
            </list>  
      </property>  
   </bean>  

    <bean name="/welcome" class="com.huangyz.struts.action.WelcomeYou">
     <property name="welcome">
      <ref bean="welcome"></ref>
     </property>
    </bean>
    <bean name="welcome" class="com.huangyz.applogic.impl.Welcome">
        <property name="usersDao">
            <ref bean="UsersDAO"></ref>
        </property>
        <property name="user">
            <ref bean="user"></ref>
        </property>
    </bean>
    <bean id="UsersDAO" class="com.huangyz.dao.impl.UsersDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="user" class="com.huangyz.entity.Users" ></bean>
</beans>

2、给出我的测试类:

package com.huangyz.applogic.impl;
import com.huangyz.applogic.interfaces.IWelcome;
import com.huangyz.dao.impl.UsersDAO;
import com.huangyz.entity.Users;
/**
* @see:数据库中给的password的最大长度为8,在这里做的是事务测试,如果起作用,那么数据库中一条都不会插入,
* 反之,会插入一条。
* @author huangyz
*
*/
public class Welcome implements IWelcome {
    private UsersDAO usersDao;
    private Users user;
    public void welcome() {
        user.setName("transManaged12");
        user.setPassword("12345678");
        usersDao.save(user);
        /*下面代码保证两次都是插入,这里改变了实体在session中的状态*/
        usersDao.getSessionFactory().getCurrentSession().evict(user);
        user.setName("tranManaged13");
        user.setPassword("123456789");
        usersDao.save(user);
    }
    public void setUsersDao(UsersDAO usersDao) {
        this.usersDao = usersDao;
    }
    public void setUser(Users user) {
        this.user = user;
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:567559次
    • 积分:6070
    • 等级:
    • 排名:第4267名
    • 原创:180篇
    • 转载:6篇
    • 译文:0篇
    • 评论:84条
    最新评论