Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2793196
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: 系统运维

2012-03-11 18:53:46

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

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

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

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

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

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

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

要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间:
       xmlns:xsi=""
       xmlns:aop=""
       xsi:schemaLocation="
           /spring-beans-2.5.xsd
            /spring-aop-2.5.xsd">

Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:
基于XML配置方式声明切面。
基于注解方式声明切面。

首先,声明切入点(要对那些joinpoint进行拦截的定义),然后再编写通知

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns=""
  3.        xmlns:xsi=""
  4.        xmlns:context=""
  5.        xmlns:aop=""
  6.        xsi:schemaLocation="
  7.            /spring-beans-2.5.xsd
  8.             /spring-context-2.5.xsd
  9.             /spring-aop-2.5.xsd">
  10.         <aop:aspectj-autoproxy/>
  11.         <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
  12.         <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>
  13.         <aop:config>
  14.             <aop:aspect id="asp" ref="aspetbean">
  15.                 <aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>
  16.                 <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
  17.                 <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
  18.                  <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
  19.                  <aop:after pointcut-ref="mycut" method="doAfter"/>
  20.                  <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
  21.             </aop:aspect>
  22.         </aop:config>
  23. </beans>


阅读(411) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~