`

5.5 Spring的通知(Advice)

 
阅读更多

5.5  Spring的通知(Advice)

Spring提供了5种Advice类型:Interception Around、Before、After Returning、Throw和Introduction。它们分别在以下情况下被调用:在JointPoint前后、JointPoint前、JointPoint后、JointPoint抛出异常时、JointPoint调用完毕后。下面来进行更详细的讲解。

5.5.1  Interception Around通知

Interception Around通知会在JointPoint的前后执行,前面示例中的LogProxy就是一个Interception Around通知,它在考勤审核程序的前后都执行了。Spring中最基本的通知类型便是Interception Around通知。实现Interception Around通知的类需要实现接口MethodInterceptor,示例代码如下:

public class LogInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation invocation) throws Throwable {

        System.out.println(" 开始审核数据...");

        Object rval = invocation.proceed();

        System.out.println(" 审核数据结束…");

        return rval;

    }

}

5.5.2  Before通知

Before通知只在JointPoint前执行,实现Before通知的类需要实现接口MethodBeforeAdvice,示例代码如下:

public class LogBeforeAdvice implements MethodBeforeAdvice {

    public void before(Method m, Object[] args, Object target) throws Throwable {

        System.out.println(" 开始审核数据...");

    }

}

5.5.3  After Returning通知

After Returning通知只在JointPoint后执行,实现After Returning通知的类需要实现接口AfterReturningAdvice,示例代码如下:

public class LogAfterAdvice implements AfterReturningAdvice {

    public void afterReturning (Method m, Object[] args, Object target) throws Throwable {

        System.out.println(" 审核数据结束...");

    }

}

5.5.4  Throw通知

Throw通知只在JointPoint抛出异常时执行,实现Throw通知的类需要实现接口ThrowsAdvice,示例代码如下:

public class LogThrowAdvice implements ThrowsAdvice {

    public void afterThrowing (RemoteException ex) throws Throwable {

        System.out.println(" 审核数据抛出异常,请检查..." + ex);

    }

}

5.5.5  Introduction通知

Introduction通知只在JointPoint调用完毕后执行,实现Introduction通知的类需要实现接口IntroductionAdvisor和接口IntroductionInterceptor。

前面所讲的知识点,更多的是理论,下面的章节将会讲述更多的实例,来帮助读者更好地理解上面的理论知识。

 

5.6  Spring的Advisor

前面讲过,Advisor是Pointcut和Advice的配置器,它是将Advice注入程序中Pointcut位置的代码。org.springframework.aop.support.DefaultPointcutAdvisor是最通用的Advisor类。在Spring中,主要通过XML的方式来配置Pointcut和Advice。

分享到:
评论

相关推荐

    spring2-aop.pdf

    - **通知**(Advice):在特定的连接点执行的动作,可以是在方法调用前后执行的前置通知(Before Advice)、后置通知(After Returning Advice)等。 - **引入**(Introduction):声明额外接口由被通知类实现的...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    5.5. Summary Chapter 6. Rendering web views 6.1. Understanding view resolution 6.2. Creating JSP views 6.2.1. Configuring a JSP-ready view resolver 6.2.2. Using Spring’s JSP libraries 6.3. Defining a...

    spring security 参考手册中文版

    5.5处理注销 36 5.5.1 LogoutHandler 37 5.5.2 LogoutSuccessHandler 37 5.5.3更多注销相关参考 38 5.6认证 38 5.6.1内存认证 38 5.6.2 JDBC认证 39 5.6.3 LDAP认证 39 5.6.4 AuthenticationProvider 41 5.6.5 ...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    《Java EE应用开发基础》第五章课件(PDF版)

    - 通知(Advice): 在连接点执行的操作。 - 切点(Pointcut): 匹配连接点的表达式。 - **优势**: - 将横切关注点从应用程序代码中分离出来。 - 提高了代码的可读性和可维护性。 #### 5.5 Spring事务支持 - **事务...

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    《MyEclipse 6 Java 开发中文教程》前10章

    10.3.2 开发前置通知(Before advice)对象:FBI 212 10.3.3 装配拦截器和Bean 212 10.3.4 测试和运行 214 10.3.5 AOP简介和相关概念 214 10.3.6 关于java.lang.ClassCastException: $Proxy0错误的解决方法 216 10.4...

    MySql存储过程编程.chm

    Section 5.5. Performing Dynamic SQL with Prepared Statements Section 5.6. Handling SQL Errors: A Preview Section 5.7. Conclusion Chapter 6. Error Handling Section 6.1. Introduction to Error ...

Global site tag (gtag.js) - Google Analytics