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源码 ...
随机推荐
- Android 用webService产生java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive错误的解决(转)
在做android Webservice开发的时候一般情况下大家接受webservice服务器返回值的时候都是使用 SoapObject soapObject = (SoapObject) enve ...
- Beta冲刺(4/5)(麻瓜制造者)
今日已完成 邓弘立:完成了商品管理(下架)和搜索功能 符天愉:完成了后台管理员界面的登录和其他视图的载入 江郑:昨天来决定跨域执行请求,后台参考一些意见以后,操作起来没有那么容易实现,和队友交流以后本 ...
- 【转】wampserver2.5 apache2.4.9配置https 图文
注: 我的wamp环境是2.5版本,apache是2.4.9 装在本地C:wamp 操作系统windows10 1.进入wamp->bin->apache->apache2.4. ...
- PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- Echo团队Alpha冲刺随笔 - 第四天
项目冲刺情况 进展 前端:完成了差不多一半,小程序部分界面基本完工,WEB端也完成了一部分 后端:也完成了大半了 问题 后端文件上传还没解决 心得 进度还行,团队配合都还不错 今日会议内容 黄少勇 今 ...
- EasyUI tab
1.新增tab2.关闭tab3.右键菜单(关闭.关闭所有.关闭其它.关闭右侧.关闭左侧) //双击关闭tab $(document).on("dblclick", ".t ...
- not annotated with HTTP method type (ex. GET, POST) 问题解决
大多数情况是真的而没有写method = RequestMethod.GET.POST等注解, 有时这么写了也报类似异常,如下 @FeignClient("microservice-prov ...
- android精品开源项目整理
转载地址:http://www.eoeandroid.com/thread-311366-1-1.html 前言:无论你是android的初学者,还有是Android开发了好多年的高手,可能都会有很多 ...
- node通过http.request向其他服务器上传文件
function upload(callback) { let boundaryKey = '----' + new Date().getTime(); // 用于标识请求数据段 let option ...
- Subversion 1.8.9 ( SVN Client ) 安装最新版本的svn客户端
For CentOS7 Users: [WandiscoSVN] name=Wandisco SVN Repo baseurl=http://opensource.wandisco.com/cento ...