AOP是面向切面编程,区别于oop,面向对象,一个是横向的,一个是纵向。

主要解决代码分散和混乱的问题。

1.概念:

切面:实现AOP共有的类

通知:切面类中实现切面功能的方法

连接点:程序被通知的特定位置

切点:连接点的定义

代理:向目标应用通知而产生的对象

2.配置

 

AOP的实现的基本要求:通过接口创建目标的代理。

1。首先是配置文件,2。然后启动扫描器<context:component-scan>和启动AOP注解支持<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

3.之后再逻辑方法类上定义容器管理@Component(“名称”)若不写名称,则默认是类名首字母小写的名字。

3.定义切面类

定义容器管理@Component和@Aspect//切面类

4.定义通知

通知可分为@before前置通知、@After后置通知、@AfterReturning返回通知、@AfterThrowing异常通知、@Around环绕通知。

前置通知:

在方法执行前执行,参数JoinPoint,代表连接点

可以得到参数列表和方法名

通过getNmae和        getAtgs,反回的是object[]类型

后置通知;在方法之后执行,同样可以获得参数列表和方法名

异常通知:

throwing=“接受异常对象规定参数”

 环绕通知:

5.定义切点表达式

@通知类型注解(pointCut=“execution(修饰符的返回值类型 包名.类名.方法名(参数类型))”)

6.公共切点@Pioneertcut

之后再调用切点表达式的时候直接写:通知类型(“公共切点表达式的类名piontCut”)

接口=============================

package com.hanqi.test;

import org.springframework.stereotype.Component;

public interface I_jsq {

	double add(double a ,double b);
double div(double a ,double b); }

  实现类业务逻辑类=========

package com.hanqi.test;

import org.springframework.stereotype.Component;

@Component("my1")//容器管理
public class Myjsq implements I_jsq { //连接点
@Override
public double add(double a, double b) {
//前置通知的连接点 //日志记录
// System.out.println("日志:a = "+a+" ,b = "+b);
System.out.println(a+"+"+b+"="+(a+b)); if(a < 0)
{
throw new RuntimeException("运行时异常");
}
return(a+b);
//后置通知的连接点
}
//连接点
@Override
public double div(double a, double b) {
//日志记录
//System.out.println("日志:a = "+a+" ,b = "+b); //检查
// if(b==0)
// {
// System.out.println("b不能是0");
// return -1;
// }
// else
// {
System.out.println(a+"/"+b+"="+(a/b));
return (a/b); //}
} }

  切面类================

package com.hanqi.test;

import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; //切面类
@Component//容器管理
@Aspect//切面类
@Order(1)
public class LogAspect { //定义公共切点
@Pointcut("execution(* com.hanqi.test.I_jsq.*(..))")
public void pointCut()
{ } //前置通知@Before
@Before("pointCut()")//引用公共切点
public void beforeLog(JoinPoint jp)//通知 JoinPoint表示连接点
{
//连接点的方法名
String ms = jp.getSignature().getName(); //连接点方法的参数
List<Object> ls = Arrays.asList(jp.getArgs()); System.out.println("前置日志,方法名 = "+ms+" 参数列表= "+ls);
} //后置通知
@After("pointCut()")
public void afterLog(JoinPoint jp)
{
//连接点的方法名
String ms = jp.getSignature().getName(); //连接点方法的参数列表
List<Object> ls = Arrays.asList(jp.getArgs()); System.out.println("后置日志,方法名 = "+ms+" 参数列表= "+ls);
} //返回通知
//@AfterReturning(pointcut="execution(* com.hanqi.test.I_jsq.*(..))" ,returning="rtn")
@AfterReturning(pointcut="pointCut()" ,returning="rtn")
public void returningLog(double rtn)
{
//接收返回值
System.out.println("返回值="+rtn);
} //异常通知
@AfterThrowing(pointcut="pointCut()" , throwing="msg")
public void errorLog(Exception msg)
{
System.out.println("异常通知="+msg.getMessage());
} //环绕通知
@Around("pointCut()")//切点表达式,第一个*表示返回类型,第二个*表示方法名
public Object aroundLog(ProceedingJoinPoint pjp)
{
Object rtn= null;
//连接点的方法名
String ms = pjp.getSignature().getName(); //连接点方法的参数
List<Object> ls = Arrays.asList(pjp.getArgs());
//实现四种通知
//1.前置通知
System.out.println("这是环绕通知的前置通知,方法名= "+ms+" 参数="+ls); //调用目标方法
try { rtn= pjp.proceed(); //3.返回通知
System.out.println("这是环绕通知的返回通知,返回值="+rtn ); } catch (Throwable e) {
//4.异常通知
System.out.println("这是环绕通知的异常通知,异常信息="+e.getMessage());
e.printStackTrace();
}
finally
{
//2.后置通知
System.out.println("这是环绕通知的后置通知");
} return rtn;
}
}
package com.hanqi.test;

