转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自【大学之旅_谙忆的博客】

如果你把此种纯Java方式实现AOP拦截读懂了,理解本篇博客会更容易。
【框架】[Spring]纯Java的方式实现AOP切面(拦截)技术

这篇讲解的是用xml配置文件来实现AOP拦截。
其实也挺简单的,无非是把一些对象通过xml文件配置new出来与初始化里面的一些值。

需要的包什么的就不解释了,直接给个网址:
http://repo.springsource.org/libs-release-local/org/springframework/spring/

项目结构图

直接上代码

1、准备好原型对象:

package cn.hncu.xmlImpl;public class Person {public void run(){System.out.println("我在run...");}public void run(int i){System.out.println("我在run"+i+"...");}public void say(){System.out.println("我在say...");}}

2、准备好代理类

代理动作什么的都会写在这里,为了方便,只实现MethodInterceptor接口,里面的invoke是最强的。

package cn.hncu.xmlImpl;import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;public class AroundAdvice implements MethodInterceptor{@Overridepublic Object invoke(MethodInvocation invocation) throws Throwable {System.out.println("前面拦截....");Object resObj = invocation.proceed();//放行System.out.println("后面拦截.....");return resObj;}}

3、配置好xml文件:

把切点和通知配置成 切面的外部bean

取名为:1.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- 代理前原对象 --><bean id="person" class="cn.hncu.xmlImpl.Person"></bean><!-- 切点 --><bean id="cut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"><property name="pattern" value=".*run.*"></property></bean><!-- 通知-由我们写,实际代理动作 --><bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean><!-- 切面 = 切点+通知 --><bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"><property name="pointcut" ref="cut"></property><property name="advice" ref="advice"></property></bean><!-- 代理工厂 --><bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 放入原型对象 --><property name="target" ref="person"></property><!-- 放入切面 --><property name="interceptorNames"><list><value>advisor</value></list></property></bean></beans>

把切点和通知配置成 切面的内部bean

<beans ..>
<!-- 代理前原对象 --><bean id="person" class="cn.hncu.xmlImpl.Person"></bean><!-- 切面 = 切点+通知 --><bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"><!-- 切点 --><property name="pointcut"><bean class="org.springframework.aop.support.JdkRegexpMethodPointcut"><property name="pattern" value=".*run.*"></property></bean></property><!-- 通知-由我们写,实际代理动作 --><property name="advice"><bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean></property></bean><!-- 代理工厂 --><bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 放入原型对象 --><property name="target" ref="person"></property><!-- 放入切面 --><property name="interceptorNames"><list><value>advisor</value></list></property></bean>
</beans>

直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置

<beans...>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"><!-- 代理前原对象 --><bean id="person" class="cn.hncu.xmlImpl.Person"></bean><!-- 切面 = 切点+通知 --><bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><!-- 切点 --><property name="patterns"><list><value>.*run.*</value></list></property><!-- 通知-由我们写,实际代理动作 --><property name="advice"><bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean></property></bean><!-- 代理工厂 --><bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 放入原型对象 --><property name="target" ref="person"></property><!-- 放入切面 --><property name="interceptorNames"><list><value>advisor</value></list></property></bean></beans>
</beans>

4、测试方法:

@Test//把切点和通知配置成 切面的外部beanpublic void demo1(){ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/1.xml");Person p =ctx.getBean("personProxied",Person.class);p.run();p.say();}@Test//把切点和通知配置成 切面的内部beanpublic void demo2(){ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/2.xml");Person p =ctx.getBean("personProxied",Person.class);p.run();p.say();}@Test//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置public void demo3(){ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/3.xml");Person p =ctx.getBean("personProxied",Person.class);p.run();p.say();}

输出结果:

这是通过定义JdkRegexpMethodPointcut切入点的方式来实现AOP,通过这种编程方式,可以针对业务方法进行包装或者监控。

但是这个JdkRegexpMethodPointcut还不是很好,它拦截的单位是类!而无法细分到方法。不过放心,后面还有3中切点技术~
用AspectJExpressionPointcut切点就可以实现专门对什么方法进行拦截。后面会有专门介绍与实例的。

本文章由[谙忆]编写, 所有权利保留。

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自【大学之旅_谙忆的博客】

【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut相关推荐

  1. Spring xml配置式的拦截器

    传统的xml配置式使用拦截器拦截请求. 一.Web.xml配置 在Web.xml 配置Spring核心控制器DispatcherServlet接收所有请求. <servlet><se ...

  2. Spring框架中XML配置特殊属性注入

    Spring框架中XML配置特殊属性注入 前言 创建测试类 其他类型属性 前言 Spring框架中,在通过set方式进行属性注入时,可能会遇到某些特殊字符的注入,例如:null和某些标签字符" ...

  3. 基础XML配置的AOP与基于注解实现的AOP详细对比

    aop思想通过动态代理,一定程度上实现了代码的解耦.我们可以将日志模块,权限校验,事物等模块,与核心的业务代码相隔离,让程序员在写代码的时候,能够更加专注于业务. 基于注解实现AOP步骤: ①将目标类 ...

  4. Spring xml 配置使用外部config 文件

    Spring xml 配置使用外部config 文件 当使用Spring framework后, 我们一般会把db connection的信息写在spring的bean config xml里面. 例 ...

  5. java aop xml配置_spring AOP使用 xml配置

    目前有一个业务层的类,我想要监控业务层这个类里面的某些方法.把其中的一些操作记录到一个log表中,但是又不想侵入代码,所以采用切面来完成 一.首先记录log的类: packagecom.aoptest ...

  6. [转] Spring XML配置十二个最佳实践

    Spring是一个强大的JAVA应用框架,广泛地应用于JAVA的应用程序.为Plain Old Java Objects(POJOs)提供企业级服务.Spring利用依赖注入机制来简化工作,同时提高易 ...

  7. spring.xml配置类属性--喜闻乐见

    相信大家在开发的过程中,都会写一些配置文件或者配置类来,毕竟好的编码习惯是不能硬编码的,所以配置文件和配置类就显得很重要了.但是我用久了之后发现,配置文件和配置类确实好用,但是假如有多个配置的话,那么 ...

  8. Spring+Spring Mvc+Mybatis+MySqlite(SSM框架整合Xml配置)

    MyBatis Spring-mvc的对应配置 Log的配置 MyBatis 我们在resources下创建spring-mybatis.xml,对应的参数配置 <?xml version=&q ...

  9. 1.3—Spring基础配置—3.AOP

    1.点睛: AOP:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一组类共享相同的行为.在OOP中只能通过继承类和实现接口,来使代码的耦合度增强,AO ...

最新文章

  1. zabbix服务无法启动
  2. 神经网络有什么理论支持? 本文作者:AI研习社 2017-11-08 18:30 导语:问:神经网络有什么理论支持? 答:目前为止(2017 年)没有什么特别靠谱的。 雷锋网按:本文原作者袁洋
  3. spark比java快吗_为什么我的Spark DataFrame比RDD慢得多?
  4. Linux下arp用法
  5. dotnet不是内部或外部的命令,也不是可运行的程序或批处理文件
  6. 计算机学业水平考试及格,信息技术学业水平考试表格部分试题(带答案)
  7. python中组合框_如何从SQL填充组合框数据?使用Python
  8. 索贝非编改bug定位
  9. 方法用于ThinkPHP3.1快速入门连贯操作
  10. webgis之geowebcache跨域
  11. 50.magento 订单状态
  12. java中cplex程序_Cplex中文教程全 PDF 下载
  13. Vivado使用流程(文字总结)
  14. 20155311《网络对抗》信息搜集与漏洞扫描
  15. 今日睡眠质量记录74
  16. PHP即充宝v3.0实例
  17. 台式计算机怎么查是32位还是64位,Win7系统怎么看电脑是32位还是64位?
  18. 区块链与ICO,了解一下
  19. 基恩士KV7500,基恩士触摸屏,搭载KV-SH04PL四轴运动控制模块,KV-C32XDT.
  20. 电脑网页打不开提示错误err connection怎么办?

热门文章

  1. 【常见Web应用安全问题】
  2. get pid and kill
  3. eclipse new creation file type
  4. 7号团队-团队任务3:每日例会(2018-11-29)
  5. luogu P1330 封锁阳光大学
  6. mysql 数据表操作 存储引擎介绍
  7. zoj How Many Shortest Path
  8. POJ 1611 The Suspects
  9. SDWebImage实现原理(怎么实现图片缓存器)
  10. StringBuffer类和String 类的 equals 和 ==