siyn

http://www.siyn.org

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  37 随笔 :: 1 文章 :: 24 评论 :: 0 Trackbacks
一个FooService类 有一个add方法
public class FooServiceImpl implements IFooService
{
    
private IFooDAO fooDAO = null;
    
public void addFoo(Bean bean)
    
{
        
//.
        fooDAO.save(bean);
        
        
throw new SystemException("强制抛出异常,以测试事务回滚");
    }


    
//fooDAO get and set method
}
一个日志类 有一个addLog方法
public class LogInterceptor 
{
    
/** 日志记录DAO实例 */
    
private ILogDAO logDAO;
    
    
public void doCheckBefore(JoinPoint jp)
    
{
        Log log 
= new Log();

        String target 
= jp.getTarget().getClass().getName();
        target 
= target.substring(target.lastIndexOf(".")+1);
        
        System.out.println(
"LogInterceptor: excute doCheckBefore!");
        System.out.println(
"================================================");
        System.out.println(
"target:"+target+" method:"+jp.getSignature().getName()+"args:"+jp.getArgs());
        System.out.println(
"================================================");
        
        log.setTarget(target);
        log.setMethod(jp.getSignature().getName());
        
        logDAO.addLog(log);
    }


    
//logDAO get and set method
}
spring配置文件
<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"
>

    
<!-- DAO -->
    
<!-- ================ 注入所有DAO BEGIN ================ -->
    
<bean id="fooDAO" class="org.siyn.foo.dao.impl.FooDaoImpl">
        
<property name="sessionFactory">
            
<ref bean="sessionFactory" />
        
</property>
    
</bean>
        
<!-- 日志 DAO -->
    
<bean id="logDAO" class="org.siyn.foo.dao.impl.LogDaoImpl">
        
<property name="sessionFactory">
            
<ref bean="sessionFactory" />
        
</property>
    
</bean>
        
<!-- ================ 注入所有DAO END ================ -->

        
<!-- Service -->
    
<!-- ================ 注入所有SERVICE BEGIN ================ -->
    
<bean id="fooService"  class="org.siyn.foo.service.impl.FooServiceImpl">
        
<property name="fooDAO">
            
<ref bean="fooDAO"/>
        
</property>
    
</bean>
    
<!-- ================ 注入所有SERVICE END ================ -->

    
<!-- ================ 配置AOP BEGIN ================ -->
    
<!-- 日志记录拦截器 -->
        
<bean id="logInterceptor" class="org.siyn.foo.interceptor.LogInterceptor">
            
<property name="logDAO">
            
<ref bean="logDAO" />
            
</property>
    
</bean>
    
    
<!-- AOP(依赖注入 面向切面编程)示例 ,声明一个切面 -->
    
<aop:config>
        
<aop:aspect id="logAspect" ref="logInterceptor">
            
<!-- 声明一个切入点 -->
            
<aop:pointcut id="simpleService" expression="execution(* org.siyn.foo.service.*.*(..))" />
            
<!-- 声明通知 -->
            
<!-- 前置通知:在某个连接点方法之前执行通知 -->
            
<aop:before pointcut-ref="simpleService" method="doCheckBefore" />
        
</aop:aspect>
    
</aop:config>
    
<!-- ================ 配置AOP END ================ -->

    
<!-- ================ 配置 事务管理 BEGIN ================ -->
    
<!-- 配置TransactionManager -->
    
<bean id="txManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
        
<property name="sessionFactory" ref="sessionFactory" />
    
</bean>

    
<!-- 配置事务策略:通知 在某个切入点执行代码,可以针对每一个方法或者每一类(通过通配符来进行方法名称的匹配) -->
    
<tx:advice id="txAdvice" transaction-manager="txManager">
        
<tx:attributes>
            
<!-- 对于以find开头的方法配置了只读型的事务(开发时统一方式命名) -->
            
<tx:method name="query*" propagation="REQUIRED" read-only="false" />
            
<tx:method name="add*" propagation="REQUIRED" read-only="false" />
            
<tx:method name="*" rollback-for="org.siyn.foo.exception.SystemException" propagation="REQUIRED" read-only="false" />
        
</tx:attributes>
    
</tx:advice>
    
    
<!-- 为事务管理定义切面-->
    
<aop:config proxy-target-class="true">
        
<aop:pointcut id="fooServiceMethods" expression="execution(* org.siyn.foo.service.*.*(..))" />
        
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods" />
    
</aop:config>
    
<!-- ================ 配置 事务管理 END ================ -->

    
<!-- ====================== datasource & sessionfactory =========================== -->

</beans>
测试类
public class MarkItemServiceImplTest extends TestCase
{
    ApplicationContext context     
= null;
    
private IFooService fooService = null;
    
    
protected void setUp() throws Exception
    
{
        
super.setUp();
        context 
= new ClassPathXmlApplicationContext("spring_debug.xml");
        fooService 
= (IFooService) context.getBean("fooService");
    }

    
    
protected void tearDown() throws Exception
    
{
        
super.tearDown();
        fooService 
= null;
        context 
= null;
    }

    
    
public void TestAddMarkItem()
    
{
        Bean bean 
= new Bean();
        bean.setId(
16);
        bean.setTitle(
"title 1 level--log");
        bean.setRemark(
"test--log");

        fooService.addFoo(bean);
    }


}

运行结果

Foo.add中回滚,Bean没添加到数据库,但是日志却添加了(这里只考虑操作成功才记录日志)。大家帮我想想,什么地方错了或有什么好的方法,谢谢!!



------------------------------

欢迎光临: http://www.siyn.org

posted on 2008-07-24 14:07 siyn 阅读(812) 评论(1)  编辑  收藏 所属分类: java

评论

# re: [求救]一个spring2声明式事务管理的问题 2008-07-27 20:35 siyn
怎么没人回一下呢,自己顶一哈  回复  更多评论
  


标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 
相关链接:
网站导航: