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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

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

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

轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/qq_26525215

本文源自【大學(xué)之旅_諳憶的博客】

如果你把此種純Java方式實(shí)現(xiàn)AOP攔截讀懂了,理解本篇博客會(huì)更容易。
【框架】[Spring]純Java的方式實(shí)現(xiàn)AOP切面(攔截)技術(shù)

這篇講解的是用xml配置文件來(lái)實(shí)現(xiàn)AOP攔截。
其實(shí)也挺簡(jiǎn)單的,無(wú)非是把一些對(duì)象通過(guò)xml文件配置new出來(lái)與初始化里面的一些值。

需要的包什么的就不解釋了,直接給個(gè)網(wǎng)址:
http://repo.springsource.org/libs-release-local/org/springframework/spring/

項(xiàng)目結(jié)構(gòu)圖

直接上代碼

1、準(zhǔn)備好原型對(duì)象:

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、準(zhǔn)備好代理類

代理動(dòng)作什么的都會(huì)寫(xiě)在這里,為了方便,只實(shí)現(xiàn)MethodInterceptor接口,里面的invoke是最強(qiáng)的。

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文件:

把切點(diǎn)和通知配置成 切面的外部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"><!-- 代理前原對(duì)象 --><bean id="person" class="cn.hncu.xmlImpl.Person"></bean><!-- 切點(diǎn) --><bean id="cut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"><property name="pattern" value=".*run.*"></property></bean><!-- 通知-由我們寫(xiě),實(shí)際代理動(dòng)作 --><bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean><!-- 切面 = 切點(diǎn)+通知 --><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"><!-- 放入原型對(duì)象 --><property name="target" ref="person"></property><!-- 放入切面 --><property name="interceptorNames"><list><value>advisor</value></list></property></bean></beans>

把切點(diǎn)和通知配置成 切面的內(nèi)部bean

<beans ..> <!-- 代理前原對(duì)象 --><bean id="person" class="cn.hncu.xmlImpl.Person"></bean><!-- 切面 = 切點(diǎn)+通知 --><bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"><!-- 切點(diǎn) --><property name="pointcut"><bean class="org.springframework.aop.support.JdkRegexpMethodPointcut"><property name="pattern" value=".*run.*"></property></bean></property><!-- 通知-由我們寫(xiě),實(shí)際代理動(dòng)作 --><property name="advice"><bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean></property></bean><!-- 代理工廠 --><bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 放入原型對(duì)象 --><property name="target" ref="person"></property><!-- 放入切面 --><property name="interceptorNames"><list><value>advisor</value></list></property></bean> </beans>

直接在切面bean中配置“切點(diǎn)的正則表達(dá)式”,省去“切點(diǎn)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"><!-- 代理前原對(duì)象 --><bean id="person" class="cn.hncu.xmlImpl.Person"></bean><!-- 切面 = 切點(diǎn)+通知 --><bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><!-- 切點(diǎn) --><property name="patterns"><list><value>.*run.*</value></list></property><!-- 通知-由我們寫(xiě),實(shí)際代理動(dòng)作 --><property name="advice"><bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean></property></bean><!-- 代理工廠 --><bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 放入原型對(duì)象 --><property name="target" ref="person"></property><!-- 放入切面 --><property name="interceptorNames"><list><value>advisor</value></list></property></bean></beans> </beans>

4、測(cè)試方法:

@Test//把切點(diǎn)和通知配置成 切面的外部beanpublic void demo1(){ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/1.xml");Person p =ctx.getBean("personProxied",Person.class);p.run();p.say();}@Test//把切點(diǎn)和通知配置成 切面的內(nèi)部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中配置“切點(diǎn)的正則表達(dá)式”,省去“切點(diǎn)bean”的配置public void demo3(){ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/3.xml");Person p =ctx.getBean("personProxied",Person.class);p.run();p.say();}

輸出結(jié)果:

這是通過(guò)定義JdkRegexpMethodPointcut切入點(diǎn)的方式來(lái)實(shí)現(xiàn)AOP,通過(guò)這種編程方式,可以針對(duì)業(yè)務(wù)方法進(jìn)行包裝或者監(jiān)控。

但是這個(gè)JdkRegexpMethodPointcut還不是很好,它攔截的單位是類!而無(wú)法細(xì)分到方法。不過(guò)放心,后面還有3中切點(diǎn)技術(shù)~
用AspectJExpressionPointcut切點(diǎn)就可以實(shí)現(xiàn)專門對(duì)什么方法進(jìn)行攔截。后面會(huì)有專門介紹與實(shí)例的。

本文章由[諳憶](méi)編寫(xiě), 所有權(quán)利保留。

轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.net/qq_26525215

本文源自【大學(xué)之旅_諳憶的博客】

總結(jié)

以上是生活随笔為你收集整理的【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。