Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

AOP简单的核心代码

$
0
0

编程步骤: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");

       }

}


作者:x1r2p3 发表于2013-2-26 8:56:45 原文链接
阅读:70 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>