Spring AOP
(1)AOP 概述
AOP 的全称为: (Aspect Oriented Programming) ,中文意思为,面向切面 ( 方面 ) 编程。
简单的说: AOP 就是实现横切的工具。
AOP 让我们的设计逻辑更加模块化,我们称之为“关注点”。我们在同一个程序的很多部分重复使用这些关注点。比如,记录日志和控制安全性能是很多程序都会用到的典型的横切。
(2)AOP 的种类
事实上有两种完全不同的 AOP ,静态和动态。
在静态 AOP 中 ,横切逻辑是在编译时加入到程序中去的。如果要修改横切,就必须修改代码重新编译。
在动态 AOP 中 , ( 比如 Spring 中的 AOP) ,横切逻辑是在动行时动态加入程序中的,这样,我们无需重新编译代码就可以修改横切的使用。
这两种 AOP 互相补充,在程序中两者一起使用时功能非常强大。
(3) AOP 中几个比较重要的概念
<1> 切面 (Aspect)
切面,对象操作过程中的截面。这可能是 AOP 中最关键的一个术语。
我们首先来看一个应用开发中常见的切面:用户权限检查。大概只要是完整的应用,都
少不了用户权限检查这个模块,不同身份的用户可以做什么,不可以做什么,均由这个模块
加以判定。而这个模块调用的位置通常也比较固定:用户发起请求之后,执行业务逻辑之前。
针对权限检查这一模块进行分离,我们就得到了一个切面:
切面意义何在?
首先根据上例,假设我们实现了一个通用的权限检查模块,那么就可以在这层切面上进
行统一的集中式权限管理。而业务逻辑组件则无需关心权限方面的问题。也就是说,通过切
面,我们可以将系统中各个不同层次上的问题隔离开来,实现统一集约式处理。各切面只需
集中于自己领域内的逻辑实现。
这一方面使得开发逻辑更加清晰,专业化分工更加易于进行;另一方面,由于切面的隔
离,降低了耦合性,我们就可以在不同的应用中将各个切面组合使用,从而使得代码可重用
性大大增强。
<2> 联接点( JoinPoint )
程序运行过程中的某个阶段点。如某个方法调用,或者某个异常被抛出。
<3> 处理逻辑( Advice ,或者叫通知)
在某个连接点所采用的处理逻辑
常用的有三种:
前置通知、后置通知、包围通知 ( 后面会详细讲解 )
<4> 切点( PointCut )
一系列连接点的集合,它指明处理方式( Advice )将在何时被触发。
(4) 理解代理
代理的核心任务就是拦截方法调用,并在需要的时候执行匹配某方法的通知链。通知的管理和调用基本与代理无关,所以这是由 Spring AOP 架构负责的。不过,代理还是需要拦截所有的方法调用,并在必要时将该调用传给 AOP 架构,再由后者调用通知。
< 静态代理示例 >
9. Spring 中常用三种通知
(1) 前置通知
接口: org.springframework.aop.MethodBeforeAdvice
使用前置通知可以在联结点执行前进行自定义的操作。不过, Spring 里只有一种联结点,即方法调用,所以前置通知事实上就是让你能在方法调用前进行一些操作。前置通知可以访问调用的目标方法,也可以对该方法的参数进行操作,不过它不能影响方法调用本身。
示例:
IHello 接口
public interface IHello { public void hello(String name); } |
IHello 接口实现
public class HelloSpeaker implements IHello { public void hello(String name) { System.out.println("Hello, " + name); } } |
IHello 实现代理
public class LogBeforeAdvice implements MethodBeforeAdvice { private Logger logger = Logger.getLogger(this.getClass().getName());
public void before(Method method, Object[] args, Object target) throws Throwable { logger.log(Level.INFO, "method starts..." + method); } } |
Spring 配置文件
<bean id="logBeforeAdvice" class="onlyfun.caterpillar.LogBeforeAdvice"/>
<bean id="helloSpeaker" class="onlyfun.caterpillar.HelloSpeaker"/>
<bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>onlyfun.caterpillar.IHello</value> </property> <property name="target"> <ref bean="helloSpeaker"/> </property> <property name="interceptorNames"> <list> <value>logBeforeAdvice</value> </list> </property> </bean> |
测试类
public class SpringAOPDemo { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "beans-config.xml"); IHello helloProxy = (IHello) context.getBean("helloProxy"); helloProxy.hello("Justin"); } } |
(2) 后置通知 ( 或返回后通知 )
接口: org.springframework.aop.AfterReturningAdvice
后置通知在联结点处的方法调用已经完成,并且已经返回一个值时运行,后置通知可以访问调用的目标方法,以及该方法的参数和返回值。因为等到通知执行时该方法已经调用,后置通知完全不能影响方法调用本身。
示例:
IHello 接口
public interface IHello { public void hello(String name); } |
IHello 接口实现
public class HelloSpeaker implements IHello { public void hello(String name) { System.out.println("Hello, " + name); } } |
IHello 实现代理
public class LogAfterAdvice implements AfterReturningAdvice { private Logger logger = Logger.getLogger(this.getClass().getName());
public void afterReturning(Object object, Method method, Object[] args, Object target) throws Throwable { logger.log(Level.INFO, "method ends..." + method); } } |
Spring 配置文件
<bean id="logAfterAdvice" class="onlyfun.caterpillar.LogAfterAdvice"/>
<bean id="helloSpeaker" class="onlyfun.caterpillar.HelloSpeaker"/>
<bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>onlyfun.caterpillar.IHello</value> </property> <property name="target"> <ref bean="helloSpeaker"/> </property> <property name="interceptorNames"> <list> <value>logAfterAdvice</value> </list> </property> </bean> |
测试类
public class SpringAOPDemo { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "beans-config.xml"); IHello helloProxy = (IHello) context.getBean("helloProxy"); helloProxy.hello("Justin"); } } |
(3) 包围通知
接口: org.aopalliance.intercept.MethodInterceptor
Spring 中的包围通知模 AOP 联盟的“方法拦截器”标准。包围通知可以在目标方法之前和之后动行,我们也可以定义在什么时候调用目标方法。如果需要,我们也可以另写自己的逻辑而完全不调用目标方法。
示例:
IHello 接口
public interface IHello { public void hello(String name); } |
IHello 接口实现
public class HelloSpeaker implements IHello { public void hello(String name) { System.out.println("Hello, " + name); } } |
IHello 实现代理
public class LogInterceptor implements MethodInterceptor { private Logger logger = Logger.getLogger(this.getClass().getName());
public Object invoke(MethodInvocation methodInvocation) throws Throwable { logger.log(Level.INFO, "method starts..." + methodInvocation.getMethod());
Object result = null; try { result = methodInvocation.proceed(); } finally { logger.log(Level.INFO, "method ends..." + methodInvocation.getMethod() + "\n"); } return result; } |
Spring 配置文件
<bean id="logInterceptor" class="onlyfun.caterpillar.LogInterceptor"/>
<bean id="helloSpeaker" class="onlyfun.caterpillar.HelloSpeaker"/>
<bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>onlyfun.caterpillar.IHello</value> </property> <property name="target"> <ref bean="helloSpeaker"/> </property> <property name="interceptorNames"> <list> <value>logInterceptor</value> </list> </property> </bean> |
测试类
public class SpringAOPDemo { public static void main(String[] args) { ApplicationContext context = new FileSystemXmlApplicationContext( "beans-config.xml"); IHello helloProxy = (IHello) context.getBean("helloProxy"); helloProxy.hello("Justin"); } |
10. 利用 Spring 中的 AOP 做权限管理
AOP 在 Spring 中占有很重要的地位,做了一个例子是利用 AOP 来做一个登陆的身份验证,使用了 AOP 可以在不破坏你的代码的前提下帮你完成验证功能。
选用实现 MethodInterceptor 接口的方法来完成这个功能
代码如下:
接口类的定义:
程序代码 : public interface ILogin {
|
接口实现类的定义:
程序代码 : import com.dragon.study.ILogin; public class LoginImpl implements ILogin { |
最重要的拦截器的定义:
程序代码 :
import org.aopalliance.intercept.MethodInterceptor; public class LoginInterceptor implements MethodInterceptor { |
ApplicationContext.xml 的定义:
<beans> |
Test 类定义:
package com; import java.io.*; import org.springframework.context.ApplicationContext; import com.dragon.study.ILogin; public class Test { |