spring 学习笔记(AOP)

一、spring 的aop思想分析如下图:
这里写图片描述

二、aop 解释
1.1 Spring中的aop
Spring能够为容器中管理的对象生成动态代理对象
以前我们要使用动态代理,我们需要自己调用下面的方法生成对象Proxy.newProxyInstance(xx,xx,xx) 生成代理对象,现在Spring能够帮我们生成代理对象。
1.2 Spring实现aop原理

  • 动态代理(优先)

被代理对象必须要实现实现接口,才能产生代理对象。如果没有接口不能使用动态代理技术。

这里写图片描述
- cglib代理(没有接口使用)

第三方代理技术,cglib代理,可以对任何类实现代理,代理的原理是对目标对象进行继承代理。如果目标对象被final修饰,该类无法被cglib代理。
这里写图片描述

1.3 aop名词解释

  • JoinPoint(连接点):在目标对象中,所有可以增强的方法
    这里写图片描述
  • PointCut(切入点):目标对象,已经增强的方法
  • Advice(通知/增强):增强的代码
  • Target(目标对象):被代理对象 (也就是userServiceImpl)
  • WeAVing(织入):将通知应用到切入点的过程
  • Proxy(代理):将通知织入到目标对象之后,形成代理对象
    这里写图片描述
  • Aspect(切面):切入点+通知 (即invoke方法)
    这里写图片描述
    2.aop代码 演示
    2.1 导包
    (4+2+2(spring_aop+spring_aspects)+2(com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar;com.springsource.org.aopalliance-1.0.0.jar))
    这里写图片描述
    2.2 准备目标对象
public class UserServiceImpl implements UserService{
    @Override
    public void save() {
        System.out.println("保存");
    }
    @Override
    public void delete() {
        System.out.println("删除");
    }
    @Override
    public void update() {
        System.out.println("更新");
    }
    @Override
    public void find() {
        System.out.println("查找");
    }
}

2.3 准备通知(增强的代码)

public class  notice{
    //前置通知,方法名是自己定义的
    public void before() {
        System.out.println("前置通知!");
    }
    //后置通知
    public void afterReturning() {
        System.out.println("后置通知(如果出现异常不会调用)");
    }
    //环绕通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕通知之前的部分!");
        Object proceed = pjp.proceed();               
        System.out.println("这是环绕通知之后的部分!");
        return proceed;
    }
    //异常通知
    public void afterException() {
        System.out.println("异常出现了!");
    }
    //后置通知
    public void after() {
        System.out.println("后置通知(出现异常也会调用)");
    }
}

2.4 配置进行织入,将通知织入目标对象中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
    <!-- 配置目标对象  -->
    <bean name="UserService"  class="com.tz.service.Impl.UserServiceImpl"></bean>
    <!-- 配置通知对象 -->
    <bean name="myadvice"  class="com.tz.aop.Myadvice"></bean>
    <!-- 3.配置将通知织入切入点对象 -->
    <aop:config>
        <!-- 配置切入点  -->
        <!--public void com.tz.service.Impl.UserServiceImpl.save()
            void com.tz.service.Impl.UserServiceImpl.save()
            * com.tz.service.Impl.UserServiceImpl.save()
            * com.tz.service.Impl.UserServiceImpl.*()


            * com.tz.service.Impl.*ServiceImpl.*(..)  ((..)对方法的参数不做要求)
            * com.tz.service.Impl..*ServiceImpl.*(..)
         -->
        <aop:pointcut expression="execution(* com.tz.service.Impl.*ServiceImpl.*(..))" id="pc"/>
        <aop:aspect ref="myadvice">
            <!-- 指定为before方法作为前置通知 -->
            <aop:before method="before" pointcut-ref="pc"/>
            <!-- 环绕通知 -->
            <aop:around method="around" pointcut-ref="pc"/>
            <!-- 异常拦截通知 -->
            <aop:after-throwing method="afterException" pointcut-ref="pc"/>
            <!-- 后置 -->
            <aop:after-returning method="afterReturning" pointcut-ref="pc"/>
             <!-- 后置 -->
            <aop:after method="after" pointcut-ref="pc"/> 
        </aop:aspect>
    </aop:config>   

</beans>

2.5 测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/tz/aop/ApplicationContext.xml")
public class test02 {

    @Resource(name="UserService")
    private UserService us;

    @Test
    public void  aopTest(){
        //调用保存方法
        us.save();

    }
输出:
  前置通知!
  环绕通知之前的部分!
  保存
  这是环绕通知之后的部分!

三、总结
AOP 面向切面编程(不懂的可以看上面的图 ),spring封装了动态代理和cglib代理的代码,可以对任何类进行代理。要熟悉aop的名词含义。以上就是Spring aop的详细分析,有什么问题可以评论或者联系我 qq:2217087909。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行走在江湖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值