Spring之AOP入门(二)

文档版本 开发工具 测试平台 工程名字 日期 做者 备注
V1.0 2016.06.22 lutianfei none

AOP的概述

什么是AOP

  • AOP Aspect Oriented Programing 面向切面编程
  • AOP采起横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视、事务管理、安全检查、缓存)
  • Spring AOP使用纯Java实现,不须要专门的编译过程和类加载器,在运行期经过代理方式向目标类织入加强代码
  • AspecJ是一个基于Java语言的AOP框架,Spring2.0开始,Spring AOP引入对Aspect的支持,AspectJ扩展了Java语言,提供了一个专门的编译器,在编译时提供横向代码的织入


AOP底层原理

  • 代理机制:
  • 动态代理:(JDK中使用)
  • JDK的动态代理,对实现了接口的类生成代理.

Spring的AOP代理

  • JDK动态代理:对实现了接口的类生成代理
  • CGLib代理机制:对类生成代理

AOP的术语

  • Joinpoint(链接点):所谓链接点是指那些被拦截到的点。在spring中,这些点指的是方法,由于spring只支持方法类型的链接点.
  • Pointcut(切入点):所谓切入点是指咱们要对哪些Joinpoint进行拦截的定义.
  • Advice(通知/加强):所谓通知是指拦截到Joinpoint以后所要作的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
  • Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction能够在运行期为类动态地添加一些方法或Field.
  • Target(目标对象):代理的目标对象
  • Weaving(织入):是指把加强应用到目标对象来建立新的代理对象的过程.
  • spring采用动态代理织入,而AspectJ采用编译期织入类转载期织入
  • Proxy(代理):一个类被AOP织入加强后,就产生一个结果代理
  • Aspect(切面): 是切入点通知(引介)的结合


AOP的底层实现

JDK动态代理

  • UserDao & UserDaoImpl
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
public interface UserDao { public void add(); public void update(); } public class UserDaoImpl implements UserDao { public void add() { System.out.println("添加用户..."); } public void update() { System.out.println("修改用户..."); } } @Test public void demo2(){ // 被代理对象 UserDao userDao = new UserDaoImpl(); // 建立代理对象的时候传入被代理对象. UserDao proxy = new JDKProxy(userDao).createProxy(); proxy.add(); proxy.update(); }
  • JDK动态代理
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
public class JDKProxy implements InvocationHandler{ private UserDao userDao; public JDKProxy(UserDao userDao) { super(); this.userDao = userDao; } public UserDao createProxy() { UserDao proxy = (UserDao) Proxy.newProxyInstance(userDao.getClass() .getClassLoader(), userDao.getClass().getInterfaces(), this); return proxy; } // 调用目标对象的任何一个方法 都至关于调用invoke(); public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if("add".equals(method.getName())){ // 记录日志: System.out.println("日志记录================="); Object result = method.invoke(userDao, args); return result; } return method.invoke(userDao, args); } }


CGLIB动态代理

  • CGLIB(Code Generation Library)是一个开源项目!是一个强大的,高性能,高质量的Code生成类库,它能够在运行期扩展Java类与实现Java接口。 Hibernate支持它来实现PO(Persistent Object 持久化对象)字节码的动态生成css

  • CGLIB生成代理机制:CGlib采用很是底层字节码技术,能够为一个类建立子类,解决无接口代理问题java

  • 如今作cglib的开发,能够不用直接引入cglib的包.已经在spring的核心中集成cglib.面试

  • 关于intercept拦截方法正则表达式

    • @param obj CGlib根据指定父类生成的代理对象
    • @param method 拦截的方法
    • @param args 拦截方法的参数数组
    • @param proxy 方法的代理对象,用于执行父类的方法
    • @return

    public Object intercept(Object obj, Method method, Object[] args,
    MethodProxy proxy) throws Throwable {
    … …
    }spring

    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
public class CGLibProxy implements MethodInterceptor{ private ProductDao productDao; public CGLibProxy(ProductDao productDao) { super(); this.productDao = productDao; } public ProductDao createProxy(){ // 使用CGLIB生成代理: // 1.建立核心类: Enhancer enhancer = new Enhancer(); // 2.为其设置父类: enhancer.setSuperclass(productDao.getClass()); // 3.设置回调: enhancer.setCallback(this); // 4.建立代理: return (ProductDao) enhancer.create(); } public Object intercept(Object proxy, Method method, Object[] args,MethodProxy methodProxy) throws Throwable { if("add".equals(method.getName())){ System.out.println("日志记录=============="); Object obj = methodProxy.invokeSuper(proxy, args); return obj; } return methodProxy.invokeSuper(proxy, args); } } //SpringTest @Test public void demo2(){ ProductDao productDao = new ProductDao(); ProductDao proxy = new CGLibProxy(productDao).createProxy(); proxy.add(); proxy.update(); }
  • 结论 : Spring框架,若是类实现了接口,就使用JDK的动态代理生成代理对象,若是这个类没有实现任何接口,使用CGLIB生成代理对象


代理知识总结

