在 Spring AOP 中,有 3 个常用的概念,Advices 、 Pointcut 、 Advisor ,解释如下:
- Advices :表示一个 method 执行前或执行后的动作。
- Pointcut :表示根据 method 的名字或者正则表达式去拦截一个 method 。
- Advisor : Advice 和 Pointcut 组成的独立的单元,并且能够传给 proxy factory 对象。
我们可以用名字匹配法和正则表达式匹配法去匹配要拦截的 method 。
1 Pointcut - Name match example
通过 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>
整个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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="customerService" class="com.shiyanlou.spring.aop.CustomerService"> <property name="name" value="Shiyanlou" /> <property name="url" value="shiyanlou.com" /> </bean> <bean id="hijackAroundMethodBean" class="com.shiyanlou.spring.aop.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>
2 Pointcut - Regular exxpression match example
你可以配置用正则表达式匹配需要拦截的 method ,如下配置:
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <bean id="customerService" class="com.shiyanlou.spring.aop.advice.CustomerService"> <property name="name" value="Shiyanlou" /> <property name="url" value="shiyanlou.com" /> </bean> <bean id="hijackAroundMethodBean" class="com.shiyanlou.spring.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.RegexpMethodPointcutAdvisor"> <property name="patterns"> <list> <value>.*URL.*</value> </list> </property> <property name="advice" ref="hijackAroundMethodBean" /> </bean> </beans>