02-SpringAop

1.基于配置配置文件

切面类

package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {



    public void before(){
        System.out.println("前置增强.......");
    }

    public void afterReturning(){
        System.out.println("后置增强.......");
    }

    /**
     * 环绕方法
     * @param pjp 正在执行的连接点===切点
     */
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强");
        //切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后增强");
        return proceed;
    }


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

    public void after(){
        System.out.println("最终增强");
    }
    

}

目标方法类

package com.itheima.aop;

public class Target implements TargetInterface {
    @Override
    public void save() {
        // int i= 1/0;
        System.out.println("save running....");
    }
}

配置文件

声明Bean,配置<aop:config>,代码内有注释

<!--目标对象-->
    <bean id="target" class="com.itheima.aop.Target"/>
    <!--切面对象-->
    <bean id="myAspect" class="com.itheima.aop.MyAspect"/>
    <!--配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
            <!--抽取切点表达式-->
            <aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
            <!--切面:切点+通知-->
            <!--&lt;!&ndash;<aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.save())"/>&ndash;&gt;-->
            <!--<aop:before method="before" pointcut="execution(* com.itheima.aop.*.*(..))"/>-->
            <!--<aop:after-returning method="afterReturning" pointcut="execution(* com.itheima.aop.*.*(..))"/>-->
            <!--<aop:around method="around" pointcut="execution(* com.itheima.aop.*.*(..))"/>-->
            <!--<aop:after-throwing method="afterThrowing" pointcut="execution(* com.itheima.aop.*.*(..))"/>-->
            <!--<aop:after method="after" pointcut="execution(* com.itheima.aop.*.*(..))"/>-->
            <aop:around method="around" pointcut-ref="myPointcut"/>
        </aop:aspect>


    </aop:config>

2.基于注解

配置文件配置组件扫描和自动代理,然后注解配置bean

    <!--配置扫描-->
    <context:component-scan base-package="com.itheima.anno"/>
    <!--配置aop自动代理-->
    <aop:aspectj-autoproxy/>

切面类

package com.itheima.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {


    //配置前置增强
    @Before("execution(* com.itheima.anno.*.*(..))")
    public void before(){
        System.out.println("前置增强.......");
    }


    @AfterReturning("execution(* com.itheima.anno.*.*(..))")
    public void afterReturning(){
        System.out.println("后置增强.......");
    }

    /**
     * 环绕方法
     * @param pjp 正在执行的连接点===切点
     */
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强");
        //切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后增强");
        return proceed;
    }


    public void afterThrowing(){
        System.out.println("异常抛出增强....");
    }
    @After("MyAspect.pointcut()")
    public void after(){
        System.out.println("最终增强");
    }

    //定义切点表达式
    @Pointcut("execution(* com.itheima.anno.*.*(..))")
    public void pointcut(){}



}

目标方法类 

package com.itheima.anno;

import org.springframework.stereotype.Component;

@Component("target")
public class Target implements TargetInterface {
    @Override
    public void save() {
        // int i= 1/0;
        System.out.println("save running....");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值