`
sunqitang
  • 浏览: 74804 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spirng aop(非注释方式)

    博客分类:
  • SSH
阅读更多

   在某些时候,我们工程中使用的JDK 不一定就是1.5 以上,也就是说可能不支持Annotation 注解,这时自然也就不能使用@AspectJ 注解驱动的AOP 了,那么如果我们仍然想使用AspectJ 灵活的切入点表达式,那么该如何呢?Spring 为我们提供了基于xml schematic 的aop 命名空间,它的使用方式和@AspectJ 注解类似,不同的是配置信息从注解中转移到了Spring 配置文件中。在这里,我们将详细介绍如何使用Spring 提供的<aop:config/> 标签来配置Spring AOP 。


1 、一点准备工作和一个例子

    使用<aop:config/> 标签,需要给Spring 配置文件中引入基于xml schema 的Spring AOP 命名空间。完成后的Spring 配置文件如下(在该节,所有例程的配置文件中添加了Spring AOP 命名空间,除非特殊情况外,为了节约空间,这部分将在给出的代码中省略),粗体内容即为我们需要添加的内容

 

  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < beans   xmlns = "http://www.springframework.org/schema/beans"   
  3.         xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  4.         xmlns:aop = "http://www.springframework.org/schema/aop"   
  5.         xsi:schemaLocation ="http://www.springframework.org/schema/beans  
  6.               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  7.               http://www.springframework.org/schema/aop   
  8.               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd >   
  9. ………… Spring配置信息  
  10. </ beans >   

    关于aop命名空间的标签,我们前面使用过的有<aop:aspectj-autoproxy/>,在这一节,我们将以<aop:config/>标签作为重点。事实上,我们在这一节介绍的所有标签都是该标签的子标签。


   下面有一个例程来直观的展示如何使用<aop:config/>标签来配置Spring AOP(完整代码见例程4.15)。在例子中,我们使用<aop:config/>配置一个切面并拦截目标对象Peoples的 SayHello()方法,在它执行前输出提示信息。
首先创建工程AOP_Test4.15,添加Spring IoC和Spring AOP库后,创建aop.test包,新建目标类People,代码如下:

 

代码    查看源代码 打印
  1. package  aop.test;  
  2.   
  3. /**  
  4.  * 该类将作为目标对象对应的类。  
  5.  * @author zhangyong  
  6.  * */   
  7. public   class  People{  
  8.   
  9.         public  String SayHello(String str){  
  10.                 System.out.println(this .getClass().getName()+  "说:" +str);  
  11.                 return  str;  
  12.         }  
  13. }  

    修改Spring xml配置文件,将该类注册为一个受管Bean:

 

代码    查看源代码 打印
  1. < bean   id = "TestBean"   class = "aop.test.People"   />   

    创建含有main()方法的测试类TestMain,从Spring IoC容器中获取Peoples对象,并调用其SayHello()方法,代码如下:

 

代码    查看源代码 打印
  1. package  aop.test;  
  2.   
  3. // import省略   
  4. public   class  TestMain {  
  5.         public   static   void  main(String[] args) {  
  6.                 // 实例化Spring IoC容器   
  7.                 ApplicationContext ac = new  ClassPathXmlApplicationContext(  
  8.                                 "applicationContext.xml" );  
  9.                 // 获取受管Bean的实例   
  10.                 People p = (People) ac.getBean("TestBean" );  
  11.                 p.SayHello("传入的参数值" );  
  12.         }  
  13. }  

   创建MyAspect类,添加一个beforeAdvice()方法作为前置通知方法,代码如下:

 

代码    查看源代码 打印
  1. package  aop.test;  
  2.   
  3. import  org.aspectj.lang.JoinPoint;  
  4.   
  5. public   class  MyAspect {  
  6.           
  7.         public   void  beforeAdvice(JoinPoint point) {  
  8.             System.out.println("前置通知被触发:"  +   
  9.                                 point.getTarget().getClass().getName()+   
  10.                                 "将要"  + point.getSignature().getName());  
  11.         }  
  12. }  

    修改xml配置文件,为其添加aop命名空间,并把MyAspect注册为一个受管Bean,作为我们下面定义切面的backing bean。代码如下:

 

代码    查看源代码 打印
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < beans   xmlns = "http://www.springframework.org/schema/beans"   
  3.         xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  4.         xmlns:aop = "http://www.springframework.org/schema/aop"   
  5.         xsi:schemaLocation ="http://www.springframework.org/schema/beans  
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  7.                http://www.springframework.org/schema/aop   
  8. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">   
  9.   
  10.         < bean   id = "MyAspect"   class = "aop.test.MyAspect"   />   
  11.         < bean   id = "TestBean"   class = "aop.test.People"   />   
  12.           
  13.         < aop:config   proxy-target-class = "true" >   
  14.                 < aop:aspect   ref = "MyAspect"   order = "0"   id = "Test" >   
  15.                         < aop:pointcut   id = "testPointcut"   
  16.                                 expression = "execution(* aop..*(..))"   />   
  17.                         < aop:before   pointcut-ref = "testPointcut"   
  18.                                 method = "beforeAdvice"   />   
  19.                 </ aop:aspect >   
  20.         </ aop:config >   
  21. </ beans >   

    运行主类,输出如下:

例程4.15输出结果

分享到:
评论

相关推荐

    java springAOP 事务+注释

    java springAOP 事务+注释 带全部jar包! 即下即用!

    Spring Aop的简单实现

    一个基于配置文件的Spring AOP的实现。实现了前置通知,后置通知,以及拦截器的功能,配置中有详细的注释。

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...

    spring注解aop配置详解

    最近使用了springAOP编程,文档里面包含了springAOP的代码示例及详细注释说明,使用的是注解配置模式

    spring-aop-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...

    spring AOP注解的应用1

    关于AOP注解前置通知、后置通知、返回通知、异常通知的注解注释及应用

    spring-aop-5.3.15-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....

    spring-aop-5.3.12-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...

    Spring AOP 权限

    Spring 权限 这里是一个 spring aop 实现的一个权限 包括权限设计 这个不是很能懂 包括注释

    spring-aop-5.3.10-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...

    spring-aop-5.1.3.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.1.3.RELEASE.jar; 赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom;...

    spring-aop-4.2.2.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-4.2.2.RELEASE.jar; 赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom;...

    Spring AOP--日志管理

    Spring AOP--日志管理,注释齐全,欢迎大家共同交流。

    spring-aop-5.3.15-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...

    spring-aop-4.3.20.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-4.3.20.RELEASE.jar 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...

    Spring源码解析4章150页+Spring3.2.4中文注释源码

    一阶段 1、Spring概述 2、一切从bean开始 ...Spring AOP的涉及原理及具体实践 SpringJDBC的涉及原理及二次开发 SpringMVC框架设计原理及手写实现 四阶段 Spring事务源码解析 需要其他源码请私信我

    spring-aop-5.3.7-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.7.jar; 赠送原API文档:spring-aop-5.3.7-javadoc.jar; 赠送源代码:spring-aop-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.7.pom; 包含翻译后的API文档:spring-aop...

    spring-aop-5.2.15.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.15.RELEASE.jar; 赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE....

    spring注解方式实现aop

    spring注解方式实现aop,希望对大家有所帮助,内附注释

Global site tag (gtag.js) - Google Analytics