Spring 的 AOPWord格式文档下载.docx

上传人:b****5 文档编号:15706205 上传时间:2022-11-15 格式:DOCX 页数:10 大小:19.31KB
下载 相关 举报
Spring 的 AOPWord格式文档下载.docx_第1页
第1页 / 共10页
Spring 的 AOPWord格式文档下载.docx_第2页
第2页 / 共10页
Spring 的 AOPWord格式文档下载.docx_第3页
第3页 / 共10页
Spring 的 AOPWord格式文档下载.docx_第4页
第4页 / 共10页
Spring 的 AOPWord格式文档下载.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

Spring 的 AOPWord格式文档下载.docx

《Spring 的 AOPWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《Spring 的 AOPWord格式文档下载.docx(10页珍藏版)》请在冰豆网上搜索。

Spring 的 AOPWord格式文档下载.docx

  4、advice(增强处理):

AOP框架特定的切入点执行的增强处理

如何使用表达式来定义切入点:

  1、引入:

将方法或字段添加到被处理的类中。

  2、目标对象:

被AOP框架进行增强处理的对象,也被称为被增强的对象。

如果AOP框架是通过运行时代理来实现的,那么这个对象是一个被代理的对象。

  3、AOP代理:

AOP框架创建的对象,简单的说,代理就是对目标对象的增强。

  4、织入(Weaving):

将增强处理添加到目标对象中,并创建一个被增强的处理的过程就是织入。

2、Spring的AOP支持

  在AOP编程,我们需要做如下三部分:

  1、定义普通组件。

  2、定义切入点、一个切入点可能横切多个业务组件。

  3、定义增强处理,增强处理就是在AOP框架为普通业务组件织入的处理动作。

  AOP编成的关键是定义切入点和定义增强处理,Spring依然有如下两个方法来实现:

  1、基于Annotation的“零配置”方式。

  2、使用XML配置文件的管理方式。

3、基于Annotation的“零配置”方式

  为了启动Spring对@AspectJ切面配置的支持,并保证Spring容器中的目标Bean被一个或多个切面自动增强,必须在Spring配置文件下配置如下代码:

  

<

?

xmlversion="

1.0"

encoding="

UTF-8"

>

beans

xmlns="

http:

//www.springframework.org/schema/beans"

xmlns:

xsi="

//www.w3.org/2001/XMLSchema-instance"

xmlns:

aop="

//www.springframework.org/schema/aop"

p="

//www.springframework.org/schema/p"

xsi:

schemaLocation="

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-2.0.xsd

//www.springframework.org/schema/aop

//www.springframework.org/schema/aop/spring-aop-3.0.xsd"

<

!

--启动@AspectJ支持-->

aop:

aspectj-autoproxy/>

/beans>

  但是希望完全启动Spring的“零配置”功能,则配置代码中还需添加:

  xmlns:

context="

//www.springframework.org/schema/context"

//www.springframework.org/schema/aop/spring-aop-3.0.xsd

//www.springframework.org/schema/context

//www.springframework.org/schema/context/spring-context-3.0.xsd"

   

  这里给出下面增强处理的目标类:

packageorg.service.imp;

importorg.service.Person;

importorg.springframework.stereotype.*;

@Component

publicclassChineseimplementsPerson{

publicStringsayHello(Stringname){

//TODOAuto-generatedmethodstub

returnname+"

你好,SpringAop"

;

}

publicvoideat(Stringfood){

System.out.println("

我正在吃:

"

+food);

}

  3.1定义切面Bean

  在启动@AspectJ支持以后,只要在Sping容器中配置一个带@Aspect注释的Bean,Spring将会自动识别该Bean,并将该Bean作为切面处理。

使用@Aspect标注一个Java类,该Java类将会作为切面Bean 

@Aspect

publicclassAspectTest

{

   //定义类的其他类容

......

  3.2定义Before增强处理

  使用@Before来标注一个方法,使该方法作为一个Before增强处理。

使用@Before标注时,通常需要指定一个value值,改值指定一个切入表达式,用于指定该增强处理将被织入哪些切入点。

  如:

@Before("

execution(*blogs.jbelial.*.*(..))"

) 

  在被它标注的方法将匹配blogs.jbelial包下的所有类的、所有方法的执行作为切入点。

packageorg.advice.Before;

importorg.aspectj.lang.annotation.Aspect;

importorg.aspectj.lang.annotation.Before;

//定义一个切面

publicclassBeforeAdviceTest{

//匹配该包下的所有类

@Before("

execution(*org.service.imp.*.*(..))"

publicvoidauthority()

{

模拟执行权限检查"

);

  注:

在定义增强处理时的Spring配置文件如何配置,可以参考Spring的“零配置”支持“的学习。

  3.3定义AfterReturning增强处理

   类似于使用@Before,使用@AfterReturning来标注一个增强处理,该增强处理将会在目标方法正常完成后被织入。

在使用@AfterReturningAnnotation时可以指定如下两个常量属性。

  1、pointcut/value:

用于指定切入点对于的表达式。

  2、returning:

制定一个返回值形参名,增强处理定义的方法可以通过该形参名来访问目标方法的返回值。

@AfterReturning(returning="

rvt"

pointcut="

  指定一个returning属性,该属性值为rvt,表示允许在增强处理方法中使用名为rvt的形参,该形参代表目标方法的返回值。

  @AfterReturningAnnotation的returning属性所制定的的形参名必须对应于增强处理中的一个形参名。

当目标方法执行返回后,返回值作为相应的参数值传入增强处理的方法。

  如下:

packageorg.advice.AfterReturning;

importorg.aspectj.lang.annotation.AfterReturning;

publicclassAfterReturningAdivceTest{

@AfterReturning(returning="

pointcut="

publicvoidlog(Objectrvt)

获取目标方法返回值:

+rvt);

模拟记录日志功能..."

  3.4定义AfterThrowing增强处理:

  AfterThrowing增强处理主要用于处理程序中未处理的异常。

  @AfterThrowingAnnotation两个常用的属性:

  >

pointcut/value:

如上。

throwing:

指定一个返回参数名,增强处理定义的方法可通过该形参名来访问目标方法中所抛出的异常对象。

@AfterThrowing(throwing="

ex"

pointcut="

  3.5After增强处理:

  After与AfterReturning的区别:

  After:

增强处理不管目标方法如何结束,他都会被织入。

因此,After增强处理必须准备处理正常返回和异常返回两种情况,这种增强处理通常用于释放资源。

  AfterReturning:

增强处理只有在目标方法成功完成才会被织入。

packageorg.advice.After;

importorg.aspectj.lang.annotation.Asp

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1