spring 注解和xml 两版本实现spring-aop

spring 注解和xml 两版本实现spring-aop

xml

通知类

package com.zho.utils;

import org.aspectj.lang.ProceedingJoinPoint;

public class Logger {

    public void beforePrintLog() {
        System.out.println("Logger.beforePrintLog");
    }

    public void afterReturningPrintLog() {
        System.out.println("Logger.afterReturningPrintLog");
    }

    public void afterThrowingPrintLog() {
        System.out.println("Logger.afterThrowingPrintLog");
    }

    public void afterPrintLog() {
        System.out.println("Logger.afterPrintLog");
    }

    public Object aroundPrintLog(ProceedingJoinPoint point){
        Object proceed = null;
        try {
            System.out.println("Logger.aroundPrintLog---前置");
            Object[] args = point.getArgs();
            proceed  = point.proceed(args);
            System.out.println("Logger.aroundPrintLog---after_returning");
            return proceed;
        } catch (Throwable throwable) {

            System.out.println("Logger.aroundPrintLog---异常");
            throw new RuntimeException(throwable);
        } finally {
            System.out.println("Logger.aroundPrintLog --- return");
        }
    }
}

xml环绕通知

方法需要一个参数ProceedingJoinPoint point

一个Object[]数组接受point中的args

point.proceed(args);执行方法,此方法就相当于明确调用切入点方法.

public Object aroundPrintLog(ProceedingJoinPoint point){
        Object proceed = null;
        try {
            System.out.println("Logger.aroundPrintLog---前置");
            Object[] args = point.getArgs();
            proceed  = point.proceed(args);
            System.out.println("Logger.aroundPrintLog---after_returning");
            return proceed;
        } catch (Throwable throwable) {

            System.out.println("Logger.aroundPrintLog---异常");
            throw new RuntimeException(throwable);
        } finally {
            System.out.println("Logger.aroundPrintLog --- return");
        }
    }
bean配置 aop:config

aop:config 中一个pointcut 切入点表达式

一个aspect 配置被监听的类,实现aop

 <!--    spring中居于xml的aop配置步骤-->
    <bean id="logger" class="com.zho.utils.Logger">
    </bean>

    <aop:config>
        <aop:pointcut id="tx" expression="execution(* com.zho.service.impl.*.*(..) )"/>

        <aop:aspect id="logAdvice" ref="logger">
<!--            <aop:before method="beforePrintLog" pointcut-ref="tx"/>-->
<!--            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="tx"/>-->
<!--            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="tx"/>-->
<!--            <aop:after method="afterPrintLog" pointcut-ref="tx"/>-->
            <aop:around method="aroundPrintLog" pointcut-ref="tx" />
        </aop:aspect>
    </aop:config>
anno

logger配置 需要有Componect注解,加入ioc中

开启Aspect注解 如不在通知类上使用@Aspect则可以在bean中加入

<aop:aspectj-autoproxy/>

开启注解使用实现aop通知开启

anno需要有一个切入点方法

实现通知的方式,在方法上使用注解注明通知方式

@Pointcut("execution(* com.zho.service.impl.*.*(..))")
    private void tx() {
    }
@Component("logger")
@Aspect
public class Logger {
    @Pointcut("execution(* com.zho.service.impl.*.*(..))")
    private void tx() {
    }

//    @Before("tx()")
    public void beforePrintLog() {
        System.out.println("Logger.beforePrintLog");
    }

//    @AfterReturning("tx()")
    public void afterReturningPrintLog() {
        System.out.println("Logger.afterReturningPrintLog");
    }

//    @AfterThrowing("tx()")
    public void afterThrowingPrintLog() {
        System.out.println("Logger.afterThrowingPrintLog");
    }

//    @After("tx()")
    public void afterPrintLog() {
        System.out.println("Logger.afterPrintLog");
    }

    @Around("tx()")
    public Object aroundPrintLog(ProceedingJoinPoint point) {
        Object proceed = null;
        try {
            System.out.println("Logger.aroundPrintLog---前置");
            Object[] args = point.getArgs();
            proceed = point.proceed(args);
            System.out.println("Logger.aroundPrintLog---after_returning");
            return proceed;
        } catch (Throwable throwable) {

            System.out.println("Logger.aroundPrintLog---异常");
            throw new RuntimeException(throwable);
        } finally {
            System.out.println("Logger.aroundPrintLog --- return");
        }
    }
}
配置类

开启spring注解 EnableAspectJAutoProxy

@Configuration
@ComponentScan("com.zho")
@EnableAspectJAutoProxy
public class SpringConfiguration {

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值