1 环境搭建

1.1 第一步:准备必要的代码和 jar 包

1.2 第二步:在配置文件中导入 context 的名称空间



 
    
    
        
        
        
        
    
 
    
    
        
        
        
        
    

1.3 第三步:把资源使用注解配置

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
 
    @Autowired
    private IAccountDao accountDao;
}
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    @Autowired
    private DBAssit dbAssit ;
}

 1.4 第四步:在配置文件中指定 spring 要扫描的包


2 配置步骤

2.1 第一步:把通知类也使用注解配置

@Component("txManager")
public class TransactionManager {
 
    //定义一个 DBAssit
    @Autowired
    private DBAssit dbAssit ;
}

2.2 第二步:在通知类上使用@Aspect 注解声明为切面

@Component("txManager")
@Aspect//表明当前类是一个切面类
public class TransactionManager {
 
    //定义一个 DBAssit
    @Autowired
    private DBAssit dbAssit ;
}

2.3 第三步:在增强的方法上使用注解配置通知

2.3.1 @Before
//开启事务
@Before("execution(* com.itheima.service.impl.*.*(..))")
public void beginTransaction() {
 
    try {
        dbAssit.getCurrentConnection().setAutoCommit(false);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
2.3.2 @AfterReturning
//提交事务
@AfterReturning("execution(* com.itheima.service.impl.*.*(..))")
public void commit() {
 
    try {
        dbAssit.getCurrentConnection().commit();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
2.3.3 @AfterThrowing
//回滚事务
@AfterThrowing("execution(* com.itheima.service.impl.*.*(..))")
public void rollback() {
 
    try {
        dbAssit.getCurrentConnection().rollback();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
2.3.4 @After
//释放资源
@After("execution(* com.itheima.service.impl.*.*(..))")
public void release() {
 
    try {
        dbAssit.releaseConnection();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

2.4 第四步:在 spring 配置文件中开启 spring 对注解 AOP 的支持


3 环绕通知注解配置 @Around

/**
* 环绕通知
* @param pjp
* @return
*/
@Around("execution(* com.itheima.service.impl.*.*(..))")
public Object transactionAround(ProceedingJoinPoint pjp) {
 
    //定义返回值
    Object rtValue = null;
 
    try {
    
        //获取方法执行所需的参数
        Object[] args = pjp.getArgs();
    
        //前置通知:开启事务
        beginTransaction();
 
        //执行方法
        rtValue = pjp.proceed(args);
 
        //后置通知:提交事务
        commit();
        }catch(Throwable e) {
 
        //异常通知:回滚事务
        rollback();
        e.printStackTrace();
    }finally {
 
    //最终通知:释放资源
    release();
    }
 
    return rtValue;
}

4 切入点表达式注解 @Pointcut

@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
private void pt1() {}
 
    /**
    * 引用方式:
    * 环绕通知
    * @param pjp
    * @return
    */
    @Around("pt1()")//注意:千万别忘了写括号
    public Object transactionAround(ProceedingJoinPoint pjp) {
 
    //定义返回值
    Object rtValue = null;
 
    try {
        //获取方法执行所需的参数
        Object[] args = pjp.getArgs();
 
        //前置通知:开启事务
        beginTransaction();
        
        //执行方法
        rtValue = pjp.proceed(args);
 
        //后置通知:提交事务
        commit();
    }catch(Throwable e) {
 
        //异常通知:回滚事务
        rollback();
        e.printStackTrace();
    }finally {
 
        //最终通知:释放资源
        release();
    }
    
    return rtValue;
}

5 不使用 XML 的配置方式

@Configuration
@ComponentScan(basePackages="com.itheima")
@EnableAspectJAutoProxy
public class SpringConfiguration {
 
}
本文转载于:https://www.yisu.com/zixun/782805.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。