五种通知方式来实现aop

五种通知方式来实现aop

五种通知包括:

1. 前置通知,在业务方法之前执行
2. 后置通知,在业务方法之后执行
3. 环绕通知,同时在业务方法的前后执行
4. 带有返回值通知,可以拿到业务方法的返回值
5. 异常通知,可以捕获业务方法中的异常对象
第一种方法实现配置文件配置
public class MyAspect {


    public void before(JoinPoint jp){

        System.out.println("-----------before---------------");
        System.out.println(jp.toString());
    }


    public void after(){

        System.out.println("------------after------------");
    }


    public Object around(ProceedingJoinPoint pjp){
        try{

            System.out.println("around before");
            Object obj=pjp.proceed();
            System.out.println("around after "+obj);

            return obj;
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
        return null;
    }


    public void myReture(JoinPoint JP,Object obj){
        System.out.println("this is myReturn " +obj);
    }

    public void myThrow(JoinPoint jp,Throwable e){
        System.out.println("this is myThrow " +e.getMessage());
    }
}

配置文件

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">


    <bean id="us" class="com.xj.aop05.UserService"/>
    <bean id="ma" class="com.xj.aop05.MyAspect"/>

    <aop:config proxy-target-class="true">
        <aop:aspect ref="ma">
            <aop:pointcut id="mpc" expression="execution(* com.xj.aop05.*.*(..))"/>
            
            <aop:before method="before" pointcut-ref="mpc"/>
            <aop:after method="after" pointcut-ref="mpc"/>
            <aop:around method="around" pointcut-ref="mpc"/>
            <aop:after-returning method="myReture" pointcut-ref="mpc" returning="obj"/>
            <aop:after-throwing method="myThrow" pointcut-ref="mpc" throwing="e"/>
        </aop:aspect>
    </aop:config>
</beans>

注意:如果同时配置所有的通知方式,则指向顺序依次为:

before>around before >业务方法>after returning >around after >after

before>around before>业务方法>after throwing>after

第二种注释

**
 *  以注解的方式实现的切面类MyAspect
 *
 *      当前类中的五种通知方式均以注解方式完成
 */
@Component          //  标注当前类为一个组件
@Aspect             //  标注当前类为一个切面类
public class MyAspect {

    /**
     * @Pointcut 注解为了避免相同的匹配规则被定义多处,专门定义该方法设置执行的匹配规则,各个自行调用即可
     *    write once, only once
     */
    @Pointcut(value = "execution(* com.qfedu.aop06.*.*(..))")
    public void setAll(){}


    /**
     * @Before 表示该方法为一个前置通知
     * @param jp 连接点
     */
    @Before("setAll()")
    public void myBefore(JoinPoint jp){
        //System.out.println(jp.getArgs());
        System.out.println("this is before.");
    }

    /**
     * @After 表示该方法为一个后置通知
     * @param jp 连接点
     */
    @After("setAll()")
    public void myAfter(JoinPoint jp){
        //System.out.println(jp.getArgs());
        System.out.println("this is after.");
    }

    /**
     * @Around 表示该方法为一个环绕通知
     * @param pjp 处理连接点
     * @return 返回每个业务方法的返回值
     */
    @Around("setAll()")
    public Object myAround(ProceedingJoinPoint pjp){
        Object obj = null;
        try {
            System.out.println("this is around before");

            obj = pjp.proceed();


            System.out.println("this is around after");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        return obj;
    }

    /**
     * @AfterReturning 表示该方法为一个带有返回值的通知
     * @param jp 连接点
     * @param obj 业务方法的返回值
     */
    @AfterReturning(value = "setAll()", returning = "obj")
    public void myReturn(JoinPoint jp, Object obj){
        System.out.println("this is after returnning " + obj);
    }

    /**
     * @AfterThrowing 表示该方法为一个带有异常的通知
     * @param jp 连接点
     * @param e Throwable对象
     */
    @AfterThrowing(value = "setAll()", throwing = "e")
    public void myThrowing(JoinPoint jp, Throwable e){
        System.out.println("this is after throwing " + e.getMessage());
    }
}
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--
    context:component-scan 组件扫描
        base-package指定要扫描的包的路径
    -->
    <context:component-scan base-package="com.qfedu.aop06" />

    <!--aop:aspectj-autoproxy标签实现自动代理-->
    <aop:aspectj-autoproxy />
</beans>

注:使用注解的话是环绕通知proceed方法之前部分先执行,使用xml配置的话取决于aop:before和aop:around的配置顺序

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容