a simple example for spring AOP
/**
* 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的更多相关文章
- Spring AOP + AspectJ annotation example
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- Spring AOP Example – Advice
Spring AOP + AspectJ Using AspectJ is more flexible and powerful. Spring AOP (Aspect-oriented progra ...
- Spring AOP + AspectJ Annotation Example---reference
In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simp ...
- Spring AOP With AspectJ
一.AOP和拦截器 某些情况下,AOP和拦截器包括Filter能够实现同样的功能,一般都是请求即controller层的操作,这三个执行顺序为Filter>Interceptor>AOP, ...
- JavaEE学习之Spring AOP
一.基本概念 AOP——Aspect-Oriented Programming,面向切面编程,它是spring框架的一个重要组成部分.一般的业务逻辑都有先后关系,我们可以理解为纵向关系,而AOP关注的 ...
- Spring AOP 不同配置方式产生的冲突问题
Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...
- Spring技术内幕:Spring AOP的实现原理(五)
7.Advice通知的实现 AopProxy代理对象生成时,其拦截器也一并生成.以下我们来分析下Aop是怎样对目标对象进行增强的.在为AopProxy配置拦截器的实现中,有一个取得拦截器配置过程,这个 ...
- (一)spring aop的两种配置方式。
sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...
- 按照自己的思路研究Spring AOP源码【2】
目录 问题的提出 哪一步导致了顺序的改变 AbstractAdvisorAutoProxyCreator.sortAdvisors()方法 总结 问题的提出 上面这篇文章介绍了Spring AOP源码 ...
随机推荐
- (9)Python循环结构
- C/C++每日小练(七)——墓地雕塑
墓地雕塑 题目描写叙述: 在一个周长为10000的圆上等距分布着n个雕塑. 如今又有m个新雕塑增加(位置能够任意放).希望全部n+m个雕塑在圆周上均匀分布.这就须要移动当中一些原有的雕塑.要求n个雕塑 ...
- 转://执行impdp时出现ORA-39154错误的解决案例
问题描述如下导出过程正常:expdp \"/ as sysdba\" tables=user_a.t directory=mydir dumpfile=t.dmp logfile= ...
- (四) 天猫精灵接入Home Assistant-ESP-WIFI模块通过mqtt协议接入HASS
总过程 1 ESP8266上电后,初始化 连接MQTT服务器 发布自身配置信息----hass自动发现该设备 订阅hass的命令话题---接收命令 发布hass的状态话题---返回自身状态 2 ESP ...
- <数据结构与算法分析>读书笔记--数学知识复习
数学知识复习是<数据结构与算法分析>的第一章引论的第二小节,之所以放在后面,是因为我对数学确实有些恐惧感.不过再怎么恐惧也是要面对的. 一.指数 基本公式: 二.对数 在计算机科学中除非有 ...
- yum问题解决
错误信息1:There was a problem importing one of the Python modulesrequired to run yum. The error leading ...
- How enable deprecated functions in Qt5
问题:Qt5中如何启用以 QT_DEPRECATED_SINCE标识的已经废弃/过时的方法? I want to port a Qt4 program to Qt5 and some function ...
- centos6安装tomcat8.5
//参考https://www.cnblogs.com/xdp-gacl/p/4097608.html [root@192 ~]# mount /dev/sr0 /mnt/usb1[root@192 ...
- C#位运算实际运用
前言 前几天写了一篇关于c#位操作,c#位运算基本概念与计算过程 最后提到一个实际问题 需求:C# 用两个short,一个int32拼成一个long型 要求:现在有两个short和一个int,需要拼成 ...
- Android 工程引入自定义Library后,工程无法识别Library中的类
这个问题有点神啊. 在工程中导入第三方类库包(自定义Library)本来运行的好好的,突然间所有引用的Library中的类都无法在工程中引用了,一个劲的打红叉,eclipse也重启了,项目也clean ...