您的位置:首页 > 编程语言 > Java开发

Spring2.5教程:6、AOP

2012-12-26 23:40 351 查看
AOP可以对方法进行拦截,进行权限的判断。

传统的AOP拦截流程:

用户(调用代理对象)--------》代理对象(实现了目标对象的所有接口)-------》invoke方法---------》目标对象(业务bean)

这样就不需要在目标对象(业务bean)进行权限判断了。

1、拦截所有的业务方法

2、判断用户是否有权限,有权限就允许他执行业务方法,没有权限不允许他执行业务方法。

有两种代理:动态代理和静态代理(很少用)

代理类:Proxy,使用的前提是业务bean必须实现接口,否则不能使用。

JDK动态代理

public class JDKProxyimplementsInvocationHandler {

privateObjecttargetObject;//代理的目标对象

publicObjectcreateProxyInstance(ObjecttargetObject){

this.targetObject =targetObject;

/*

* 第一个参数设置代码使用的类装载器,一般采用跟目标类相同的类装载器

* 第二个参数设置代理类实现的接口

* 第三个参数设置回调对象,当代理对象的方法被调用时,会委派给该参数指定对象的invoke方法

*/

returnProxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),

this.targetObject.getClass().getInterfaces(),this);

}

publicObject invoke(Object proxy,Methodmethod,Object[]args)

throws Throwable {

returnmethod.invoke(this.targetObject,args);//把方法调用委派给目标对象

}

}

当目标类实现了接口,我们可以使用jdk的Proxy来生成代理对象。

使用CGLIB生成代理(当业务bean没有实现接口,可以通过地方框架CGLIB,spring帮我们提供了)

public class CGLIBProxyimplementsMethodInterceptor {

privateObjecttargetObject;//代理的目标对象

publicObjectcreateProxyInstance(ObjecttargetObject){

this.targetObject =targetObject;

Enhancer enhancer =new Enhancer();//该类用于生成代理对象

enhancer.setSuperclass(this.targetObject.getClass());//设置父类

enhancer.setCallback(this);//设置回调用对象为本身

returnenhancer.create();

}

publicObject intercept(Object proxy,Methodmethod,Object[]args,

MethodProxymethodProxy) throwsThrowable {

returnmethodProxy.invoke(this.targetObject,args);

}

}

CGLIB可以生成目标类的子类,并重写父类非final修饰符的方法。

AOP中的概念

Aspect(切面):指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面横切性关注点的抽象.

joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点,实际上joinpoint还可以是field或类构造器)

Pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义.

Advice(通知):所谓通知是指拦截到joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知

Target(目标对象):代理的目标对象

Weave(织入):指将aspects应用到target对象并导致proxy对象创建的过程称为织入.

Introduction(引入):在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.

使用Spring进行面向切面(AOP)编程(实际上spring的AOP编程就是使用上面的两种方法,只是进行了封装)

要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间:

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

</beans>

Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:

l基于XML配置方式声明切面。
l基于注解方式声明切面。

基于注解方式声明切面

首先启动对@AspectJ注解的支持(蓝色部分):

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<aop:aspectj-autoproxy/>

<bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/>

<bean id="log" class="cn.itcast.service.LogPrint"/>

</beans>

创建一个类

@Aspect

public class LogPrint {

@Pointcut("execution(*
cn.itcast.service..*.*(..))")

private void anyMethod() {}//声明一个切入点

@Before("anyMethod() &&
args(userName)")//定义前置通知

public void
doAccessCheck(String
userName) {}

@AfterReturning(pointcut="anyMethod()",returning="revalue")//定义后置通知

public void
doReturnCheck(String revalue) {}

@AfterThrowing(pointcut="anyMethod()",
throwing="ex")//定义例外通知

public void
doExceptionAction(Exception ex) {}

@After("anyMethod()")//定义最终通知

public void
doReleaseAction() {}

@Around("anyMethod()")//环绕通知(ProceedingJoinPoing是固定的,方法签名不能变)

public Object
doBasicProfiling(ProceedingJoinPointpjp) throws
Throwable {

return pjp.proceed();

}

@Before("allMethod() &&
args(name)") //得到拦截方法的参数

public void checkPerson(String name) {

System.out.println("前置方法!" + name);

}

@AfterReturning(pointcut="allMethod()",
returning="result") //得到拦截方法的返回值

public void aferRuning(String result) {

System.out.println("后置方法" + result);

}

@AfterThrowing(pointcut="allMethod()", throwing="e")//得到拦截方法抛出的异常

public void exceptionMethod(Exception e) {

System.out.println("异常方法" + e);

}

}

举例说明:

环绕通知。。。。(比如struts2提供的拦截器就是)

if(){

前置通知。。。

try{

后置通知。。。

}catch(){

例外通知。。。

}finally{

最终通知。。。

}

}

基于基于XML配置方式声明切面

public class LogPrint {

public void doAccessCheck() {}定义前置通知

public void
doReturnCheck() {}定义后置通知

public void
doExceptionAction() {}定义例外通知

public void
doReleaseAction() {}定义最终通知

public Object
doBasicProfiling(ProceedingJoinPointpjp) throws
Throwable {

return pjp.proceed();环绕通知

}

}

<bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/>

<bean id="log" class="cn.itcast.service.LogPrint"/>//切面类

<aop:config>

<aop:aspect id="myaop" ref="log">

<aop:pointcut id="mycut" expression="execution(*
cn.itcast.service..*.*(..))"/>

<aop:beforepointcut-ref="mycut" method="doAccessCheck"/>

<aop:after-returningpointcut-ref="mycut"
method="doReturnCheck "/>

<aop:after-throwingpointcut-ref="mycut"
method="doExceptionAction"/>

<aop:afterpointcut-ref="mycut" method=“doReleaseAction"/>

<aop:aroundpointcut-ref="mycut" method="doBasicProfiling"/>

</aop:aspect>

</aop:config>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: