使用spring的aop模拟事物

本文主要介绍spring aop模拟事物的一些开启,提交,回滚等基本操作,加深对spring aop的理解:

1.定义一个事物切面:

package com.aligns.spring.aop.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


/**
 * 定义切面:模拟事物
 * @author kefan
 *
 */


@Aspect
public class TransactionAspect {
    
    /**
     * 定义切入点
     */
    @Pointcut("execution(* com.aligns.spring.aop.service.*.*(..))")
    public  void  transactionPointCut(){
        
    }
    
    /**
     * 定义前置通知:开启事物
     */
    @Before("transactionPointCut()")
    public  void  beginTransaction(){
        System.out.println("事物开启....");
    }
    
    /**
     * 定义后置通知:提交事物
     */
    @After("transactionPointCut()")
    public  void  CommitTransaction(){
        System.out.println("事物提交....");
    }
    
    /**
     * 定义异常通知:事物回滚
     */
    @AfterThrowing("transactionPointCut()")
    public  void  rollBackTransaction(){
        System.out.println("事物回滚....");
    }
    

}

2.编写对应的业务代码:

package com.aligns.spring.aop.service;

import org.springframework.stereotype.Service;


@Service
public class UserService {
    
    public void addUser(){
        
        System.out.println("用户添加");
        throw new RuntimeException("添加用户出错");
    }
    
    public void updateUser(){
        System.out.println("用户修改");
    }
    
    public void deleteUser(){
        System.out.println("用户删除");
    }

    public void getUser(){
        System.out.println("用户查询");
    }
    
}


3.在spring的配置文件中配置aspectj,定义切面等:


<?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
    default-lazy-init="true">

    <description>Spring Configuration</description>


    <!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 -->
    <context:component-scan base-package="com.aligns.spring"><!-- base-package
            如果多个,用“,”分隔 -->
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    
    <aop:aspectj-autoproxy/>
    
    
    
    <bean id="logAspect" class="com.aligns.spring.aop.aop.TransactionAspect"></bean>

</beans>


4.编写单元测试:

package com.aligns.spring.aop.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.aligns.spring.aop.service.AccountService;
import com.aligns.spring.aop.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath*:spring-context.xml,classpath*:spring-context-jedis.xml")
@ContextConfiguration(locations = { "classpath*:/spring-context-test.xml"})  
@Transactional
public class BaseTest {
    
    @Resource AccountService accountService;
    @Resource UserService userService;
    
    
    @Test
    public void fun1(){
        
        accountService.inAccount();
        accountService.outAccount();
        userService.addUser();
        userService.deleteUser();
        
        System.out.println(accountService);
    }



}


   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章