日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

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

發布時間:2024/4/17 asp.net 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載請注明出處: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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。