  • Spring在运行期,生成动态代理对象,不须要特殊的编译器
  • Spring AOP的底层就是经过JDK动态代理或CGLib动态代理技术 为目标Bean执行横向织入
    • 1.若目标对象实现了若干接口,spring使用JDK的java.lang.reflect.Proxy类代理。
    • 2.若目标对象没有实现任何接口,spring使用CGLIB库生成目标对象的子类。
  • 程序中应优先对接口建立代理,便于程序解耦维护
  • 标记为final的方法,不能被代理,由于没法进行覆盖
  • JDK动态代理,是针对接口生成子类,接口中方法不能使用final修饰
  • CGLib 是针对目标类生产子类,所以类或方法 不能使final的
  • Spring只支持方法链接点,不提供属性链接

Spring中的AOP

Spring的传统AOP

  • AOP:不是由Spring定义.由AOP联盟的组织定义.

Spring中的通知(加强类型)

  • AOP联盟为通知Advice定义了org.aopalliance.aop.Interface.Advice
  • Spring按照通知Advice在目标类方法的链接点位置,能够分为5类express

  • 前置通知 org.springframework.aop.MethodBeforeAdvice编程

    • 在目标方法执行前实施加强
  • 后置通知 org.springframework.aop.AfterReturningAdvice
    • 在目标方法执行后实施加强
  • 环绕通知 org.aopalliance.intercept.MethodInterceptor
    • 在目标方法执行先后实施加强
  • 异常抛出通知 org.springframework.aop.ThrowsAdvice
    • 在方法抛出异常后实施加强
  • 引介通知 org.springframework.aop.IntroductionInterceptor(课程不讲.)
    • 在目标类中添加一些新的方法和属性

Spring中的切面类型

  • Advisor : Spring中传统切面.表明通常切面,Advice自己就是一个切面,对目标类全部方法进行拦截数组

    • Advisor:都是有一个切点和一个通知组合.
  • Aspect:多个切点和多个通知组合.缓存

  • PointcutAdvisor : 表明具备切点的切面,能够指定拦截目标类哪些方法(带有切点的切面,针对某个方法进行拦截)安全

  • IntroductionAdvisor : 表明引介切面,针对引介通知而使用切面(不要求掌握)


Spring的AOP的开发

针对全部方法的加强:(不带有切点的切面)

  • 第一步:导入相应jar包.

    • spring-aop-3.2.0.RELEASE.jar
    • com.springsource.org.aopalliance-1.0.0.jar
  • 第二步:编写被代理对象:

    • CustomerDao接口
    • CustoemrDaoImpl实现类
  • 第三步:编写加强的代码:

    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
public class MyBeforeAdvice implements MethodBeforeAdvice{ /** * method:执行的方法 * args:参数 * target:目标对象 */ public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("前置加强..."); } }


  • 第四步:生成代理:(配置生成代理)
    • 生成代理Spring基于ProxyFactoryBean类.底层自动选择使用JDK的动态代理仍是CGLIB的代理.
    • 属性:
    • target : 代理的目标对象
    • proxyInterfaces : 代理要实现的接口
    • 若是多个接口可使用如下格式赋值
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
<list> <value></value> .... </list>
* proxyTargetClass : 是否对类代理而不是接口,设置为true时,使用CGLib代理
* interceptorNames : 须要织入目标的Advice
* singleton : 返回代理是否为单实例,默认为单例
* optimize : 当设置为true时,强制使用CGLib
  • applicationContext.xml
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
<!-- 定义目标对象 --> <bean id="customerDao" class="cn.itcast.spring3.demo3.CustomerDaoImpl"></bean> <!-- 定义加强 --> <bean id="beforeAdvice" class="cn.itcast.spring3.demo3.MyBeforeAdvice"></bean> <!-- Spring支持配置生成代理: --> <bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 设置目标对象 --> <property name="target" ref="customerDao"/> <!-- 设置实现的接口 ,value中写接口的全路径 --> <property name="proxyInterfaces" value="cn.itcast.spring3.demo3.CustomerDao"/> <!-- 须要使用value:要的名称 --> <property name="interceptorNames" value="beforeAdvice"/> </bean>
  • 注入的时候要注入代理对象:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
@Autowired // @Qualifier("customerDao")// 注入是真实的对象,必须注入代理对象. @Qualifier("**customerDaoProxy**") private CustomerDao customerDao;


带有切点的切面:(针对目标对象的某些方法进行加强)

