Spring中AOP切入点表达式配置方式

<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 https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="accountService" class="cn.gpxxg.service.Impl.AccountServiceImpl"></bean>

    <!--配置loger通知类-->
    <bean id="loger" class="cn.gpxxg.utils.Loger"></bean>

    <!--配置aop-->
    <aop:config>
        <!--
            aop:pointcut用于配置公共的切面表达式
            id:用于执行唯一的id名称
            expression:切面表达式的内容
            注意:
                1、此标签写在aop:aspect标签内部时,只有当前切面使用
                2、写在aop:aspect上方时,所有切面都可使用
                3、不可写在aop:aspect下方,因为当前xml的约束不允许
        -->
        <aop:pointcut id="p1" expression="execution(* cn.gpxxg.service.Impl.*.*(..))"></aop:pointcut>

        <!--配置切面使用aop:aspect标签-->
        <aop:aspect id="logAdvice" ref="loger">
            <!--
                1、配置通知的类型 before 前置通知
                2、建立通知方法和切入点方法的关联
                    2.1 通知方法 method
                    2.2 切入点 pointcut
                        书写格式:execution (修饰符 返回值类型 全限定类名.方法名())
                            标准写法:public void cn.gpxxg.service.Impl.AccountServiceImpl.save()
                        通配符简化书写:
                            修饰符可以省略:void cn.gpxxg.service.Impl.AccountServiceImpl.save()
                            返回值类型可以使用*来代表所有返回值类型:* cn.gpxxg.service.Impl.AccountServiceImpl.save()
                            包名可以使用通配符,但有几级包,就要写几个:* *.*.*.*.AccountServiceImpl.save()
                            包名可以使用..表示当前包及其子包:* *..AccountServiceImpl.save()
                            类名和方法名可以使用*表示所有:* *..*.*()
                            参数列表:可以直接写基本数据类型,但引用数据类型需要写包名.类名的方式,如:java.lang.String
                            全通配符的写法,* *..*.*(..)有无参数均可,可以是任意数据类型
                            但日常不这样写,实际开发中切入点只会切换到service业务层的所有方法下,即:* cn.gpxxg.service.Impl.*.*(..)

            -->
            <!--前置通知,在切入点方法之前先执行-->
            <aop:before method="beforePrintLog" pointcut-ref="p1"></aop:before>

            <!--后置通知,在切入点方法之后执行,它和异常通知只会执行一个-->
            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="p1"></aop:after-returning>

            <!--异常通知,在遇到异常时执行,它和后置通知只会执行一个-->
            <aop:after-throwing method="afterthrowingPrintLog" pointcut-ref="p1"></aop:after-throwing>

            <!--最终通知,不管有没有异常都会执行-->
            <aop:after method="afterPrintLog" pointcut-ref="p1"></aop:after>

        </aop:aspect>
    </aop:config>

</beans>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值