栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring配置AOP,事务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring配置AOP,事务

       在开发中,为了给业务方法中增加日志记录,权限检查,事务控制等功能,此时我们需要去修改业务方法代码,考虑到代码的重用性,我们可以考虑使用 OOP 的继承或组合关系来消除重复,但是无论怎么样,我们都会在业务方法中纵向地增加这些功能方法的调用代码。此时,既不遵循开闭原则,也会为后期系统的维护带来很大的麻烦。(即不管怎样都得修改到原来的代码)为了解决该问题,OOP 思想是不行了,得使用 AOP 思想。

(学习之后的总结)

Spring配置AOP有xml和注解两种方法  xml配置AOP

  在AOP包里面新建AOP(事务)对象

public class MyAspect {
    
    public void before(){
        System.out.println("前置通知");
    }

    public void afterReturning(){
        System.out.println("后置通知");
    }
    
    //环绕通知
    public Object around(ProceedingJoinPoint around) throws Throwable {         

        System.out.println("环绕通知之前");
        Object proceed = around.proceed();          //执行目标方法
        System.out.println("环绕通知之后");
        return proceed;                             //将目标方法的返回值返回
    }

    public void after(){
        System.out.println("最终通知");
    }

    public void afterThrowing(){
        System.out.println("异常抛出通知");
    }

}

 

 在 applicationContext.xml 里面配置。

 
   
   

    
    
    

    
    
        
            
            
            
            
            
            
            
            
            
            
            
            
        
    

 

aop配置注意事项

在我们开发中通知顺序应该为:

  1. 前置通知

  2. 环绕通知之前

  3. 目标方法执行

  4. 环绕通知之后

  5. 后置通知

  6. 最终通知

但是如果在applicationContext.xml配置顺序如果不一样则会导致不同的情况,因此必须严格控制在spring配置文件中配置的通知顺序。

 注解配置AOP

 注解说明

@Aspect    作用:把当前类声明为切面类。贴在方法上(切面)
@Order(10)   作用:表示顺序,越小越早执行
@Pointcut("execution(* com.bohang.service.impl.*ServiceImpl.*(..))")  作用:指定切入点表达式
@Before("txPointCut()")  作用:把当前方法表示为前置通知
@AfterReturning("txPointCut()")   作用:把当前方法表示为正常执行的后置通知
@AfterThrowing("txPointCut()")   作用:把当前方法表示为异常时执行的通知
@After   作用:把当前方法表示为最终通知,不管报不报异常都会执行
@Around  作用:把当前方法表示为环绕通知

applicationContext.xml 文件配置 

 

    

    
    

 切面类

@Component
@Aspect
public class AnnoMyAspect {

    @Pointcut("execution(* com.bohang.service.impl.*ServiceImpl.*(..))")
    public void AnnoMyAspect(){}
    
    @Before("AnnoMyAspect()")             //引用切点
    public void before(){
        System.out.println("前置通知");
    }

    @AfterReturning("AnnoMyAspect()")
    public void afterReturning(){
        System.out.println("后置通知");
    }

    //环绕通知
    @Around("AnnoMyAspect()")
    public Object around(ProceedingJoinPoint around) throws Throwable {         

        System.out.println("环绕通知之前");
        //执行目标方法
        Object proceed = around.proceed();          
        System.out.println("环绕通知之后");
        //将目标方法的返回值返回
        return proceed;                             
    }

    @After("AnnoMyAspect()")
    public void after(){
        System.out.println("最终通知");
    }

    @AfterThrowing("AnnoMyAspect()")
    public void afterThrowing(){
        System.out.println("异常抛出通知");
    }
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/658328.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号