Spring AOP:面向切面编程,AspectJ,是基于spring 的xml文件的方法
导包等不在赘述;
建立一个接口:ArithmeticCalculator,没有实例化的方法;
package com.atguigu.spring.aop.impl.panpan;
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);
}
建立一个类:ArithmeticCalculatorImpl 继承于接口:ArithmeticCalculator,实现接口中没有实例化的方法
package com.atguigu.spring.aop.impl.panpan;
import org.springframework.stereotype.Component;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@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;
}
}
建立类:ArithmeticCalculatorLoggingProxy,实例化一些面向切面编程的方法
package com.atguigu.spring.aop.impl.panpan; import java.util.Arrays; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class ArithmeticCalculatorLoggingProxy { //前置通知的方法,在xml文件中可以通过method这个属性获取该方法的方法名,joinPoint是切点,
public void beforeMethod(JoinPoint joinPoint){
//获取方法名,和参数值,参数值要多个,所以用数组集合的方法
String methodName=joinPoint.getSignature().getName();
Object[] args=joinPoint.getArgs(); System.out.println("The method "+methodName+" begains "+Arrays.asList(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+ " *** ends :"+result);
} //异常通知的方法
public void afterThrowing(JoinPoint joinPoint, Exception e){
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+ " Exception :"+ e);
} //环绕通知的
public Object aroundMethod(ProceedingJoinPoint pjp){
Object result=null;
//获取方法名
String methodName=pjp.getSignature().getName();
try {
//前置通知,Arrays.asList(pjp.getArgs())为该方法的参数个数,为数组集合
System.out.println("The method "+methodName+" begains "+Arrays.asList(pjp.getArgs())); //执行目标方法
result=pjp.proceed(); //返回通知
System.out.println("The method "+methodName+ " ends with :"+result); } catch (Throwable e) {
//异常通知
System.out.println("The method " +methodName+ " occurs exception "+ e);
e.printStackTrace();
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;
}
}
建立spring的xml文件,进行bean和AOP的配置
<!-- 配置bean ,全类名是继承接口类的全类名-->
<bean id="arithmeticCalculator"
class="com.atguigu.spring.aop.impl.panpan.ArithmeticCalculatorImpl"></bean> <!-- 配置切面的bean ,全类名是实现切面编程的类全类名-->
<bean id="arithmeticCalculatorLoggingProxy"
class="com.atguigu.spring.aop.impl.panpan.ArithmeticCalculatorLoggingProxy"></bean> <!-- 配置AOP -->
<aop:config>
<!-- 配置切点表达式,全类名为接口的全类名 -->
<aop:pointcut expression="execution(* com.atguigu.spring.aop.impl.panpan.ArithmeticCalculator.*(int,int))"
id="pointcut"/> <!-- 配置切面及通知 method中的为方法名,-->
<aop:aspect ref="arithmeticCalculatorLoggingProxy" order="1">
<aop:before method="beforeMethod" pointcut-ref="pointcut"/>
<aop:after method="afterMethod" pointcut-ref="pointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
<aop:around method="afterMethod" pointcut-ref="pointcut" />
</aop:aspect>
</aop:config>
建立类Main,进行测试
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.test.xml");
//ArithmeticCalculator,是一个接口类,注解的是继承该接口的类
ArithmeticCalculator impl=(ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
System.out.println(impl.getClass().getName());
int result=impl.add(12,2);
System.out.println(result);
double result2=impl.div(12, 2);
System.out.println(result2);
Spring AOP:面向切面编程,AspectJ,是基于spring 的xml文件的方法的更多相关文章
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- 【原创】Android AOP面向切面编程AspectJ
一.背景: 在项目开发中,对 App 客户端重构后,发现用于统计用户行为的友盟统计代码和用户行为日志记录代码分散在各业务模块中,比如在视频模块,要想实现对用户对监控点的实时预览和远程回放行为进行统计, ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- spring AOP面向切面编程学习笔记
一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...
- 【Spring系列】Spring AOP面向切面编程
前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...
- Spring AOP面向切面编程详解
前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...
- Spring Aop面向切面编程&&自动注入
1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...
- 从源码入手,一文带你读懂Spring AOP面向切面编程
之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...
- Spring AOP 面向切面编程相关注解
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...
- Spring AOP 面向切面编程入门
什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...
随机推荐
- FTP规范
FTP协议命令+返回值+返回值解析 FTP message format:FTP commands are Telnet strings terminated by the Telnet end of ...
- 01分数规划poj2728(最优比例生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21766 Accepted: 6087 Desc ...
- [transferred] javascript exception handling.
my error handling clause: window.onerror = function (errorMessage, scriptURI, lineNumber, columnNumb ...
- SpringMvc:视图和视图解析器
请求处理方法执行完成后,最终返回一个ModelAndView对象,对于返回String,View或ModelMap等类型的处理方法,SpringMvc也会在内部将它们装配成一个ModelAndView ...
- 使用javabeen的好处
什么是javabeen? javaBean在MVC设计模型中是model,又称模型层, 在一般的程序中,我们称它为数据层, 就是用来设置数据的属性和一些行为,然后提供获取属性和设置属性的get/set ...
- JavaScript中Date(日期对象),Math对象--学习笔记
Date对象 1.什么是Date对象? 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 语法:var Udate=new Date(); 注:初始值为当前时间(当前电脑系统 ...
- mybatis(一)安装
1.创建web项目,添加jar包 2.创建实验表user_t 3.在src下创建conf.xml文件,如下 <?xml version="1.0" encoding=&quo ...
- 夺命雷公狗-----tp中遇到数据乘积的问题的遇见
昨晚遇到了一个很神奇的问题, 然后打印出来的结果是 ) LIMIT , 然后dump出来的结果有点无语,他是10*10这样的倍增的,如果数据量大,分分钟直接可以让服务器直接死掉.... 想这问题我想了 ...
- ahb2apb和apb2apb async bridge
AHB 3.0目前不支持security world. AHB到APB的async bridge主要包括三个部分: 1)AHB domain 1)产生信号hactive = HSEL & HT ...
- [javascript] ajaxfileupload.js 跨域上传文件
原文地址: http://www.ueffort.com/jqueryajaxfileupload-js-duo-wen-jian-shang-chuan-chuan-zhi-kua-yu/ 跨域 这 ...