【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut
轉(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)題。
- 上一篇: 使用swipecard实现卡片视图左右滑
- 下一篇: 探索ASP.NET MVC5系列之~~~