Spring基于aspectJ切点传统开发

第一步:在spring的配置文件中定义目标与通知

    <!--目标target-->
    <bean id="orderService" class="com.itcast.aop.OrderServiceImpl"></bean>

    <!--通知Advice-->
    <bean id="orderServiceAdvice" class="com.itcast.aop.OrderServiceAdvice"></bean>

第二步:使用aop:xxx标签来完成切面与切点声明

    <!--通过AOP的标签来完成我们的配置-->
    <aop:config>    <!--它帮我们完成自动代理功能-->
        <aop:pointcut id="mypointcut" expression="execution(* com.itcast.aop.OrderServiceImpl.*(..))"></aop:pointcut><!--切点-->
        <aop:advisor advice-ref="orderServiceAdvice" pointcut-ref="mypointcut"></aop:advisor><!--切面-->
    </aop:config>

注意1:需要在xml配置文件中导入aop声明
注意2:因为我们使用aspectj的切面声明方式 需要在导入aspectj的jar包
在这里插入图片描述
< aop:xxx>标签说明:
< aop:config>来声明要对aop进行配置
< aop:pointcut>它是用于声明切点(简单说就是对哪些方法进行拦截)
< aop:advisor> 定义传统的aop的切面,传统的aop切面它只能包含一个切点与一个增强
< aop:aspect>定义aspectj框架的切面.,它可以包含多个切点与多个通知

关于切点表达式写法:

<aop:pointcut id="mypointcut" expression="execution(* com.itcast.aop.OrderServiceImpl.*(..))"></aop:pointcut>

这个语法源于aspectJ的语法,spring中aop开发,对aspectj不是完全支持,只支持部分语法。
在这里插入图片描述
在这里插入图片描述
在开发中使用的比较多的是execution语法.
关于execution语法常用:

  1. execution(public * *()) 所有的public的方法
  2. execution(* cn.itheima.aop..(…)) 所有的aop包下的所有类的方法(不包含子包)
  3. execution(* cn.itheima.aop…*(…)) 所有的aop包及其子包下的所有类的方法
  4. execution(* cn.itheima.aop.IOrderService.*(…)) IOrderService接口中定义的所有方法
  5. execution(* cn.itheima.aop.IOrderService+.*(…)) 匹配实现特定接口所有类的方法
  6. execution(* save*(…)) 区配所有的以save开头的方法

猜你喜欢

转载自blog.csdn.net/Marion158/article/details/85624472
今日推荐