首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

AOP简略的核心代码

2013-03-01 
AOP简单的核心代码编程步骤:annotation1.aop:aspectj-autoproxy/aop:aspectj-autoproxy2.加切面@Aspec

AOP简单的核心代码

编程步骤:annotation

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

2.加切面@Aspect

3.加组件@Component

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

Xml:方法一:

<beanid= "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");

       }

}


热点排行