SpringAop:
1.加入 jar 包
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
 
commons-logging-1.1.1.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
 
2.在配置文件中加入aop 命名空间.
 
3.基于注解的方式来配置AOP
1)在配置文件中加入如下配置:
<context:component-scan base-package="com.mmz.spring.aop"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
 
2)把横切关注点的代码抽象到切面中,切面首先是一个IOC容器的 bean ,即加入@Component 注解,切面还需要加入 @Aspect 注解,(可以使用@Order 指定切面的优先级)
 
3)在类中声明各种通知注解(@Before,@After,@AfterRunning,@AfterThrowing,@Around)
a.在方法前声明注解,注解里面添加切入点表达式:在 AspectJ 中, 切入点表达式可以通过操作符 &&, ||, ! 结合起来,方法可以添加参数 JoinPoint joinPoint,然后能间接访问链接细节
b.eg.
@Before("execution(public int com.mmz.spring.aop.ArithmeticCalculator.*(int, int))")
public void beforeMethod(JoinPoint joinPoint){
String methdName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methdName+" begins width"+args);
}
 
在方法返回结果之后执行:
@AfterReturning(value="execution(public int com.mmz.spring.aop.*.*(int, int))",returning="result")
public void afterRunningMethod(JoinPoint joinPoint,Object result){
String methdName = joinPoint.getSignature().getName();
System.out.println("The method "+methdName+" ends width "+result);
}

  

 
异常通知,在方法抛出异常之后
@AfterThrowing(value="execution(public int com.mmz.spring.aop.*.*(int, int))",throwing="ex")
public void afterRunningMethod(JoinPoint joinPoint,Exception ex){
String methdName = joinPoint.getSignature().getName();
System.out.println("The method "+methdName+" ends width "+ex);
}
 
重用切入点表达式:
1.定义一个方法,用于声明切入点表达式,该方法中不需要添加其他代码
2.使用@PointCut 来声明切入点表达式
3.后面的其他通知直接使用方法名来使用当前的切入点表达式.
 
4.基于配置文件的方式来配置AOP
1.<!-- 配置业务层的bean -->
<bean id="arithmeticCalculator" class="com.mmz.spring.aop.xml.ArithmeticCalculatorImpl"></bean>
 
2.<!-- 配置切面的bean -->
<bean id="loggingAspect" class="com.mmz.spring.aop.xml.LoggingAspect"></bean>
 
<bean id="validationAspect" class="com.mmz.spring.aop.xml.ValidationAspect"></bean>
 
3.<!-- 配置AOP-->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(* com.mmz.spring.aop.xml.*.*(int, int))" id="pointcut"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-returning method="afterRunningMethod" pointcut-ref="pointcut" returning="result"/>
<aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="ex"/>
</aop:aspect>
 
<aop:aspect ref="validationAspect" order="1">
<aop:before method="validateMethod" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>

Spring框架之AOP的更多相关文章

  1. Spring框架的AOP技术(注解方式)

    1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...

  2. 10.Spring——框架的AOP

    1.Spring 框架的 AOP 2.Spring 中基于 AOP 的 XML架构 3.Spring 中基于 AOP 的 @AspectJ 1.Spring 框架的 AOP Spring 框架的一个关 ...

  3. Spring 框架的 AOP 简介

    Spring 框架的 AOP Spring 框架的一个关键组件是面向方面的编程(AOP)(也称为面向切面编程)框架. 面向方面的编程需要把程序逻辑分解成不同的部分称为所谓的关注点. 跨一个应用程序的多 ...

  4. 使用spring框架进行aop编程实现方法调用前日志输出

    aop编程 之使用spring框架实现方法调用前日志输出 使用spring框架实现AOP编程首先需要搭建spring框架环境: 使用Spring框架实现AOP工程编程之后,不需要我们去写代理工厂了,工 ...

  5. Spring框架之AOP源码完全解析

    Spring框架之AOP源码完全解析 Spring可以说是Java企业开发里最重要的技术.Spring两大核心IOC(Inversion of Control控制反转)和AOP(Aspect Orie ...

  6. Spring框架(4)---AOP讲解铺垫

    AOP讲解铺垫      不得不说,刚开始去理解这个Aop是有点难理解的,主要还是新的概念比较多,对于初学者一下子不一定马上能够快速吸收,所以我先对什么事Aop做一个解释: 首先说明:本文不是自己所写 ...

  7. Spring框架的AOP的底层实现

    1. Srping框架的AOP技术底层也是采用的代理技术,代理的方式提供了两种 1. 基于JDK的动态代理 * 必须是面向接口的,只有实现了具体接口的类才能生成代理对象 2. 基于CGLIB动态代理 ...

  8. Spring 框架的AOP之注解的方式

    1. 环境搭建 1.1 导入 jar 包 Spring 框架的基本开发包(6个); Spring 的传统AOP的开发包 spring-aop-4.3.10.RELEASE org.aopallianc ...

  9. Spring框架及AOP

    Spring核心概念 Spring框架大约由20个功能模块组成,这些模块主分为六个部分: Core Container :基础部分,提供了IoC特性. Data Access/Integration ...

  10. spring框架中AOP思想与各种配置详解

    Spring中提供两种AOP支持:   1.基于代理的经典AOP   2.Aspectj注解配置AOP    首先我们先了解什么是AOP,AOP(Aspect Oriented Programming ...

随机推荐

  1. 基本的文件 I/O

    基本的文件 I/O MSDN 抽象基类 Stream 支持读取和写入字节.Stream 集成了异步支持.其默认实现根据其相应的异步方法来定义同步读取和写入,反之亦然. 所有表示流的类都是从 Strea ...

  2. English Metric Units and Open XML

    English Metric Units and Open XML 在Open XML里使用了English Metric Units(EMUs)来作为度量单位.比如 public class Ext ...

  3. ViewHolder模式超简洁写法

    ViewHolder是什么就不解释了.大家通常怎么写ViewHolder呢? ViewHolder holder = null; if (convertView == null) { convertV ...

  4. wait、notify、notifyAll的阻塞和恢复

    前言:昨天尝试用Java自行实现生产者消费者问题(Producer-Consumer Problem),在coding时,使用到了Condition的await和signalAll方法,然后顺便想起了 ...

  5. PlayFramework 1.2.x 在Controller 中识别JSON提交

    链接 http://stackoverflow.com/questions/6132892/consuming-json-in-play-framework-controller @Global pu ...

  6. JAVA IO NIO

    http://www.cnblogs.com/handsome1013/p/4882862.html http://www.cnblogs.com/dolphin0520/ http://www.cn ...

  7. Swift 字符与字符串

    Swift 的 String 和 Character 类型

  8. [LeetCode] Range Sum Query 2D - Immutable

    Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fac ...

  9. What's New in iOS9 iOS9功能改进

    What's New in iOS9 This article summarizes the key developer-related features introduced in iOS 9, w ...

  10. Android之线程回掉更新ui

    一:工作线程中的回掉更新UI public class MainActivity extends AppCompatActivity { private int i; private Callback ...