什么是AOP?
基本概念
切面(aspect):横切关注点被模块化的特殊对象。
通知(advice):切面必须要完成的工作。切面中的每个方向称之为通知。通知是在切面对象中的。
目标(target):被通知的对象。
代理(proxy):向目标对象应用通知后创建的对象。

连接点(joinpoint):目标对象的程序执行的某个特定位置。如某个方向调用前,调用后的位置。包括两个信息:1.目标程序的哪个方法?2.方法执行前还是执行后?
切点(pointcut):每个类会有多个连接点,AOP通过切点定位到特定的边接点。
类比,连接点相当于数所成方圆 中的记录,而切点相当于查询条件。一个切点匹配多个连接点。

一、注解(Annotation)方式实现Spring面向切面

  首先定义一个接口(为了不违反开闭原则和更好的可扩展性,目标对象实际上是实现了已定义好的某个接口)

package com.entities;

public interface IHuman {
public void eat();
public void sleep();
}

  创建一个继承自IHuman接口的类

  为该类加上@Compoent注解

package com.entities;

import org.springframework.stereotype.Component;

@Component
public class Chinese implements IHuman { @Override
public void eat() {
System.out.println("中国人在吃饭");
//int i =1/0;
} @Override
public void sleep() {
System.out.println("中国人在睡觉"); } }

  定义一个切面类

  为该类加上@Aspect注解@Compoent注解

package com.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Aspect
@Component
public class HumanAOP {
@Before("execution(* com.entities.*.eat(..))")//before是在方法执行之前
public void beforeeat(){
System.out.println("先洗 手");
}
@After(value = "execution(* com.entities.*.eat(..))")//after是在方法执行之后
public void aftereat(){
System.out.println("去漱口");
}
@AfterThrowing(value="execution(* com.entities.*.eat(..))",throwing="ex")//AfterThrowing是在方法报错抛异常时执行
public void except(Exception ex){
System.out.println(ex.getMessage());
System.out.println("撑死了");
}
@AfterReturning(value="execution(* com.entities.*.eat(..))",returning="result")//AfterReturning在目标方法执行成功后执行的通知
public void fanhui(Object result){
System.out.println(result);
System.out.println("吃完了");
}
@Before("execution(* com.entities.*.sleep(..))")
public void beforesleesp(){
System.out.println("先洗澡");
}
}

  创建xml文件

  <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>自动为匹配的类生成代理对象

<?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: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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 自动扫描包下的类,并将其实例化。多个包之间用,隔开 -->
<context:component-scan base-package="com.entities,com.aop"></context:component-scan>
<!-- 配置文件中启动AspectJ的注解功能 ,默认是false,要将其改为true -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>

  定义一个main函数类进行效果实现

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.entities.Chinese; public class Test { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Chinese c= context.getBean(Chinese.class);
c.eat();
c.sleep(); } }
先洗 手
中国人在吃饭
去漱口
null
吃完了
先洗澡
中国人在睡觉

二、XML方式实现Spring面向切面

  同理,先定义一个接口

package com.entities;

public interface IHuman {
public void eat();
public void sleep();
}

  创建一个继承自IHuman接口的类

package com.entities;

public class Chinese implements IHuman {

    @Override
public void eat() {
System.out.println("中国人在吃饭");
//int i =1/0;
} @Override
public void sleep() {
System.out.println("中国人在睡觉"); } }

  定义一个切面类

package com.aop;

public class HumanAOP {

    public void beforeeat(){
System.out.println("先洗 手");
} public void aftereat(){
System.out.println("去漱口");
} public void except(Exception ex){
System.out.println(ex.getMessage());
System.out.println("撑死了");
} public void fanhui(Object result){
System.out.println(result);
System.out.println("吃完了");
} public void beforesleesp(){
System.out.println("先洗澡");
}
}

  创建xml文件

<?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: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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"
default-autowire="byName"
> <bean id="chinese" class="com.entities.Chinese"></bean>
<bean id="gou" class="com.entities.Gou"></bean>
<bean id="humanaop" class="com.aop.HumanAOP"></bean> <aop:config>
<aop:pointcut expression="execution(* com.entities.*.eat())" id="aftereat"/>
<aop:pointcut expression="execution(* com.entities.*.eat())" id="beforeeat"/> <aop:aspect ref="humanaop">
<aop:after method="aftereat()" pointcut-ref="aftereat"/>
<aop:before method="beforeeat()" pointcut-ref="beforeeat"/>
</aop:aspect>
</aop:config> </beans>

  定义一个main函数来执行

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.entities.Chinese;
import com.entities.IHuman; public class Test { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
IHuman c= (IHuman)context.getBean("chinese");
c.eat(); } }
先洗 手
中国人在吃饭
去漱口

