SpringAop的简单使用

1、xml版配置aop

1.1 配置前后置通知

<!-- 配置自定义事物类 -->
<bean id="txManage" class="cn.itsource._03_aopxml.TxManage"/>

<!--配置aop-->
<aop:config>
    <!--配置切点(这里面就是需要进行添加自定义代码的地方)-->
    <aop:pointcut id="txAop" expression="execution(* cn.itsource._03_aopxml.service.I*Service.*(..))"/>

    <!--配置切面(里面包含需要执行的自定义代码)-->
    <aop:aspect ref="txManage">
        <!--在这个切点(地方),执行这个事物类里的begin方法-->
        <aop:before method="begin" pointcut-ref="txAop"/>
        <!--后置通知-->
        <aop:after-returning method="commit" pointcut-ref="txAop"/>
        <!--异常通知-->
        <aop:after-throwing method="rollback" pointcut-ref="txAop" throwing="e"/>
        <!--最终通知-->
        <aop:after method="close" pointcut-ref="txAop"/>

        <!--环绕通知-->
        <!--<aop:around method="around" pointcut-ref="txAop"/>-->
    </aop:aspect>
</aop:config>

1.2 配置环绕通知(里面就包含前后置通知)

<bean id="txManage" class="cn.itsource._03_aopxml.TxManage"/>

<!--配置aop-->
<aop:config>
    <!--配置切点-->
    <aop:pointcut id="txAop" expression="execution(* cn.itsource._03_aopxml.service.I*Service.*(..))"/>

    <!--配置切面-->
    <aop:aspect ref="txManage">
    
        <!--环绕通知-->
        <aop:around method="around" pointcut-ref="txAop"/>
    
    </aop:aspect>
</aop:config>


=====================================
//ProceedingJoinPoint :这里面能得到自己的代码
public static void around(ProceedingJoinPoint joinPoint){
        try {
            begin();
            //这能获取到要执行的代码
            joinPoint.proceed();
            commit();
        } catch (Throwable e) {
            e.printStackTrace();
            rollback(e);
        } finally {
            close();
        }
    }

2、注解配置aop

2.1 前后置通知

@Component
@Aspect //切面注解
public class TxManage {

    //配置切点
    @Pointcut("execution(* cn.itsource._04_aopanno.service.I*Service.*(..))")
    public static void  pointcut(){

    }

    //前置通知(需要切点才能定位要在哪个方法之前执行)
    @Before("pointcut()")
    public static void begin(){
        System.out.println("前");
    }

    //后置通知
    @AfterReturning("pointcut()")
    public static void commit(){
        System.out.println("后");
    }

    //异常通知,throwing:这里面的参数,必须和下面传的参数一样
    @AfterThrowing(value = "pointcut()",throwing = "e")
    public static void rollback(Throwable e){
        System.out.println("异常,原因:"+e.getMessage());
    }

    @After("pointcut()")
    public static void close(){
        System.out.println("最终");
    }
}

2.2 环绕通知

@Component
@Aspect //切面注解
public class TxManage {

    //配置切点
    @Pointcut("execution(* cn.itsource._04_aopanno.service.I*Service.*(..))")
    public static void  pointcut(){

    }

    public static void begin(){
        System.out.println("前");
    }

    public static void commit(){
        System.out.println("后");
    }

    public static void rollback(Throwable e){
        System.out.println("异常,原因:"+e.getMessage());
    }

    public static void close(){
        System.out.println("最终");
    }

    @Around("pointcut()")
    public static void around(ProceedingJoinPoint joinPoint){
        try {
            begin();
            //这能获取到要执行的代码
            joinPoint.proceed();
            commit();
        } catch (Throwable e) {
            e.printStackTrace();
            rollback(e);
        } finally {
            close();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP是一种面向切面编程的技术,用于在不修改原始代码的情况下添加横切关注点。Spring AOP提供了一种简单的方法来创建横切关注点,例如日志记录、性能监控、安全性等,这些关注点可以应用于多个类和方法中。 以下是使用Spring AOP的步骤: 1. 添加Spring AOP依赖 在Maven项目中,可以通过以下方式添加Spring AOP依赖: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.8</version> </dependency> ``` 2. 创建切面类 切面类是用于定义横切关注点的类。它包含一个或多个通知方法,每个方法在目标方法执行前、执行后或出现异常时执行。通知方法可以使用@Before、@After、@AfterReturning、@AfterThrowing和@Around注解进行标记。 ```java @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.myapp.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Before executing " + joinPoint.getSignature().getName() + " method"); } @AfterReturning(pointcut = "execution(* com.example.myapp.service.*.*(..))", returning = "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { System.out.println("After executing " + joinPoint.getSignature().getName() + " method with result " + result); } @AfterThrowing(pointcut = "execution(* com.example.myapp.service.*.*(..))", throwing = "ex") public void logAfterThrowing(JoinPoint joinPoint, Exception ex) { System.out.println("Exception thrown in " + joinPoint.getSignature().getName() + " method with message " + ex.getMessage()); } } ``` 3. 配置切面 在Spring配置文件中,需要将切面类声明为一个bean,并配置切入点和通知。 ```xml <bean id="loggingAspect" class="com.example.myapp.aspect.LoggingAspect"/> <aop:config> <aop:aspect ref="loggingAspect"> <aop:pointcut expression="execution(* com.example.myapp.service.*.*(..))" id="serviceMethods"/> <aop:before pointcut-ref="serviceMethods" method="logBefore"/> <aop:after-returning pointcut-ref="serviceMethods" method="logAfterReturning" returning="result"/> <aop:after-throwing pointcut-ref="serviceMethods" method="logAfterThrowing" throwing="ex"/> </aop:aspect> </aop:config> ``` 4. 应用切面 在需要应用横切关注点的类或方法上添加@Aspect注解。 ```java @Service public class MyService { @Autowired private MyRepository repository; @Transactional public void save(MyEntity entity) { repository.save(entity); } } ``` 以上就是使用Spring AOP的基本步骤。通过切面类、切入点和通知,可以方便地实现横切关注点,并将其应用于多个类和方法中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值