  • 经常使用PointcutAdvisor 实现类

    • DefaultPointcutAdvisor 最经常使用的切面类型,它能够经过任意Pointcut和Advice 组合定义切面
    • RegexpMethodPointcutAdvisor 构造正则表达式切点切面
  • 第一步:建立被代理对象.

    • OrderDao
  • 第二步:编写加强的类:

    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
public class MyAroundAdvice implements MethodInterceptor{ public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("环绕前加强..."); Object result = methodInvocation.proceed();// 执行目标对象的方法 System.out.println("环绕后加强..."); return result; } }


  • 第三步:生成代理:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
<!-- 带有切点的切面 --> <!-- 定义目标对象 --> <bean id="orderDao" class="cn.itcast.spring3.demo4.OrderDao"></bean> <!-- 定义加强 --> <bean id="aroundAdvice" class="cn.itcast.spring3.demo4.MyAroundAdvice"></bean> <!-- 定义切点切面: --> <bean id="myPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <!-- 定义表达式,规定哪些方法执行拦截 --> <!-- . 任意字符 * 任意个 --> <!-- <property name="pattern" value=".*"/> --> <!-- <property name="pattern" value="cn\.itcast\.spring3\.demo4\.OrderDao\.add.*"/> --> <!-- <property name="pattern" value=".*add.*"></property> --> <property name="patterns" value=".*add.*,.*find.*"></property> <!-- 应用加强 --> <property name="advice" ref="aroundAdvice"/> </bean> <!-- 定义生成代理对象 --> <bean id="orderDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 配置目标 --> <property name="target" ref="orderDao"></property> <!-- 针对类的代理 --> <property name="proxyTargetClass" value="true"></property> <!-- 在目标上应用加强 --> <property name="interceptorNames" value="myPointcutAdvisor"></property> </bean>


自动代理

  • 前面的案例中,每一个代理都是经过ProxyFactoryBean织入切面代理,在实际开发中,很是多的Bean每一个都配置ProxyFactoryBean开发维护量巨大。

  • 自动建立代理(基于后处理Bean.在Bean建立的过程当中完成的加强.生成Bean就是代理.)

  • BeanNameAutoProxyCreator 根据Bean名称建立代理

  • DefaultAdvisorAutoProxyCreator 根据Advisor自己包含信息建立代理

  • AnnotationAwareAspectJAutoProxyCreator 基于Bean中的AspectJ 注解进行自动代理


BeanNameAutoProxyCreator :按名称生成代理

    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
<!-- 定义目标对象 --> <bean id="customerDao" class="cn.itcast.spring3.demo3.CustomerDaoImpl"></bean> <bean id="orderDao" class="cn.itcast.spring3.demo4.OrderDao"></bean> <!-- 定义加强 --> <bean id="beforeAdvice" class="cn.itcast.spring3.demo3.MyBeforeAdvice"></bean> <bean id="aroundAdvice" class="cn.itcast.spring3.demo4.MyAroundAdvice"></bean> <!-- 自动代理:按名称的代理 基于后处理Bean,后处理Bean不须要配置ID--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames" value="*Dao"/>** <property name="interceptorNames" value="beforeAdvice"/>** </bean>
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext2.xml") public class SpringTest5 { @Autowired @Qualifier("orderDao") private OrderDao orderDao; @Autowired @Qualifier("customerDao") private CustomerDao customerDao; @Test public void demo1(){ orderDao.add(); orderDao.delete(); customerDao.update(); } }


DefaultAdvisorAutoProxyCreator :根据切面中定义的信息生成代理

    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
<!-- 定义目标对象 --> <bean id="customerDao" class="cn.itcast.spring3.demo3.CustomerDaoImpl"></bean> <bean id="orderDao" class="cn.itcast.spring3.demo4.OrderDao"></bean> <!-- 定义加强 --> <bean id="beforeAdvice" class="cn.itcast.spring3.demo3.MyBeforeAdvice"></bean> <bean id="aroundAdvice" class="cn.itcast.spring3.demo4.MyAroundAdvice"></bean> <!--定义一个带有切点的切面 --> <bean id="myPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">** <property name="pattern" value=".*add.*"/> <property name="advice" ref="aroundAdvice"/> </bean> <!--自动生成代理 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext3.xml") public class SpringTest6 { @Autowired @Qualifier("orderDao") private OrderDao orderDao; @Autowired @Qualifier("customerDao") private CustomerDao customerDao; @Test public void demo1(){ orderDao.add(); orderDao.update(); orderDao.delete(); customerDao.add(); } }


