为什么要使用AOP,在编写程序的时候,除了不必关心依赖的组件如何实现,在实际开发过程中,还需要将程序中涉及的公共问题集中解决。AOP是Aspect-Oriented Programming的简称,意思是面向切面编程。是对OOP的补充和完善,比如在开发中,所有的业务方法都需要日志记录、参数验证、事务处理,这些公共的处理如果放在每个业务方法中,系统会变的十分臃肿,即使写成公用类进行调用,也很难维护。散步在系统各处的需要在实现业务系统时关注的事情被称为“切面”,也称之为关注点,AOP的思想就是把这些公用部分从业务方法中提取出来,集中处理。

AOP将软件系统分为两个部分:核心关注点和横切关注点。术语:

1切面:Aspect:切面是系统中抽象出来的某一个模块。

2.连接点:JoinPoint:程序执行过程中某个特定的点,如调用某方法或处理异常时,在SpringAOP中,连接点总是代表着某个方法的执行。

3.通知:Advice:通知是切面具体实现,是放置”切面代码的“类。

4.切入点:PointCut:多个连接点组成一个切入点,可以使用切入点表达式表示。

5.目标对象:Target Object:包含一个连接点的对象,即被拦截的对象。

6.AOP代理:AOP Proxy:AOP框架产生的对象,是通知和目标对象的结合体。

7.织入:Weaving:将切入点和通知结合的过程称之为织入。

package com.sp.IC;

import com.sp.bean.OutOfStockException;

//手机业务接口
public interface PhoneBiz {
	public void buyPhone(int num);//购买手机;
	public void salePhone(int num) throws OutOfStockException;//销售手机
}
package com.sp.bean;

import com.sp.IC.PhoneBiz;

public class PhoneBizImpl implements PhoneBiz{
	//库存属性;
	int num=140;
	public void buyPhone(int num) {
		System.out.println("手机进货"+num+"部");
	}

	public void salePhone(int num) throws OutOfStockException{
		if(this.num<num){
			throw new OutOfStockException("库存不足,客户需要"+num+"部手机,库存只有"+this.num+"部");
		}
		System.out.println("手机销售"+num+"部");
	}
}

package com.sp.bean;

public class OutOfStockException extends Exception {
	//缺货异常;
	public OutOfStockException(String msg){
		super(msg);
	}
}
package com.sp.bean;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

//通知类;
public class LogAspect {
	//前置通知;
	public void before(JoinPoint jp)throws Throwable{
		Object[]args=jp.getArgs();	//目标方法所有参数;
		String methodName=jp.getSignature().getName();	//获得目标方法名称;

		if("buyPhone".equals(methodName)){
			System.out.println(currentTime()+"即将进行进货操作,数量为:"+args[0]);
		}
		if("salePhone".equals(methodName)){
			System.out.println(currentTime()+"即将进行销售操作,数量为:"+args[0]);
		}
	}
	//***********************后置通知***********************
	public void afterReturning(JoinPoint jp)throws Throwable{
		String methodName=jp.getSignature().getName();//获得签名方法的名字;
		if("buyPhone".equals(methodName)){
			System.out.println(currentTime()+"进货操作完毕");
		}
		if("salePhone".equals(methodName)){
			System.out.println(currentTime()+"销售操作完毕");
		}
	}
	public void after(JoinPoint jp)throws Throwable{
		String methodName=jp.getSignature().getName();
		if("buyPhone".equals(methodName)){
			System.out.println(currentTime()+"进货操作完毕,异常也要执行完毕的最终通知...");
		}
		if("salePhone".equals(methodName)){
			System.out.println(currentTime()+"销售操作完毕,异常也要执行完毕的最终通知...");
		}
	}
	//环绕通知;

