【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut
来源:互联网 发布:mac如何退出dashboard 编辑:程序博客网 时间:2024/06/03 14:52
转载请注明出处: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{ @Override public 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://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//把切点和通知配置成 切面的外部bean public void demo1(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/1.xml"); Person p =ctx.getBean("personProxied",Person.class); p.run(); p.say(); } @Test//把切点和通知配置成 切面的内部bean public 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
本文源自【大学之旅_谙忆的博客】
2 0
- 【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut
- 【框架】[Spring]AOP拦截-使用切点:AspectJExpressionPointcut-切点语言
- Spring AOP之切点拦截
- Spring框架:AOP中切点采用aspectj
- spring中的切点(AOP)和事务(ACID)的结合使用 spring.xml的配置
- Spring AOP 之 实现切点、切面
- Spring AOP XML配置实现
- spring aop 切点表达式
- xml实现aop拦截
- spring切点切面aop——xml非注解
- spring切点切面aop——xml非注解
- spring AOP切面开发 基于aspectJ框架切点开发
- Spring 2.0及以上AOP 切点配置整理
- Spring中配置AOP: execution切点函数用法
- Spring框架-AOP细节2(XML配置AOP切面)
- SSH集成框架下真正实现Spring AOP拦截功能
- SSH集成框架下真正实现Spring AOP拦截功能
- 【框架】[Spring]AOP拦截-三种方式实现自动代理
- linux图形界面 安装oracle client
- 与Servlet第一次握手
- 看时钟 联系到PLL,并搜索转载的文章
- 你的Java代码对JIT编译友好么?
- instr 和 substr
- 【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut
- ubuntu 14.04 安装qt
- Python装饰器与面向切面编程
- 职权的合理使用
- 在 Oracle 中查看正在运行中语句
- Oracle 使用DBLink跨服务迁移数据、并解决Lob 字段 ORA-22992 错误
- python print'hello' File "<stdin>",line 1 print 'hello' SyntaxError:invalid syntax
- webservice解析wsdl出错解决办法
- android 自定义listview——实现上拉刷新下拉加载的功能