  • 区分基于ProxyFattoryBean****的代理与自动代理区别?
    • ProxyFactoryBean:先有被代理对象,将被代理对象传入到代理类中生成代理.
    • 自动代理基于后处理Bean.在Bean的生成过程当中就产生了代理对象,把代理对象返回.生成Bean已是代理对象


使用AspectJ实现AOP

  • AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法因此它有一个专门的编译器用来生成遵照Java字节编码规范的Class文件。

  • AspectJ是一个基于Java语言的AOP框架

  • Spring2.0之后新增了对AspectJ切点表达式支持

  • @AspectJ 是AspectJ1.5新增功能,经过JDK5注解技术,容许直接在Bean类中定义切面

  • 新版本Spring框架,建议使用AspectJ方式来开发AOP

AspectJ表达式

  • 语法:execution(表达式)

    • execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
  • execution(“* cn.itcast.spring3.demo1.dao.*(..)”) —只检索当前包

  • execution(“* cn.itcast.spring3.demo1.dao..*(..)”) —检索包及当前包的子包.

  • execution(* cn.itcast.dao.GenericDAO+.*(..)) —检索GenericDAO及子类


AspectJ的通知类型

  • @Before 前置通知,至关于BeforeAdvice
    • 就在方法以前执行.没有办法阻止目标方法执行的.
  • @AfterReturning 后置通知,至关于AfterReturningAdvice
    • 后置通知,得到方法返回值.
  • @Around 环绕通知,至关于MethodInterceptor
    • 在能够方法以前和以后来执行的,并且能够阻止目标方法的执行.
  • @AfterThrowing 抛出通知,至关于ThrowAdvice
  • @After 最终final通知,不论是否异常,该通知都会执行
  • @DeclareParents 引介通知,至关于IntroductionInterceptor (不要求掌握)


基于注解方式配置切面

  • 第一步:引入相应jar包.
  • aspectj依赖aop环境,以前导入的AOP jar包须要继续导入。

    • spring-aop-3.2.0.RELEASE.jar
    • com.springsource.org.aopalliance-1.0.0.jar
    • spring-aspects-3.2.0.RELEASE.jar
    • com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  • 第二步:编写被加强的类:

    • UserDao



  • 第三步:使用AspectJ注解形式:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
@Aspect public class MyAspect { @Before("execution(* cn.itcast.spring3.demo1.UserDao.add(..))") public void before(){ System.out.println("前置加强...."); } }


  • 第四步:建立applicationContext.xml
    • 引入aop的约束:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
<?xml version="1.0" encoding="UTF-8"?> <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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 开启AspectJ自动代理--> <aop:aspectj-autoproxy /> </beans>


  • 开启AspectJ自动代理 : <aop:aspectj-autoproxy />

    • 底层就是AnnotationAwareAspectJAutoProxyCreator
  • 定义目标类与切面类

    

    

    
    
    
    
    
    