import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
// 切面类
@Component
@Aspect
@Order(2)
public class CheckAspect { //前置通知
//@Before("execution(* com.hanqi.test.Myjsq.div(double,double))")
@Before("LogAspect.pointCut()")//重用切点表达式
public void beforeCheck(JoinPoint jp)
{
//获取参数
Object[] ob = jp.getArgs(); if((Double.parseDouble(ob[1].toString()))== 0)
{
System.out.println("参数不能等于0");
System.out.println("1=="+Double.parseDouble(ob[1].toString()));
System.out.println("0=="+Double.parseDouble(ob[0].toString()));
}
} }

  测试类

package com.hanqi.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestMain { public static void main(String[] args) {
// TODO 自动生成的方法存根
//I_jsq jsq1 = new Myjsq(); ApplicationContext ac =
new ClassPathXmlApplicationContext("app.xml"); I_jsq jsq1 =(I_jsq)ac.getBean("my1");//通过接口创建代理对象 jsq1.add(123, 456); jsq1.div(200, 0); } }

  

  

Spring-AOP面向切面编程的更多相关文章

  1. 详细解读 Spring AOP 面向切面编程(二)

    本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...

  2. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  3. spring AOP面向切面编程学习笔记

    一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...

  4. 【Spring系列】Spring AOP面向切面编程

    前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...

  5. 从源码入手,一文带你读懂Spring AOP面向切面编程

    之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...

  6. Spring AOP面向切面编程详解

    前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...

  7. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  8. Spring AOP 面向切面编程入门

    什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...

  9. 详细解读 Spring AOP 面向切面编程(一)

    又是一个周末, 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之. ...

  10. Spring Aop面向切面编程&&自动注入

    1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...

随机推荐

  1. Bash 中的 $0 在什么时候不是 argv[0]

    每个 C 程序都有一个 main 函数,每个 main 函数都有一个 argv 参数,这个参数是一个字符串数组,这个数组的值是由该 C 程序的父进程在通过 exec* 函数启动它时指定的. 很多人说 ...

  2. Android 网络编程

    HttpClient 发送get请求 创建一个客户端对象 HttpClient client = new DefaultHttpClient(); 创建一个get请求对象 HttpGet hg = n ...

  3. Problem to be sovled

    Given an array A of N integers, we draw N discs in a 2D plane such that the I-th disc is centered on ...

  4. php的register_globals配置

    1.需求 看ci文档的时候,看到register_globals,要了解这个配置的使用 2.分析 register_globals是PHP.ini里的一个配置,这个配置影响到php如何接收传递过来的参 ...

  5. plist中的中文数据

    直接打印那个值就会显示中文了,你打引整个 plist 文件的内容,则会显示出 NSUTF8 编码后的数据:请放心使用:

  6. git 教程(14)--解决冲突

    人生不如意之事十之八九,合并分支往往也不是一帆风顺的. 准备新的feature1分支,继续我们的新分支开发:

  7. Java中的Serializable接口transient关键字,及字节、字符、对象IO

    1.什么是序列化和反序列化Serialization是一种将对象转为为字节流的过程:deserialization是将字节流恢复为对象的过程. 2.什么情况下需要序列化a)当你想把的内存中的对象保存到 ...

  8. 解决SprngMVC中ResponseBody注解中文乱码

    配置文件前面加入如下结构,版本号3.2.5 <bean class="org.springframework.web.servlet.mvc.method.annotation.Req ...

  9. jQuery 名称冲突

    jQuery 名称冲突 jQuery 使用 $ 符号作为 jQuery 的简介方式. 某些其他 JavaScript 库中的函数(比如 Prototype)同样使用 $ 符号. jQuery 使用名为 ...

  10. jquery input change事件

    input输入框的change事件,要在input失去焦点的时候才会触发 $('input[name=myInput]').change(function() { ... }); 在输入框内容变化的时 ...