读书人

AOP简略的核心代码

发布时间: 2013-03-01 18:33:02 作者: rapoo

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

}

}


读书人网 >编程

热点推荐