	public Object aroundTest(ProceedingJoinPoint pjp)throws Throwable{
		String method=pjp.getSignature().getName();
		long begin=System.currentTimeMillis();//当前时间;
		System.out.println(currentTime()+":"+method+"方法开始执行,计时开始!");
		try {
			return pjp.proceed();
		}finally{
			long end=System.currentTimeMillis();
			System.out.println(currentTime()+":"+method+"方法执行完毕,耗时:"+(end-begin)+"毫秒");
		}
	}
	//***********************后置通知结束*********************
	public void afterThrowing(JoinPoint jp,OutOfStockException e){
		String methodName=jp.getSignature().getName();
		System.out.println(currentTime()+methodName+"方法执行,发生缺货异常"+e.getMessage());
	}
	//输出时间的方法;
	public String currentTime(){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
		return sdf.format(new Date());
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- 目标业务对象 -->
	<bean id="phoneBiz" class="com.sp.bean.PhoneBizImpl"></bean>
	<!-- 日志管理切面类 -->
	<bean id="logAspectBean" class="com.sp.bean.LogAspect"></bean>
	<!-- Aop配置 -->
	<aop:config>
		<aop:pointcut expression="execution(void *Phone(int))" id="p1"/>
		<!-- 配置日志管理切面 -->
		<aop:aspect id="logAspect" ref="logAspectBean">
			<!-- 配置日志记录前置通知 -->
			<aop:before method="before" pointcut-ref="p1"/>
			<!-- 配置日志记录后置通知 -->
			<aop:after method="afterReturning" pointcut-ref="p1"/>
			<!-- 配置日志记录异常通知; -->
			<aop:after-throwing method="afterThrowing" pointcut-ref="p1" throwing="e"/>
			<!-- 配置日志记录最终通知 -->
			<aop:after method="after" pointcut-ref="p1"/>
			<!-- 配置日志记录环绕通知 -->
			<aop:around method="aroundTest" pointcut-ref="p1"/>
		</aop:aspect>
	</aop:config>
</beans>

Spring切面之一的更多相关文章

  1. spring切面-单线程简单权限判定

    spring切面简单模拟用户权限判定 需求: 游客:仅注册用户 用户:修改,注册 管理员:删除,查询,修改,注册 1,文件配置 导包 src下创建applicationContext.xml文件配置如 ...

  2. Spring 切面可以应用五种类型的通知?

    Spring 切面可以应用五种类型的通知: before:前置通知,在一个方法执行前被调用. after: 在方法执行之后调用的通知,无论方法执行是否成功. after-returning: 仅当方法 ...

  3. spring切面编程AOP 范例一

    参照网上的spring AOP编程实例进行配置,但是碰到了几个坑.这篇文章重点讲解一下我踩过的两个坑: 1.使用@Service自动装配的时候,基础扫描包配置要正确: 2.xml中切面配置中的exec ...

  4. Spring切面通知执行的顺序(Advice Order)

    问题描述 如果在Spring的程序中同时定义了环绕通知(Around)和前置通知(Before)..那么,有以下问题: 1.怎么让两个切面通知都起作用 2.或者让两者切面按自己指定的顺序进行执行? 3 ...

  5. Spring切面编程步骤

    什么是面向切面编程 面向对象的编程主要注重核心业务,而面向切面编程主要关注一些不是核心的业务,但又是必须的辅助功能,比如一个完整的系统中,记录平时系统运行时抛出的异常,需要我们去记录,以便我们对系统尽 ...

  6. 扩展Spring切面功能

    概述 Spring的切面(Spring动态代理)在Spring中应用十分广泛,例如还有事务管理,重试等等.网上介绍SpringAop源码很多,这里假设你对SpringAop有基本的了解.如果你认为Sp ...

  7. Spring切面编程实践【原创】

    定义 什么叫Spring面向切面编程(AOP),请自行百度,这边就不做详细介绍了. 场景 有两个对象,字典和工程信息Bean,每次新增或修改对象时,记录新增和修改的时间. 基类定义 package m ...

  8. 扩展Spring切面

    概述 Spring的切面(Spring动态代理)在Spring中应用十分广泛,例如还有事务管理,重试等等.网上介绍SpringAop源码很多,这里假设你对SpringAop有基本的了解.如果你认为Sp ...

  9. spring切面拦截实现

    1.建立业务类(英雄战斗实现类) package com.g2.api; public interface Weapon { int attack(); String getName(); } pac ...

  10. 《Java Spring框架》Spring切面(AOP)配置详解

    1.  Spring 基本概念 AOP(Aspect Oriented Programming)称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2 ...

随机推荐

  1. HTTP协议请求信息详解

    通常HTTP消息包括客户机向服务器的请求消息和服务器向客户机的响应消息.客户端向服务器发送一个请求,请求头包含请求的方法.URI.协议版本.以及包含请求修饰符.客户信息和内容的类似于MIME的消息结构 ...

  2. hdu - 6282,2018CCPC湖南全国邀请赛G题,字符串,规律

    HDU – 6282 http://acm.hdu.edu.cn/showproblem.php?pid=6282 by Hzu_Tested 题意:给出两个字符串S和T,只由a,b,c三种字符组成( ...

  3. C#判断字符串中是否有数字

    // <summary> /// 提取字符串中的数字字符串 /// </summary> /// <param name="str"></ ...

  4. python项目通过配置文件方式配置日志-logging

    背景:项目中引入日志是必须的,这里介绍通过配置文件config.ini的方式配置日志 1.新建config.ini 2.添加配置 [loggers]keys=root,ProxyIP [handler ...

  5. Eclipse/myEclipse 代码提示/自动提示/自动完成设置(转)

    一.设置超级自动提示 设置eclipse/myEclipse代码提示可以方便开发者,不用在记住拉杂的单词,只用打出首字母,就会出现提示菜单.如同dreamweaver一样方便. 1.菜单window- ...

  6. c++团队作业工作笔记

    这周时间还比较充裕,所以就有较多的时间来投入团队作业之中. emmmm,由于组长那边感觉完全没动,于是我完成了选英雄的UI界面,到时候给button加上信号就没什么问题. 虽然界面比较简单,但是还是花 ...

  7. 让程序运行更加面向用户——电梯V2.1

    电梯V2.1 GitHub仓库地址 Problem 为程序添加命令行参数(自行利用搜索引擎进行学习). 写成 .cpp .h 文件分离的形式(大多数同学已经达到). 继续完善函数分离.模块化思想. 要 ...

  8. JSON:JavaScript 对象表示法

    JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. ...

  9. mybatis_mysql

    SELECT round(avg(c.AVG_DELAY_TIME)) as AVG_DELAY FROM `result_road_saturation_day` a LEFT JOIN info_ ...

  10. virtualenv是什么?virtualenv的安装及pycharm的配置和使用

    virtualenv是什么? virtualenv是一个创建隔绝的Python环境的工具.virtualenv创建一个包含所有必要的可执行文件的文件夹,用来使用Python工程所需的包.简单的说就是一 ...