1. 程式人生 > >Spring筆記5-基於配置檔案的方式配置AOP

Spring筆記5-基於配置檔案的方式配置AOP

<beans>

    <!--配置普通的bean-->
    <bean id="testAspectImpl" class="com.kcj.test.TestAspectImpl"></bean>

    <!--配置切面bean-->
    <bean id="loggingAspect" class="com.kcj.test.LoggingAspect"></bean>

    <!--配置AOP-->
    <aop:config>
        <!--配置切點表示式-->
<aop:pointcut id="pointcut" expression="execution(* com.kcj.test.*.*(..))"></aop:pointcut> <!--配置切面及通知--> <aop:aspect ref="loggingAspect" order="1"> <aop:before method="before" pointcut-ref="pointcut"/> <aop:after method
="after" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/> <aop:after-throwing method="afterThorwing" pointcut-ref="pointcut" throwing="e"/> </aop:aspect> </aop:config> </beans
>
package com.kcj.test;
public class TestAspectImpl implements TestAspect{
    @Override
    public int add(int i, int j) {
        return 0;
    }
}
package com.kcj.test;
import org.aspectj.lang.JoinPoint;
public class LoggingAspect {
    public void declareJoinPointExxcution(){
    }
    public void before(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
    }
    public void after(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
    }
    public void afterReturning(JoinPoint joinPoint, Object result) {
    }
    public void afterThorwing(JoinPoint joinPoint, Exception e) {
    }

}