  • 1
  • 2
<bean id="userDao" class="cn.itcast.spring3.demo1.UserDao"></bean> <bean id="myAspect" class="cn.itcast.spring3.demo1.MyAspect"></bean>


常见切面类编写

@Before前置通知
  • 能够在方法中传入JoinPoint对象,用来得到切点信息


@AfterReturing 后置通知
  • 经过returning属性 能够定义方法返回值,做为参数


@Around 环绕通知
  • around方法的返回值就是目标代理方法执行返回值
  • 参数为ProceedingJoinPoint 能够调用拦截目标方法执行
  • 重点:若是不调用 ProceedingJoinPoint的 proceed方法,那么目标方法就被拦截了


@AfterThrowing 抛出通知
  • 经过设置throwing属性,能够设置发生异常对象参数


@After 最终通知


    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
/** * 切面类:就是切点与加强结合 * @author 姜涛 * */ @Aspect public class MyAspect { @Before("execution(* cn.itcast.spring3.demo1.UserDao.add(..))") public void before(JoinPoint joinPoint){ System.out.println("前置加强...."+joinPoint); } @AfterReturning(value="execution(* cn.itcast.spring3.demo1.UserDao.update(..))",returning="returnVal") public void afterReturin(Object returnVal){ System.out.println("后置加强....方法的返回值:"+returnVal); } @Around(value="MyAspect.myPointcut()") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("环绕前加强...."); Object obj = proceedingJoinPoint.proceed(); System.out.println("环绕后加强...."); return obj; } @AfterThrowing(value="MyAspect.myPointcut()",throwing="e") public void afterThrowing(Throwable e){ System.out.println("很差了 出异常了!!!"+e.getMessage()); } @After("MyAspect.myPointcut()") public void after(){ System.out.println("最终通知..."); } @Pointcut("execution(* cn.itcast.spring3.demo1.UserDao.find(..))") private void myPointcut(){} }


经过@Pointcut为切点命名

  • 在每一个通知内定义切点,会形成工做量大,不易维护,对于重复的切点,可使用@Poingcut进行定义
  • 切点方法:private void 无参数方法,方法名切点名
  • 当通知多个切点时,可使用|| 进行链接


  • 面试: Advisor和Aspect的区别?
    • Advisor:Spring传统意义上的切面:支持一个切点和一个通知的组合.
    • Aspect:能够支持多个切点和多个通知的组合.




使用XML配置切面

  • 第一步:编写被加强的类:

    • ProductDao
  • 第二步:定义切面

  • 第三步:配置applicationContext.xmll

前置通知

  • 代码:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
public void before(){ System.out.println("前置通知..."); }
  • 配置:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
<aop:config> <!-- 定义切点: --> <aop:pointcut expression="execution(* cn.itcast.spring3.demo2.ProductDao.add(..))" id="mypointcut"/> <aop:aspect ref="myAspectXML"> <!-- 前置通知 --> <aop:before method="before" pointcut-ref="mypointcut"/> </aop:aspect> </aop:config>
  • 示例:


后置通知

  • 代码:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
public void afterReturing(Object returnVal){ System.out.println("后置通知...返回值:"+returnVal); }
  • 配置:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
<aop:config> <!-- 定义切点: --> <aop:pointcut expression="execution(* cn.itcast.spring3.demo2.ProductDao.add(..))" id="mypointcut"/> <aop:aspect ref="myAspectXML"> <!-- 后置通知 --> <aop:after-returning method="afterReturing" pointcut-ref="mypointcut" returning="returnVal"/> </aop:aspect> </aop:config>
  • 示例:


环绕通知

  • 代码:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("环绕前加强...."); Object result = proceedingJoinPoint.proceed(); System.out.println("环绕后加强...."); return result; }
  • 配置:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
<aop:config> <!-- 定义切点: --> <aop:pointcut expression="execution(* cn.itcast.spring3.demo2.ProductDao.add(..))" id="mypointcut"/> <aop:aspect ref="myAspectXML"> <!-- 环绕通知 --> **<aop:around method="around" pointcut-ref="mypointcut"/>** </aop:aspect> </aop:config>
  • 示例:


异常通知

  • 代码
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
public void afterThrowing(Throwable e){ System.out.println("异常通知..."+e.getMessage()); }
  • 配置
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
<aop:config> <!-- 定义切点: --> <aop:pointcut expression="execution(* cn.itcast.spring3.demo2.ProductDao.add(..))" id="mypointcut"/> <aop:aspect ref="myAspectXML"> <!-- 异常通知 --> <aop:after-throwing method="afterThrowing" pointcut-ref="mypointcut" throwing="e"/> </aop:aspect> </aop:config>
  • 示例:


最终通知

  • 代码:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
public void after(){ System.out.println("最终通知...."); }
  • 配置:
    

    

    
    
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
<aop:config> <!-- 定义切点: --> <aop:pointcut expression="execution(* cn.itcast.spring3.demo2.ProductDao.add(..))" id="mypointcut"/> <aop:aspect ref="myAspectXML"> <!-- 最终通知 --> <aop:after method="after" pointcut-ref="mypointcut"/> </aop:aspect> </aop:config>
  • 示例:
相关文章
相关标签/搜索