AOP简单的核心代码

编程步骤:annotation

1.    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

2.加切面@Aspect

3.加组件@Component

1.    加切入点@Pointcut ("execution(public* com.service..*.add(..))")

Xml:方法一:

<bean id= "logInterceptor" class="com.*.aop.LogInterceptpr"></bean>

    <!-- 切面配置 -->

    <aop:config>

    <!-- 指定切入点表达式,指定哪些目标对象或对象方法被切入 -->

        <aop:pointcut expression="execution(public *com.*.service..*.add(..))" id="servicePointcut"/>

        <!-- 指定logAspect为切面组件 -->

        <aop:aspect id="logAspect" ref="logInterceptor">

           <aop:before method="before" pointcut-ref="servicePointcut"/>

        </aop:aspect>

  </aop:config>

方法二:

<!-- 切面配置 -->

    <aop:config>

        <aop:aspect id="logAspect" ref="logInterceptor">

           <aop:before method="before" pointcut="execution(public* com.*.service..*.add(..))"/>

        </aop:aspect>

    </aop:config>

Annotation:

@Aspect

@Component

publicclass LogInterceptpr {

       @Pointcut ("execution(public* com.bjsxt.service..*.add(..))")//用方法定义切入点的名字

       publicvoid myMethod(){};

       @Before("myMethod()")

       publicvoid before(){

              System.out.println("method start");

       }

       @AfterReturning("myMethod()")

       //@AfterThrowing("myMethod()")

      

       publicvoid afterReturning(){

              System.out.println("method after returning");

       }

       @Around("myMethod()")

       publicvoid around(ProceedingJoinPointpjp) throws Throwable{

              System.out.println("method around start");

              pjp.proceed();

              System.out.println("method around end");

       }

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值