Spring笔记3Aop

1.AOP简介

  • aop是一种思想
    AOP为Aspect Oriented Programming的缩写(aop),面向切面编程
  • aop作用
    在不修改目标代码的情况下 对目标方法进行增强 (aop底层技术实现就是动态代理)
  • aop底层实现 动态代理
    • 基于jdk
      条件:目标对象必须有接口(动态生成的代理对象是接口的实现对象)
    • 基于cglib
      不需要接口 就可以完成动态的代理对象的生成
  • aop相关概念
    • 目标对象(target):需要被增强的对象
    • 代理对象(proxy):执行增强的对象
    • 连接点(joinpoint):可以被增强的方法 连接点(方法) 方法级别
    • 切(入)点(pointcut):将要被增强的方法 方法级别
    • 增强(通知)(advice):封装增强业务逻辑 方法级别
    • 切面(aspect):切点+增强 逻辑概念
    • 织入(weaver):将切点和增强结合的过程称为织入
2.基于xml方式的AOP配置
  1. 导入额外的开发包
    与aop相关:
    spring-aop-4.2.4.RELEASE aop核心包
    spring-aspects-4.2.4.RELEASE 切面包
    com.springsource.org.aopalliance-1.0.0 aop联盟
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE aspectj的织入包
  2. 准备类
    目标类:内部有切点
public class Target implements TargetInterface {
    @Override
    public void show() {
        System.out.println("show一下.......");
        }
    }

切面类:内部有增强

public class MyAspect {
    //前置增强
    public void before(){
        System.out.println("前置增强...");
    }
    //后置增强
    public void afterReturning(){
        System.out.println("后置增强...");
    }
}

3.配置
目标对象与切面对象存在与容器中

<!-- 配置目标 -->
<bean id="target" class="com.itheima.aop.Target"></bean>        
<!-- 配置切面实例 -->
<bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>

配置织入:将切点与增强结合 都进入到切面内部

!-- 配置aop的织入 -->
    <aop:config>
    <!-- 指定切面  将容器中名称为myAspect实例指定为切面对象-->
    <aop:aspect ref="myAspect">
    <!-- 切面=切点+增强 -->
    <!-- 配置切点 -->
    <!-- 细节点2:切点表达式的写法
    expression="execution(切点表达式)"
    示例:public void com.itheima.aop.Target.show()
    切点表达式语法:访问修饰符 返回值类型 包.类.方法(参数类型列表)
    通配符配置:
        访问修饰符可以省略
        返回值类型、包、类、方法都可以使用*代表任意
        参数类型列表可以使用..代表任意'
    示例:
    * com.itheima.aop.Target.*()    任意返回值、Target类下的任意无参方法
    * com.itheima.aop.Target.*(..)  任意返回值、Target类下的任意方法
    * com.itheima.aop.*.*(..)       任意返回值、aop包下的任意类的任意方法
    * com.itheima.aop..*.*(..)      任意返回值、aop包及其子包下的任意类的任意方法

-->
<aop:pointcut expression="execution(public void com.itheima.aop.Target.show())" id="myPointcut"/>
<aop:pointcut expression="execution(* com.itheima.aop.Target.*(..))" id="myPointcut2"/>
    <!-- 配置增强 -->
    <!-- 
        细节点1:增强的类型
            aop:before:前置增强
            aop:after-returning:后置增强
            aop:around:环绕通知/增强
            aop:after-throwing:异常抛出增强
            aop:after:最终增强
     -->
    <!-- <aop:before method="before" pointcut-ref="myPointcut"/>
    <aop:after-returning method="afterReturning" pointcut-ref="myPointcut"/> -->
    <aop:around method="around" pointcut-ref="myPointcut"/>
    <aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut"/>
    <aop:after method="after" pointcut-ref="myPointcut2"/>
    </aop:aspect>
</aop:config>
设置spring注解开发,需要设置自动代理
<!-- 开启自动代理 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

3.基于注解方式的AOP配置(半注解半配置文件)

注解配置

//<bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
            @Component("myAspect")
            //<aop:aspect ref="myAspect">
            @Aspect
            public class MyAspect {

                //前置增强
                //aop:pointcut expression="execution(public void com.itheima.aop.Target.show())" id="myPointcut"/>
                //<aop:before method="before" pointcut-ref="myPointcut"/>
                //@Before("execution(public void com.itheima.anno.Target.show())")
                public void before(){
                    System.out.println("前置增强...");
                }
                //后置增强
                //@AfterReturning("execution(public void com.itheima.anno.Target.show())")
                public void afterReturning(){
                    System.out.println("后置增强...");
                }


                //aop:pointcut expression="execution(public void com.itheima.aop.Target.show())" id="myPointcut"/>
                @Pointcut("execution(public void com.itheima.anno.Target.*(..))")
                public void pointcut(){

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值