AOP面向切面编程
文章目录
Spring AOP(面向切面编程)框架,用于在模块化方面的横切关注点。简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后添加额外的功能。
在Spring AOP中,有 4 种类型通知(advices)的支持:
- 通知(Advice)之前 - 该方法执行前运行
- 通知(Advice)返回之后 – 运行后,该方法返回一个结果
- 通知(Advice)抛出之后 – 运行方法抛出异常后,
- 环绕通知 – 环绕方法执行运行,结合以上这三个通知。
一、Spring AOP 通知——Advice
1. 前置通知
它会在方法执行之前执行。创建一个实现 MethodBeforeAdvice 接口的类。
package com.myprj.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class HijackBeforeMethod implements MethodBeforeAdvice
{
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("HijackBeforeMethod : Before method hijacked!");
}
}
在 bean 配置文件(applicationContext.xml),声明 前置通知HijackBeforeMethod 类,创建一个CustomerService的代理类并命名为“customerServiceProxy” 作为一个新的代理对象。
- ‘target’ – 定义你想拦截的bean。
- ‘interceptorNames’ – 定义要应用这个代理/目标对象的类(通知)。
<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.myprj.customer.services.CustomerService">
<property name="name" value="myprj Mook Kim" />
<property name="url" value="http://www.myprj.com" />
</bean>
<bean id="hijackBeforeMethodBean" class="com.myprj.aop.HijackBeforeMethod" />
<bean id="customerServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>hijackBeforeMethodBean</value>
</list>
</property>
</bean>
</beans>
2.返回后通知
该方法将在返回一个结果之后执行。创建一个实现AfterReturningAdvice接口的类。
package com.myprj.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class HijackAfterMethod implements AfterReturningAdvice
{
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("HijackAfterMethod : After method hijacked!");
}
}
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.myprj.customer.services.CustomerService">
<property name="name" value="Yong Mook Kim" />
<property name="url" value="http://www.myprj.com" />
</bean>
<bean id="hijackAfterMethodBean" class="com.myprj.aop.HijackAfterMethod" />
<bean id="customerServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>hijackAfterMethodBean</value>
</list>
</property>
</bean>
</beans>
3.抛出后通知
它将在方法抛出一个异常后执行。创建一个实现ThrowsAdvice接口的类,并创建一个afterThrowing方法拦截抛出:IllegalArgumentException异常。
package com.myprj.aop;
import org.springframework.aop.ThrowsAdvice;
public class HijackThrowException implements ThrowsAdvice {
public void afterThrowing(IllegalArgumentException e) throws Throwable {
System.out.println("HijackThrowException : Throw exception hijacked!");
}
}
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.myprj.customer.services.CustomerService">
<property name="name" value="Yong Mook Kim" />
<property name="url" value="http://www.myprj.com" />
</bean>
<bean id="hijackThrowExceptionBean" class="com.myprj.aop.HijackThrowException" />
<bean id="customerServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>hijackThrowExceptionBean</value>
</list>
</property>
</bean>
</beans>
4.环绕通知
它结合了上面的三个通知,在方法执行过程中执行。创建一个实现了MethodInterceptor接口的类。必须调用“methodInvocation.proceed();” 继续在原来的方法执行,否则原来的方法将不会执行。
package com.myprj.aop;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class HijackAroundMethod implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Method name : "
+ methodInvocation.getMethod().getName());
System.out.println("Method arguments : "
+ Arrays.toString(methodInvocation.getArguments()));
// same with MethodBeforeAdvice
System.out.println("HijackAroundMethod : Before method hijacked!");
try {
// proceed to original method call
Object result = methodInvocation.proceed();
// same with AfterReturningAdvice
System.out.println("HijackAroundMethod : Before after hijacked!");
return result;
} catch (IllegalArgumentException e) {
// same with ThrowsAdvice
System.out.println("HijackAroundMethod : Throw exception hijacked!");
throw e;
}
}
}
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.myprj.customer.services.CustomerService">
<property name="name" value="Yong Mook Kim" />
<property name="url" value="http://www.myprj.com" />
</bean>
<bean id="hijackAroundMethodBean" class="com.myprj.aop.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>
二、Spring AOP(Pointcut,Advisor)
在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截。但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入’切入点’的原因。它允许你通过它的方法名来拦截方法。另外,一个“切入点”必须具有“Advisor’ 相关联。
在Spring AOP中,有三个非常专业术语- Advices, Cut , Advisor,把它在非官方的方式…
- Advice – 指示之前或方法执行后采取的行动。
- Cut – 指明哪些方法应该拦截,通过方法的名称或正则表达式模式。
- Advisor – 分组"通知"和”切入点“成为一个单元,并把它传递到代理工厂对象。
切入点的例子
可以通过以下两种方式相匹配的方法:
- 名称匹配
- 正则表达式匹配
1. 切入点 - 名称匹配的例子
通过“切入点”和“advisor”拦截printName()方法。创建NameMatchMethodmyprjcut切入点bean,并提出要在“mappedName”属性值来拦截方法名。
<bean id="customermyprjcut"
class="org.springframework.aop.support.NameMatchMethodmyprjcut">
<property name="mappedName" value="printName" />
</bean>
创建 DefaultmyprjcutAdvisor 通知 bean,通知和切入点相关联。
<bean id="customerAdvisor"
class="org.springframework.aop.support.DefaultmyprjcutAdvisor">
<property name="pointcut" ref="customermyprjcut" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean>
更换代理“interceptorNames”到“customerAdvisor”(它是“hijackAroundMethodBeanAdvice”)。
<bean id="customerServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>customerAdvisor</value>
</list>
</property>
</bean>
2. 切入点 - 正则表达式的例子
也可以通过使用正则表达式匹配切入点方法的名称 – RegexpMethodmyprjcutAdvisor.
<bean id="customerAdvisor"
class="org.springframework.aop.support.RegexpMethodmyprjcutAdvisor">
<property name="patterns">
<list>
<value>.*URL.*</value>
</list>
</property>
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean>
现在,它拦截方法名称中有“URL”的方法。在实践中,可以用它来管理DAO层,声明“.DAO.” 拦截所有的DAO类来支持事务。
三、Spring自动代理创建者实例
1. BeanNameAutoProxyCreator示例
在自动代理机制,只需要创建一个的 BeanNameAutoProxyCreator,并包含所有你的bean(通过bean的名字,或正则表达式名)和“advisor” 作为一个单位。
<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.myprj.customer.services.CustomerService">
<property name="name" value="myprj Mook Kim" />
<property name="url" value="http://www.myprj.com" />
</bean>
<bean id="hijackAroundMethodBeanAdvice" class="com.myprj.aop.HijackAroundMethod" />
<bean id="customerAdvisor"
class="org.springframework.aop.support.NameMatchMethodmyprjcutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>customerAdvisor</value>
</list>
</property>
</bean>
</beans>
XML
现在,可以通过“CustomerService”的原始名称获取bean, 如果知道这个bean已经代理。
CustomerService cust = (CustomerService)appContext.getBean("customerService");
2. DefaultAdvisorAutoProxyCreator示例
这个 DefaultAdvisorAutoProxyCreator
是非常强大的,如果有 bean 相关连,Spring会自动创建一个代理。
<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.myprj.customer.services.CustomerService">
<property name="name" value="myprj Mook Kim" />
<property name="url" value="http://www.myprj.com" />
</bean>
<bean id="hijackAroundMethodBeanAdvice" class="com.myprj.aop.HijackAroundMethod" />
<bean id="customerAdvisor"
class="org.springframework.aop.support.NameMatchMethodmyprjcutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
</beans>