`
leiwuluan
  • 浏览: 697118 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Spring中常用三种通知

阅读更多

(1)前置通知

接口:org.springframework.aop.MethodBeforeAdvice

使用前置通知可以在联结点执行前进行自定义的操作。不过,Spring里只有一种联结点,即方法调用,所以前置通知事实上就是让你能在方法调用前进行一些操作。前置通知可以访问调用的目标方法,也可以对该方法的参数进行操作,不过它不能影响方法调用本身。

 

示例:

 

IHello接口

public interface IHello {

    public void hello(String name);

}

 

IHello接口实现

public class HelloSpeaker implements IHello {

    public void hello(String name) {

        System.out.println("Hello, " + name);

    }

}

 

IHello实现代理

public class LogBeforeAdvice implements MethodBeforeAdvice {

    private Logger logger = Logger.getLogger(this.getClass().getName());

   

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

        logger.log(Level.INFO, "method starts..." + method);

   }

}

 

Spring配置文件

    <bean id="logBeforeAdvice"

          class="onlyfun.caterpillar.LogBeforeAdvice"/>

 

    <bean id="helloSpeaker"

          class="onlyfun.caterpillar.HelloSpeaker"/>

   

    <bean id="helloProxy"

          class="org.springframework.aop.framework.ProxyFactoryBean">

        <property name="proxyInterfaces">

            <value>onlyfun.caterpillar.IHello</value>

        </property>

        <property name="target">

            <ref bean="helloSpeaker"/>

        </property>

        <property name="interceptorNames">

            <list>

                <value>logBeforeAdvice</value>

            </list>

        </property>

    </bean>

 

 

测试类

public class SpringAOPDemo {

    public static void main(String[] args) {

        ApplicationContext context =

                new FileSystemXmlApplicationContext(

                        "beans-config.xml");

        IHello helloProxy =

            (IHello) context.getBean("helloProxy");

        helloProxy.hello("Justin");

    }

}

 

(2)后置通知(或返回后通知)

接口:org.springframework.aop.AfterReturningAdvice

后置通知在联结点处的方法调用已经完成,并且已经返回一个值时运行,后置通知可以访问调用的目标方法,以及该方法的参数和返回值。因为等到通知执行时该方法已经调用,后置通知完全不能影响方法调用本身。

 

示例:

 

IHello接口

public interface IHello {

    public void hello(String name);

}

 

IHello接口实现

public class HelloSpeaker implements IHello {

    public void hello(String name) {

        System.out.println("Hello, " + name);

    }

}

 

IHello实现代理

public class LogAfterAdvice implements AfterReturningAdvice {

    private Logger logger = Logger.getLogger(this.getClass().getName());

   

    public void afterReturning(Object object, Method method,

                               Object[] args,

                               Object target) throws Throwable {

        logger.log(Level.INFO, "method ends..." + method);

   }

}

 

Spring配置文件

    <bean id="logAfterAdvice"

          class="onlyfun.caterpillar.LogAfterAdvice"/>

 

    <bean id="helloSpeaker"

          class="onlyfun.caterpillar.HelloSpeaker"/>

   

    <bean id="helloProxy"

          class="org.springframework.aop.framework.ProxyFactoryBean">

        <property name="proxyInterfaces">

            <value>onlyfun.caterpillar.IHello</value>

        </property>

        <property name="target">

            <ref bean="helloSpeaker"/>

        </property>

        <property name="interceptorNames">

            <list>

                <value>logAfterAdvice</value>

            </list>

        </property>

    </bean>

 

测试类

public class SpringAOPDemo {

    public static void main(String[] args) {

        ApplicationContext context = new FileSystemXmlApplicationContext(

                        "beans-config.xml");

        IHello helloProxy = (IHello) context.getBean("helloProxy");

        helloProxy.hello("Justin");

    }

}

 

 

 

(3)包围通知

接口:org.aopalliance.intercept.MethodInterceptor

Spring中的包围通知模AOP联盟的“方法拦截器”标准。包围通知可以在目标方法之前和之后动行,我们也可以定义在什么时候调用目标方法。如果需要,我们也可以另写自己的逻辑而完全不调用目标方法。

 

示例:

 

IHello接口

public interface IHello {

    public void hello(String name);

}

 

 

 

IHello接口实现

public class HelloSpeaker implements IHello {

    public void hello(String name) {

        System.out.println("Hello, " + name);

    }

}

 

IHello实现代理

public class LogInterceptor implements MethodInterceptor {

    private Logger logger = Logger.getLogger(this.getClass().getName());

   

   public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        logger.log(Level.INFO, "method starts..." + methodInvocation.getMethod());

        

        Object result = null;

        try {

          result = methodInvocation.proceed();

        }

        finally {

            logger.log(Level.INFO, "method ends..." +

               methodInvocation.getMethod() + "\n");

        }

        return result;

   }

 

Spring配置文件

    <bean id="logInterceptor"

          class="onlyfun.caterpillar.LogInterceptor"/>

   

    <bean id="helloSpeaker"

          class="onlyfun.caterpillar.HelloSpeaker"/>

   

    <bean id="helloProxy"

          class="org.springframework.aop.framework.ProxyFactoryBean">

        <property name="proxyInterfaces">

            <value>onlyfun.caterpillar.IHello</value>

        </property>

        <property name="target">

            <ref bean="helloSpeaker"/>

        </property>

        <property name="interceptorNames">

            <list>

                <value>logInterceptor</value>

            </list>

        </property>

    </bean>

 

测试类

public class SpringAOPDemo {

    public static void main(String[] args) {

        ApplicationContext context = new FileSystemXmlApplicationContext(

                        "beans-config.xml");

        IHello helloProxy = (IHello) context.getBean("helloProxy");

        helloProxy.hello("Justin");

    }

 

 

分享到:
评论

相关推荐

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    如何在Spring Boot中使用@Before注解

    这是一种常用的AOP通知类型,常用于日志记录、安全性检查等方面。 下面详细解释如何在Spring Boot中使用@Before注解,并提供一个完整的实例和代码来演示其用法。在实例中,我们将创建一个简单的用户认证功能,并在...

    Spring攻略(第二版 中文高清版).part1

    13.8 使用Spring的常用测试注解 540 13.8.1 问题 540 13.8.2 解决方案 540 13.8.3 工作原理 541 13.9 小结 542 第14章 Spring Portlet MVC框架 544 14.1 用Spring Portlet MVC开发一个简单的Portlet ...

    Spring攻略(第二版 中文高清版).part2

    13.8 使用Spring的常用测试注解 540 13.8.1 问题 540 13.8.2 解决方案 540 13.8.3 工作原理 541 13.9 小结 542 第14章 Spring Portlet MVC框架 544 14.1 用Spring Portlet MVC开发一个简单的Portlet ...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Java EE设计模式:Spring企业级开发最佳实践

    在 Java EE 设计模式中,常用的设计模式有: 1.Singleton Pattern:单例模式,确保某个类只有一个实例,并提供一个全局访问点。 在 Spring 中,singleton scope 是默认的 scopes,表示 Bean 只会被实例化一次,...

    springCloud

    Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种...

    Spring面试题详细讲解

    Spring 中常用的设计模式达到九种,我们举例说明: 工厂模式(Factory Method)通常由应用程序直接使用 new 创建新的对象,为了将对象的创建和使用相分离,采用工厂模式,即应用程序将对象的创建及初始化职责交给...

    基于Vue+SpringCloud博客的设计与实现 有论文

    用户会员中心:SVIP与VIP,定时任务/RabbitMQ延迟队列/登录验证三种判定会员截止时间到期用邮箱去提醒 用户支付中心:我的钱包和支付宝支付以及打印我的账单,内网穿透获得异步通知作为结果判定标志,原始支付的普通...

    Spring笔记(面试题)md

    1.什么是Spring框架 2.Spring的特点 3.什么是IOC ...13.AOP常用的术语 14.什么是AspectJ框架 15.AspectJ常见通知类型 16.AspectJ 的切入点表达式(掌握) 17.AspectJ的前置通知@Before .... .... ....

    spring第四天.pdf

    10. 认识Spring AOP中底层常用的一些核心类 11. 源码阅读之查找aop相关的BeanDefinitionParser流程课程目标 1. 了解什么是AOP? 2. 了解AOP能干什么? 3. 了解AOP都有哪些实现? 4. 掌握AOP核心概念(通知、切入点、...

    如何使用Spring Boot的@Pointcut注解

    总的来说,@Pointcut注解是Spring Boot AOP中非常有用的一部分,它允许我们轻松地定义切点并应用通知来实现面向切面编程。通过这种方式,我们可以有效地在应用程序中实现横切关注点的功能,提高代码的可维护性和重用...

    77道Spring面试题以及参考答案(2024年最新版)

    4. 注解部分列举了常用的Spring注解及其作用,如@Component、@Autowired等。 5. 数据访问部分涉及了Spring对JDBC、Hibernate等数据访问技术的集成支持。 6. AOP部分对面向切面的核心概念如通知、连接点、切点等进行...

    spring AOP(声明式事务管理)小程序

    用spring AOP(包括几种常用的通知类型)做的小程序

    基于 SpringBoot、Spring Security、JWT 的前后端分离的通用权限管理系统,个人学习,快速搭建模板

    - 后端采用Spring Boot、Spring Security、Redis & Jwt。 - 权限认证使用Jwt,支持多终端认证系统。 - 支持加载动态权限菜单,多方式轻松权限控制。 - 高效率开发,使用代码生成器可以一键生成前后端代码。 ## 功能...

    spring boot校园失物招领系统

    - 后端采用Spring Boot、Spring Security、Redis & Jwt。 - 权限认证使用Jwt,支持多终端认证系统。 - 支持加载动态权限菜单,多方式轻松权限控制。 内置功能 1. 用户管理:用户是系统操作者,该功能主要完成系统...

    Spring AOP代码示例

    Spring AOP 常用于日志记录,性能统计,安全控制,事务处理,异常处理等等,本项目对其常用语法进行示例。

    pig权限管理系统是一个基于 Spring Cloud Hoxton 、 OAuth2 的 RBAC 权限管理系统.rar

    模块简介 基于 IJPay 让支付触手可及,封装了微信支付、支付宝支付常用的支付方式以及各种常用的接口,提供渠道管理、订单管理、商品订单、回调管理等功能 协同办公工作流 模块简介 基于activiti 最经典版本5.22 ...

    基于SpringBoot,Spring Security,JWT,Vue & Element 的前后端分离多商户商城管理系统

    后端采用Spring Boot、Spring Security、Redis & Jwt。 权限认证使用Jwt,支持多终端认证系统。 支持加载动态权限菜单,多方式轻松权限控制。 高效率开发,使用代码生成器可以一键生成前后端代码。 内置功能 用户...

    java常用面试题及答案

    多态的好处,代码中如何实现多态 ...Spring中有哪些类型的通知(Advice)? Spring Boot 的核心配置文件有哪几个?它们的区别是什么? springboot集成mybatis的过程 springcloud如何实现服务的注册和发现

Global site tag (gtag.js) - Google Analytics