`
zhjb2000
  • 浏览: 58315 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

Spring AOP 示例

阅读更多

解释就懒得写了,有代码运行一下就知道了,jar除了Spring所必备的以外还要加上bsh-1.2b7.jar

  1. packagecom.test;
  2. /**
  3. *在线图书销售系统业务逻辑接口
  4. */
  5. publicinterfaceBookBiz{
  6. publicfloat[]buy(StringuserName,StringbookName,doubleprice);
  7. publicvoidcomment(StringuserName,Stringcomments);
  8. }

  1. packagecom.test;
  2. publicclassBookBizImplimplementsBookBiz{
  3. /**
  4. *购买图书
  5. */
  6. publicfloat[]buy(StringuserName,StringbookName,doubleprice){
  7. System.out.println("业务方法buy开始执行");
  8. System.out.println("·"+userName+"购买图书:"+bookName);
  9. System.out.println("·"+userName+"增加积分:"+(int)(price/10));
  10. System.out.println("·"+"向物流系统下发货单");
  11. System.out.println("业务方法buy结束");
  12. returnnull;
  13. }
  14. /**
  15. *发表书评
  16. */
  17. publicvoidcomment(StringuserName,Stringcomments){
  18. System.out.println("业务方法comment开始执行");
  19. System.out.println("·"+userName+"发表书评"+comments);
  20. System.out.println("业务方法comment结束");
  21. }
  22. }
  1. packagecom.test;
  2. importjava.lang.reflect.Method;
  3. importjava.util.Arrays;
  4. importorg.aopalliance.intercept.MethodInterceptor;
  5. importorg.aopalliance.intercept.MethodInvocation;
  6. importorg.springframework.aop.AfterReturningAdvice;
  7. importorg.springframework.aop.MethodBeforeAdvice;
  8. importbsh.Interpreter;
  9. publicclassMyAdviceimplementsMethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor{
  10. /**
  11. *前通知
  12. */
  13. publicvoidbefore(Methodm,Object[]args,Objecttarget)
  14. throwsThrowable{
  15. System.out.println("前通知,调用的方法:"+m.getName()+",参数:"+Arrays.toString(args));
  16. }
  17. /**
  18. *后通知
  19. */
  20. publicvoidafterReturning(Objectonject,Methodmethod,Object[]args,
  21. Objectarg3)throwsThrowable{
  22. System.out.println("后通知,调用的方法:"+method.getName()+",参数:"+Arrays.toString(args));
  23. }
  24. /**
  25. *环绕通知,最维强大的通知,可以控制目标方法是否执行,也可以改变方法的返回值
  26. */
  27. publicObjectinvoke(MethodInvocationmethod)throwsThrowable{
  28. System.out.println("[环绕通知]");
  29. Object[]args=method.getArguments();
  30. /**
  31. *这里我们禁止李四发表任何的评论
  32. */
  33. if(method.getMethod().getName().equals("comment")&"李四".equals(args[0])){
  34. System.out.println("屏蔽李四所有的评论");
  35. StringreturnType=method.getMethod().getReturnType().getName();
  36. if("int".equals(returnType)||"long".equals(returnType)||"float".equals(returnType)||"double".equals(returnType)||"byte".equals(returnType)||"short".equals(returnType)){
  37. //利用BeanShell构造一个内置变量返回,这里想了好久,没有想到什么方法可以根据
  38. //指定数据类型返回指定的变量
  39. Interpreteri=newInterpreter();
  40. returni.eval("("+returnType+")0");
  41. }elseif("boolean".equals(returnType)){
  42. returnfalse;
  43. }
  44. returnnull;
  45. }else{
  46. returnmethod.proceed();
  47. }
  48. }
  49. }
  1. packagecom.test;
  2. importorg.springframework.context.ApplicationContext;
  3. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  4. publicclassAOPTest{
  5. /**
  6. *@paramargs
  7. */
  8. publicstaticvoidmain(String[]args){
  9. ApplicationContextcontext=
  10. newClassPathXmlApplicationContext("springAop.xml");
  11. BookBizbookBiz=(BookBiz)context.getBean("bookBiz");
  12. bookBiz.buy("张三","Spring深入潜出",50);
  13. bookBiz.comment("李四","《恐怖世界》一点都不恐怖,很好看!");
  14. bookBiz.comment("张三","《Spring深入潜出》还是写得不错的!");
  15. }
  16. }
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
  3. <beans>
  4. <beanid="bookBizTarget"class="com.test.BookBizImpl"/>
  5. <beanid="myAdvice"class="com.test.MyAdvice"/>
  6. <beanid="bookBiz"class="org.springframework.aop.framework.ProxyFactoryBean">
  7. <propertyname="proxyInterfaces">
  8. <value>com.test.BookBiz</value>
  9. </property>
  10. <propertyname="interceptorNames">
  11. <list>
  12. <value>myAdvice</value>
  13. </list>
  14. </property>
  15. <propertyname="target"ref="bookBizTarget"/>
  16. </bean>
  17. </beans>

运行AOPTest 这个类,下面是输出结果:

[环绕通知]
前通知,调用的方法:buy,参数:[张三, Spring深入潜出, 50.0]
业务方法buy开始执行
·张三购买图书:Spring深入潜出
·张三增加积分:5
·向物流系统下发货单
业务方法buy结束
后通知,调用的方法:buy,参数:[张三, Spring深入潜出, 50.0]
[环绕通知]
屏蔽李四所有的评论
[环绕通知]
前通知,调用的方法:comment,参数:[张三, 《Spring深入潜出》还是写得不错的!]
业务方法comment开始执行
·张三发表书评《Spring深入潜出》还是写得不错的!
业务方法comment结束
后通知,调用的方法:comment,参数:[张三, 《Spring深入潜出》还是写得不错的!]

从输出结果可以看出,环绕通知是最先开始执行的,,如果环绕通知把目标方法屏蔽了不执行,那么后面的前后通知都不会执行

分享到:
评论

相关推荐

    SpringAOP示例讲解

    `SpringAop.ppt`文件很可能包含了一个详细的讲解,涵盖了Spring AOP的基本概念、配置方式、使用注解声明切面、基于XML的配置以及如何自定义切面。PPT通常会通过图表、代码示例和流程图来帮助理解复杂的概念,使得...

    Spring AOP示例

    这个“Spring AOP示例”包含了一个具体的实践案例,帮助我们更好地理解和应用Spring AOP。 在Spring AOP中,核心概念有以下几个: 1. **切面(Aspect)**:切面是关注点的模块化,比如事务管理就是一个切面。在...

    Spring Aop 示例

    以下是一个简单的Spring AOP示例,展示如何使用注解定义切面和通知: ```java // 定义切面 @Aspect @Component public class LoggingAspect { // 定义切入点,匹配所有以'execute'开头的方法 @Pointcut(...

    spring aop简单应用示例

    本示例将深入探讨Spring AOP的基础知识,以及如何在实际应用中使用它。 首先,我们来看"LogProfilter.java",这很可能是实现一个日志拦截器的类。在Spring AOP中,这样的类通常被称为切面(Aspect)。切面是封装了...

    spring aop示例

    在这个"spring aop示例"中,我们看到了如何使用Spring AOP来实现方法执行前打印方法名和参数的功能。这主要涉及到三个方面:AOP的基本概念、注解的使用以及Spring的自动注入。 首先,AOP的核心概念包括切面(Aspect...

    Spring AOP完整例子

    Spring AOP(面向切面编程)是Spring框架的核心特性之一,它允许开发者在不修改源代码的情况下,通过插入切面来增强或改变程序的行为。在本教程中,我们将深入探讨Spring AOP的不同使用方法,包括定义切点、通知类型...

    SpringAOP:Spring AOP示例

    SpringAOP Spring AOP(面向方面​​的编程)用于模块化“横截面”服务。 用一种简单的方式,我们可以说它是一个旨在拦截某些进程的组件,例如,在执行某个方法时,Spring AOP可以审核该执行方法,并在该方法执行...

    Spring AOP 1.0示例

    在这个“Spring AOP 1.0示例”中,我们重点关注如何在实际项目中应用这一特性。 首先,我们需要了解AOP的基本概念。AOP的核心思想是将那些影响多个类的公共行为(如日志记录)抽取出来,形成独立的模块,称为切面...

    Spring AOP面向方面编程原理:AOP概念

    接下来,我们通过一个简单的Spring AOP示例来加深对上述概念的理解。假设我们需要在调用某个公共方法前记录日志,我们可以定义一个`BeforeAdvice`,并在目标方法上应用此通知。 ```java package com.example.aop; ...

    spring aop 示例

    7. **实战应用**:在提供的示例中,`spring-aop-demo`项目可能包含了以下内容: - `pom.xml`:Maven配置文件,确保依赖了Spring AOP和其他必要的库。 - `src/main/java`:包含切面类、业务类和其他组件的Java源...

    spring aop API示例

    在这个"spring aop API示例"中,我们将深入探讨如何利用Spring AOP的四种通知类型:Before、After、AfterThrowing和Around,以及它们在实际开发中的应用。 1. **Before通知**: 在方法执行前触发,可以用来执行...

    spring aop demo 两种实现方式

    压缩包中的"aop"文件可能包含了一个简单的Spring AOP示例项目,包括了上述两种实现方式的源代码和配置文件。下载后,可以直接运行以观察AOP如何工作。 总结来说,Spring AOP提供了一种强大的方式来实现横切关注点,...

    Spring AOP简单demo

    **Spring AOP示例** 以入门级的`advice`为例,我们可能有一个简单的日志切面: ```java @Aspect @Component public class LoggingAspect { @Pointcut("execution(* com.example.service.*.*(..))") public void ...

    Spring AOP实现 项目源码 Myeclipse 直接导入可用

    项目的源码包含了一个简单的Spring AOP示例,可能包括以下几个部分: 1. `Aspect`类:包含了切面逻辑的类,通常有多个通知方法。 2. `Service`类:业务逻辑类,切面将会影响到的方法。 3. `Spring配置文件`:定义了...

    springAop.rar_AOP java_cglib_spring aop

    在提供的压缩包文件中,"www.pudn.com.txt"可能是下载来源的说明或者包含一些额外的信息,而"springAop"可能是一个包含Spring AOP示例代码的文件。对于学习和理解Spring AOP以及CGLIB的用法,分析这个文件的内容将...

    简单spring aop 例子

    本示例将简要介绍如何在Spring应用中实现AOP,通过实际的代码示例帮助理解其工作原理。 首先,我们要理解AOP的核心概念。AOP是一种编程范式,它允许开发者定义“切面”(Aspects),这些切面封装了特定的关注点,如...

    SpringAOPExample:使用 TestNG 的 Spring AOP 示例

    **使用TestNG运行Spring AOP示例** 在给定的"SpringAOPExample"中,使用`mvn clean test`命令来运行项目。Maven是一个流行的项目管理和依赖管理工具,它会执行clean生命周期阶段以删除已生成的目标文件,然后执行...

    Spring AOP精讲

    package com.ascenttech.springaop.test; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class TestBeforeAdvice implements MethodBeforeAdvice { public void ...

    javaXML方式实现SpringAop编程(源码+jar包)

    接下来,我们将使用XML配置来创建一个简单的Spring AOP示例: 1. **配置Spring容器**:首先,我们需要一个Spring配置文件(如`springstudy02/spring-context.xml`),在其中定义bean并启用AOP支持。例如: ```xml ...

    Spring AOP代码示例

    Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的一个重要组成部分。它提供了一种模块化和声明式的方式来处理系统中的交叉关注点,如日志、性能监控、安全性、事务管理等。通过AOP,...

Global site tag (gtag.js) - Google Analytics