通知分类:
前置通知(@Before) -- 在目标方法执行前执行的通知。
后置通知(@After) -- 在目标方法执行后执行的通知。无论是否发生异常。后置通知中,无法读取目标方法返回的结果。
返回通知(@AfterReturnning) --在目标方法执行成功后执行的通知。在返回通知中可以访问目标返回结果的。
@AfterReturnning(value="execution(* com.itnba..*(..))",returning="result")
public void afterRun(Object result){
System.out.println(result);
}

异常通知(@AfterThrowing) -- 在目标方法执行出错后执行的通知。
@AfterThrowing(value="execution(* com.itnba..*(..))",throwing="ex")
public void afterThrowing(Exception ex){
System.out.println(ex.getMessage());
}

环绕通知(@Around) -- 需要切面方法携带ProceedingJoinPoion参数,类似于一个完整的动态代理,环绕通知必须要有一个返回值,是目标方法的返回值。

@Around("execution(* com.itnba..*(..))")
public object aroundAdvice( ProceedingJoinPoint pjp){ object obj = null;
try{
//做前置通知
obj = pjp.proceed();
//做返回通知
}
catch(Exception ex){
//做异常通知
}
//做后置通知
return obj;
}

添加日志:

切面方法可以加入参数(JoinPoint) joinPost.getSignature().getXXX()获得相关方法信息
切面方法中可以加入Object参数,用来获得目标方法的返回值(只对返回通知起作用)

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-boot】spring aop 面向切面编程初接触--切点表达式

    众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...

  5. 【spring-boot】spring aop 面向切面编程初接触

    众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...

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

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

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

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

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

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

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

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

随机推荐

  1. LINQ 基础语句

    去全部集合 using (dat0216DataContext con = new dat0216DataContext()) { //LoList   是转换成  List集合 List<Us ...

  2. C#语言基础 Main 函数中的输出输入

    C# 是一门面向对象的编程语言,保留了C  C++等等强大功能,但是它与 Java 非常相似,有许多强大的编程功能,它是微软(Microsoft)专门为.NET应用而开发的一门语言. 也就是人与计算机 ...

  3. POJ 3252 Round Numbers (区间DP,基础)

    题意: 统计区间[L,R]有多少个数,其二进制表示法中的0的个数不少于1的个数?(不允许前缀0) 思路: 状态表示为 [当前第几位][总位数][1的个数],最后判断一下1的个数是否满足条件,要注意前导 ...

  4. POJ 1845 Sumdiv (数学,乘法逆元)

    题意: 给出数字A和B,要求AB的所有因子(包括AB和1)之和 mod 9901 的结果. 思路: 即使知道公式也得推算一阵子. 很容易知道,先把分解得到,那么得到,那么的所有因子之和的表达式如下: ...

  5. codeforce Gym 100342H Hard Test (思考题)

    题意:构造让Dijkstra单源最短路算法有效松弛次数最多的数据... 题解:构造,题意换种说法就是更新晚的路径要比更新早的路径短.因为所有点都会更新一次,那么按照更新时间形成一条链,即到最后一个点的 ...

  6. python_105_类的特殊成员方法

    aa.py class C(): def __init__(self): self.name='QiZhiguang' 类的特殊成员方法: # 1. __doc__ 表示类的描述信息 class Do ...

  7. 【转】绝对不要在树莓派上使用无源的HDMI→VGA视频转换器

    http://www.guokr.com/post/521521/ 树莓派由于BCM2835的限制,仅有HDMI和复合视频两种输出形式.所以对于使用VGA显示器的广大用户,HDMI转VGA转换器就成了 ...

  8. JavaScript——图片懒加载

    前言 有一个朋友问我这个问题,刚好有时间,现在就简单的写个Demo~ github | https://github.com/wangyang0210/bky/tree/picLoadLazy 内容 ...

  9. ios 设计模式总结

    设计模式:备注:消息传递模型(Message Passing)是Objective-C语言的核心机制.在Objective-C中,没有方法调用这种说法,只有消息传递.在C++或Java中调用某个类的方 ...

  10. 【转】Matlab的regionprops详解

    matlab函数_连通区域 1. matlab函数bwareaopen──删除小面积对象格式:BW2 = bwareaopen(BW,P,conn)作用:删除二值图像BW中面积小于P的对象,默认情况下 ...