/**
* Created by Administrator on 2015/11/25.
* a interface
*/
public interface ArithmeticCalculator{
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
public ArithmeticCalculatorImpl(){}//无参数构造器
@Override
public int add(int i, int j) {
int result=i+j;
return result;
} @Override
public int sub(int i, int j) {
int result=i-j;
return result;
} @Override
public int mul(int i, int j) {
int result=i*j;
return result;
} @Override
public int div(int i, int j) {
int result=i/j;
return result;
}
}
public class LoggingAspect {
//前置通知。
public void beforeMethod(JoinPoint joinPoint){
String methodname=joinPoint.getSignature().getName();
List<Object> args= Arrays.asList(joinPoint.getArgs());
System.out.println("The Method name is "+methodname+" args is:"+args);
} //后置通知。
public void AfterMethod(JoinPoint joinPoint){
String methodname=joinPoint.getSignature().getName();
System.out.println("The Method "+methodname+" ends!");
} //返回通知,可以访问到方法的返回值。
public void afterReturning(JoinPoint joinPoint,Object result){
String methodname=joinPoint.getSignature().getName();
System.out.println("The Method "+methodname+" end with:"+result);
} //异常通知
public void afterThrowing(JoinPoint joinPoint,Exception e){
String methodname=joinPoint.getSignature().getName();
System.out.println("The Method "+methodname+" occurs excetion:"+e);
}
}
public class Main {
public static void main(String[]args){
//貌似java的自动代理机制只能代理接口里面的方法。有待验证。AOP好像也只能代理接口里面的方法
//java的动态代理是要代理一大堆类,用类你怎么实现这个功能呢
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
//不明白为什么一定要用接口来接受bean的实例,换了实现类会跑异常
ArithmeticCalculator arithmeticCalculator=(ArithmeticCalculator)ctx.getBean("arithmeticCalculatorImpl");
arithmeticCalculator.add(100, 20);
arithmeticCalculator.div(9, 3);
}
} spring里面的配置如下:
<!--配置一个普通的bean-->
<bean id="arithmeticCalculatorImpl" class="Spring_AOP.Aophelloworld.ArithmeticCalculatorImpl"></bean> <!--配置一个普通的bean-->
<bean id="loggingAspect" class="Spring_AOP.Aophelloworld.LoggingAspect"></bean> <!--配置Aop信息-->
<aop:config>
<!--配置切面表达式-->
<!--第一个*是public int 表示任意返回类型,接着是全类名.方法(int,int),这里第二个*表示
所有的方法,(..)表示任意类型的参数-->
<aop:pointcut id="pointcut" expression="execution(* Spring_AOP.Aophelloworld.ArithmeticCalculator.*(..))"/>
<!--配置切面通知-->
<aop:aspect ref="loggingAspect" order="1"><!--order用来配置切面优先级-->
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:after method="AfterMethod" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
</aop:aspect>
</aop:config>

a simple example for spring AOP的更多相关文章

  1. Spring AOP + AspectJ annotation example

    In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...

  2. Spring AOP Example – Advice

    Spring AOP + AspectJ Using AspectJ is more flexible and powerful. Spring AOP (Aspect-oriented progra ...

  3. Spring AOP + AspectJ Annotation Example---reference

    In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...

  4. Spring AOP With AspectJ

    一.AOP和拦截器 某些情况下,AOP和拦截器包括Filter能够实现同样的功能,一般都是请求即controller层的操作,这三个执行顺序为Filter>Interceptor>AOP, ...

  5. JavaEE学习之Spring AOP

    一.基本概念 AOP——Aspect-Oriented Programming,面向切面编程,它是spring框架的一个重要组成部分.一般的业务逻辑都有先后关系,我们可以理解为纵向关系,而AOP关注的 ...

  6. Spring AOP 不同配置方式产生的冲突问题

    Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...

  7. Spring技术内幕:Spring AOP的实现原理(五)

    7.Advice通知的实现 AopProxy代理对象生成时,其拦截器也一并生成.以下我们来分析下Aop是怎样对目标对象进行增强的.在为AopProxy配置拦截器的实现中,有一个取得拦截器配置过程,这个 ...

  8. (一)spring aop的两种配置方式。

    sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...

  9. 按照自己的思路研究Spring AOP源码【2】

    目录 问题的提出 哪一步导致了顺序的改变 AbstractAdvisorAutoProxyCreator.sortAdvisors()方法 总结 问题的提出 上面这篇文章介绍了Spring AOP源码 ...

随机推荐

  1. 最长公共前缀的golang实现

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 输入: ["flower","flow",&quo ...

  2. 用Python读写Excel文件的方式比较

    虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TAB分割的文本文件(TSV),再在Ex ...

  3. php 安装xdebug进行调试(phpstorm)

    一.下载xdebug xdebug官网:https://xdebug.org/download.php 在选择下载哪个版本的xdebug的时候需要注意了,下面有两种方法,让你准确的下载自己环境对应的x ...

  4. 【洛谷】【计数原理+Floyed】P1037 产生数

    [题目描述:] 给出一个整数 n \((n<10^{30})\) 和 k 个变换规则\((k≤15)\) . 规则: 一位数可变换成另一个一位数: 规则的右部不能为零. 例如: n=234 .有 ...

  5. exit status 3221225477 npm run dev 报错

    Fatal error in , line 0 # Check failed: U_SUCCESS(status). # # # #FailureMessage Object: 000000B5882 ...

  6. luogu P4781 【模板】拉格朗日插值

    嘟嘟嘟 本来以为拉格朗日插值是一个很复杂的东西,今天学了一下才知道就是一个公式-- 我们都知道\(n\)个点\((x_i, y_i)\)可以确定唯一一个最高次为\(n - 1\)的多项式,那么现在我们 ...

  7. Oracle 11gR2使用RMAN duplicate复制数据库

    11g的RMAN duplicate 个人感觉比10g的先进了很多,10g需在rman备份的基础上进行复制,使用RMAN duplicate创建一个数据完全相同但DBID不同的数据库.而11g的RMA ...

  8. api接口简短实力

    无参数传递 public function actionJiekou(){ $url = 'http://wap.guoshihui.com/_static/wap/video/startVideo. ...

  9. 0 ardunio processing 显示三轴数据

    http://blog.csdn.net/Fortware/article/details/38307799 /** * Mixture * by Simon Greenwold. * * Displ ...

  10. MATLAB的两种移位运算

    MATLAB的两种移位运算: 1)circshift矩阵移位 circshift:循环移位数组 语法:B = circshift(A,shiftize) 说明: B  = circshift(A,sh ...