Spring的aop操作

还是再啰嗦一下,再讲一次。Aop面向切面的编程,在不修改原来代码的情况下给程序添加统一的功能。例如在进行每一个操作之后都需要做日志,这时就可以用aop
一:专业术语:

连接点:类中可以被增强的方法,这些方法称作连接点
切入点:我们当前拦截的方法,就需要增强的方法
通知:拦截到方法之后要做的事情
           前置通知前置通知 before
           后置通知 afterReturning
           最终通知 after
           环绕通知 around
           异常通知 afterThrowing

切面:切入点+通知等于切面

我个人的总结是:
     切入点:要针对哪些方法
     通知:要做什么操作
     切面 = 切入点 + 通知。针对哪些方法做操作

二:开发步骤:
第一步:导入包
这里写图片描述
第二步骤:创建spring核心配置文件,导入aop的约束

<?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">
</beans>

这里写图片描述
第三步:配置文件,具体如何配置看下面【配置文件的方式】
特别注意一点:这里要给大家讲一下execution表达式,通过写execution表达式拦截配置切入点,所以要先学习一下

    //表达式格式
    execution(<访问修饰词>? <返回值类型> <方法名>(<参数>?)<异常>)
    //?也就是访问修饰词0或者1个。可不写。下面举例
    (1)execution(* cn.itcast.aop.Book.add(..))  
    (2)execution(* cn.itcast.aop.Book.*(..))
    (3)execution(* *.*(..))
    (4) 匹配所有save开头的方法 execution(* save*(..))
    <!-- 1  配置对象 -->
    <bean id="book" class="cn.itcast.aop.Book"></bean>
    <bean id="myBook" class="cn.itcast.aop.MyBook"></bean>

    <!-- 2 配置aop操作 -->
    <aop:config>
        <!-- 2.1 配置切入点,拦截Book类下面所有的方法-->
        <aop:pointcut expression="execution(* cn.itcast.aop.Book.*(..))" id="pointcut1"/>

        <!-- 2.2 配置切面 
            把增强用到方法上面
        -->
        <aop:aspect ref="myBook">
            <!-- 配置增强类型 
                method: 增强类里面使用哪个方法作为前置
            -->
            <aop:before method="before1" pointcut-ref="pointcut1"/>
            <aop:after method="after" pointcut-ref="pointcut1"/>
            <aop:after-returning method="after1" pointcut-ref="pointcut1"/>
            <aop:afterThrowing method="afterThrowing" pointcut-ref="pointcut1"/>
            <aop:around method="around1" pointcut-ref="pointcut1"/>
        </aop:aspect>
    </aop:config>
//配置项是固定的。主要是第二步配置

以上配置就可以了.结果如下,结果的每个人自己测试一下就可以了

前置通知    before
后置通知    afterReturning
异常通知    afterThrowing
最终通知    after
环绕通知(前、后)
ProceedingJoinPoint 

以上所有通知都配置上时,执行结果如下:
1,不抛异常:
    == before ==
    == 环绕通知(前) ==
    >> 删除一个User <<
    == after ==
    == afterReturning ==
    == 环绕通知(后) ==
2,抛异常   
    == before ==
    == 环绕通知(前) ==
    >> 查询所有User <<
    == after ==
    == afterThrows ==

四:基于aspectj的注解aop【注解的方式】
第一步 创建对象,并且开启aop操作

<?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">

    <!-- 2 开启aop操作 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!-- 1  配置对象 -->
    <bean id="book" class="cn.itcast.aop.Book"></bean>
    <bean id="myBook" class="cn.itcast.aop.MyBook"></bean>

</beans>

第二步 在增强类上面使用注解完成aop操作

<!--首先声明这是一个切面-->
@Aspect
public class MyBook {
    //设置切点
    //在方法上面使用注解完成增强配置
    @Before(value="execution(* cn.itcast.aop.Book.*(..))")
    public void before1() {
        //这是通知
        System.out.println("before..............");
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值