上一篇的Spring AOP Advice例子中,Class(CustomerService)中的所有method都被自动的拦截了。可是大多状况下,你只须要一个方法去拦截一两个method。这样就引入了Pointcut(切入点)的概念,它容许你根据method的名字去拦截指定的method。另外,一个Pointcut必须结合一个Advisor来使用。java
在Spring AOP中,有3个经常使用的概念,Advices、Pointcut、Advisor,解释以下,web
Advices:表示一个method执行前或执行后的动做。正则表达式
Pointcut:表示根据method的名字或者正则表达式去拦截一个method。spring
Advisor:Advice和Pointcut组成的独立的单元,而且可以传给proxy factory 对象。app
下边来回顾一下上一篇例子中的代码this
CustomerService.javaurl
package com.lei.demo.aop.advice; public class CustomerService { private String name; private String url; public void setName(String name) { this.name = name; } public void setUrl(String url) { this.url = url; } public void printName() { System.out.println("Customer name : " + this.name); } public void printURL() { System.out.println("Customer website : " + this.url); } public void printThrowException() { throw new IllegalArgumentException(); } }
配置文件Spring-AOP-Advice.xml:spa
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService"> <property name="name" value="LeiOOLei" /> <property name="url" value="http://www.cnblogs.com/leiOOlei/" /> </bean> <bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" /> <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="customerService" /> <property name="interceptorNames"> <list> <value>hijackAroundMethodBean</value> </list> </property> </bean> </beans>
HijackAroundMethod.java代理
package com.lei.demo.aop.advice; import java.util.Arrays; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class HijackAroundMethod implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("Method name : " + methodInvocation.getMethod().getName()); System.out.println("Method arguments : " + Arrays.toString(methodInvocation.getArguments())); // 至关于 MethodBeforeAdvice System.out.println("HijackAroundMethod : Before method hijacked!"); try { // 调用原方法,即调用CustomerService中的方法 Object result = methodInvocation.proceed(); // 至关于 AfterReturningAdvice System.out.println("HijackAroundMethod : After method hijacked!"); return result; } catch (IllegalArgumentException e) { // 至关于 ThrowsAdvice System.out.println("HijackAroundMethod : Throw exception hijacked!"); throw e; } } }
运行以下App.javacode
package com.lei.demo.aop.advice; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext( new String[] { "Spring-AOP-Advice.xml" }); System.out.println("使用Spring AOP 以下"); CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy"); System.out.println("*************************"); cust.printName(); System.out.println("*************************"); cust.printURL(); System.out.println("*************************"); try { cust.printThrowException(); } catch (Exception e) { } } }
运行结果:
使用Spring AOP 以下
*************************
Method name : printName
Method arguments : []
HijackAroundMethod : Before method hijacked!
Customer name : LeiOOLei
HijackAroundMethod : After method hijacked!
*************************
Method name : printURL
Method arguments : []
HijackAroundMethod : Before method hijacked!
Customer website : http://www.cnblogs.com/leiOOlei/
HijackAroundMethod : After method hijacked!
*************************
Method name : printThrowException
Method arguments : []
HijackAroundMethod : Before method hijacked!
HijackAroundMethod : Throw exception hijacked!
上边的结果中,CustomerService.java中,所有的method方法所有被拦截了,下边咱们将展现怎样利用Pointcuts只拦截printName()。
你能够用名字匹配法和正则表达式匹配法去匹配要拦截的method。
经过pointcut和advisor拦截printName()方法。
建立一个NameMatchMethodPointcut的bean,将你想拦截的方法的名字printName注入到属性mappedName,以下
<bean id="customerPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut"> <property name="mappedName" value="printName" /> </bean>
建立一个DefaultPointcutAdvisor的advisor bean,将pointcut和advice关联起来。
<bean id="customerAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut" ref="customerPointcut" /> <property name="advice" ref=" hijackAroundMethodBean " /> </bean>
更改代理的interceptorNames值,将上边的advisor( customerAdvisor)替代原来的hijackAroundMethodBean。
<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="customerService" /> <property name="interceptorNames"> <list> <value>customerAdvisor</value> </list> </property> </bean>
全部的配置文件以下:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService"> <property name="name" value="LeiOOLei" /> <property name="url" value="http://www.cnblogs.com/leiOOlei/" /> </bean> <bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" /> <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="customerService" /> <property name="interceptorNames"> <list> <value>customerAdvisor</value> </list> </property> </bean> <bean id="customerPointcut"class="org.springframework.aop.support.NameMatchMethodPointcut"> <property name="mappedName" value="printName" /> </bean> <bean id="customerAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut" ref="customerPointcut" /> <property name="advice" ref=" hijackAroundMethodBean " /> </bean> </beans>
再运行一下App.java,输出结果以下:
使用Spring AOP 以下
*************************
Method name : printName
Method arguments : []
HijackAroundMethod : Before method hijacked!
Customer name : LeiOOLei
HijackAroundMethod : After method hijacked!
*************************
Customer website : http://www.cnblogs.com/leiOOlei/
*************************
以上运行结果显示,只拦截了printName()方法。
注意:
以上配置中pointcut和advisor能够合并在一块儿配置,即不用单独配置customerPointcut和customerAdvisor,只要配置customerAdvisor时class选择NameMatchMethodPointcutAdvisor以下:
<bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="mappedName" value="printName" /> <property name="advice" ref="hijackAroundMethodBean" /> </bean>
这样,整个配置文件以下:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService"> <property name="name" value="LeiOOLei" /> <property name="url" value="http://www.cnblogs.com/leiOOlei/" /> </bean> <bean id="hijackAroundMethodBean" class="com.lei.demo.aop.advice.HijackAroundMethod" /> <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="customerService" /> <property name="interceptorNames"> <list> <value>customerAdvisor</value> </list> </property> </bean> <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="mappedName" value="printName" /> <property name="advice" ref="hijackAroundMethodBean" /> </bean> </beans>
实际上这种作法将method名字与具体的advice捆绑在一块儿,有悖于Spring松耦合理念,若是将method名字单独配置成pointcut(切入点),advice和pointcut的结合会更灵活,使一个pointcut能够和多个advice结合。
你能够配置用正则表达式匹配须要拦截的method,以下配置
<bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="patterns"> <list> <value>.*URL.*</value> </list> </property> <property name="advice" ref="hijackAroundMethodBeanAdvice" /> </bean>
如今,你能够拦截名字中包含URL字符的method了,在实际工做中,你能够用它来管理DAO层,例如,你能够用“.*DAO.*”来拦截全部DAO层中的相关业务。