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源码 ...
随机推荐
- elasticsearch使用bulk实现批量操作
本篇文章提供ES原生批量操作语法及使用bulk批量操作文档.文章依旧提供语法,具体实现大家根据语法,在对应处进行替换即可 一.原生批量获取文档 1.获取指定文档值(1) 语法: GET /_mget ...
- VRS的GPS/BDS双系统网元固定存在的问题
问题如下:部分网元固定卫星数少于2个. 另外:北方xinkong的网元组网也存在问题
- VS2015安装与C++进行简单单元测试
1:VS2015是微软最新发布的编译器,http://www.itellyou.cn/这是我们的北航大神助教提供的下载网址,以前我们都是自己在网上找,找到的总不是那么如意,这下大神助教提供的网址就好好 ...
- Spring消息中间件ActiveMQ
1:下载ActiveMQ (apache-activemq-5.15.6) 2:下载后的结构如下 3:启动activeMQ 可以通过cmd控制台的方式,也可以通过注册services服务启动的方式 3 ...
- [ADS]An installation support file could not be installed
ADS:ARM Developer Suits 错误:An installation support file could not be installed 描述: 之前安装了一个不能用的ADS的版本 ...
- mysql 拒绝访问的解决办法
java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect to thi ...
- Django的settings配置
静态文件 STATIC_URL = '/static/' # 别名 STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static'), os.path.join ...
- ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO) ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: YES)
windows下,以上两个错误的解决方法 工具/原料 windows 8 MySql 方法/步骤 找到配置文件my.ini ,然后将其打开,可以选择用记事本打开 打开后,搜索mysq ...
- k8s搭建问题(1)--OOMKilled
kubectl describe pods ****** --namespace=****** 现象 Host Port: /TCP State: Waiting Reason: CrashLoopB ...
- httpclient实现的get请求及post请求
导出mven依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId& ...