Spring基本操作之AOP

aop的底层是动态代理 作用是在不改变原有代码的基础上,添加新的功能,切面编程。不熟悉动态代理的同学可以查看这篇文章

动态代理点击查看


原理

将要添加的功能通过Spring提供的方法(动态代理),织入在所需方法之前或之后 共有三种方式实现 来看看吧

1.使用Spring的api接口实现aop

目标类

public interface Rent {
    void rent();
}
//真实类
public class RentImpl implements Rent{
    @Override
    public void rent() {
        System.out.println("租了一套房");
    }
}

将要织入的类实现接口

将要织入的类 实现AfterReturningAdvice或MethodBeforeAdvice接口,重写其中方法。该方法就可以织入到目标类的前方或后方

public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("方法名"+method.getName()+"返回内容"+returnValue);
    }
}
public class BeforLog implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName()+"正在使用方法"+method.getName()+"参数为"+ Arrays.toString(objects));
    }
}

在xml中配置

注入目标类和要植入的类

<bean id="rentImpl" class="RentImpl"/>
   <bean id="befor" class="BeforLog"/>
   <bean id="after" class="AfterLog"/>

切入

    <aop:config>
      <!--execution(返回值 类名 方法名 参数)-->
      <!-- 切入点 expression 表达式 execution 执行的位置-->
      <aop:pointcut id="pointcut" expression="execution(* RentImpl.*(..))"></aop:pointcut>
      <!--执行环绕增加-->
      <aop:advisor advice-ref="befor" pointcut-ref="pointcut"/>
      <aop:advisor advice-ref="after" pointcut-ref="pointcut"/>
   </aop:config>

2.使用自定义类实现aop 切面定义

定义要织入的类

public class ABLog {
    public void befor(){
        System.out.println("正在启动日志 开始记录");
    }
    public void after(){
        System.out.println("关闭日志 正在保存记录");
    }
}

在xml中配置

注入目标类和要植入的类

   <bean id="rentImpl" class="RentImpl"/>
      <bean id="ablog" class="ABLog"/>

切面织入

 <aop:config>
      <!--定义切面 ref要使用的类 -->
      <aop:aspect ref="ablog">
         <!--切入点-->
         <aop:pointcut id="pointcut" expression="execution(* RentImpl.*(..))"/>
         <!--通知-->
         <aop:after method="after" pointcut-ref="pointcut"/>
         <aop:before method="befor" pointcut-ref="pointcut"/>
      </aop:aspect>
   </aop:config>

2.注解实现aop

定义类

//只是一个切面
@Aspect
public class AnnotationLog {
    @Before("execution(* RentImpl.*(..))")
    public void befor(){
        System.out.println("日志开始");
    }

    @After("execution(* RentImpl.*(..))")
    public void after(){
        System.out.println("日志结束");
    }

    /**
     *
     * @param pj
     * @throws Throwable
     * 结果是 环绕前 日志开始 日至结束 环绕后
     */
      //在环绕增强中 我们可以给出一个参数来代表 我们要处理切入的点
    @Around("execution(* RentImpl.*(..))")
    public void around(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("环绕前");
       //执行切入点的方法
        Object proceed = pj.proceed();

        System.out.println("环绕后");
    }
}

开启注解

<bean id="annotationLog" class="AnnotationLog"/>
   <!--aop方式三  使用注解定义-->
   <aop:aspectj-autoproxy/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值