Spring AOP(面向切面)
什么是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(面向切面)的更多相关文章
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- spring AOP面向切面编程学习笔记
一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...
- 【spring-boot】spring aop 面向切面编程初接触--切点表达式
众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...
- 【spring-boot】spring aop 面向切面编程初接触
众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...
- 【Spring系列】Spring AOP面向切面编程
前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...
- 从源码入手,一文带你读懂Spring AOP面向切面编程
之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...
- Spring AOP面向切面编程详解
前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...
- Spring AOP 面向切面编程相关注解
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...
随机推荐
- HDU Rabbit and Grass 兔子和草 (Nim博弈)
思路:简单Nim博弈,只需要将所给的数字全部进行异或,结果为0,则先手必败.否则必胜. #include <iostream> using namespace std; int main( ...
- Java和ABAP中的几种引用类型的分析和比较
Java编程语言中几种不同的引用类型是面试时经常容易被问到的问题:强引用,软引用,弱引用,虚引用. 其实除了Java之外,某些 其他编程语言也有类似概念,比如ABAP.今天我们就来比较一下. 根据AB ...
- C#实现灰度图像和彩色图像的4种镜像
一:灰度图像的水平镜像核心代码: 二:灰度图像的竖直镜像 核心代码:三:彩色图像的水平镜像 核心代码: 四:彩色图像的竖直镜像 核心代码:
- Codeforces Round #321 (Div. 2) B. Kefa and Company (尺取)
排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...
- Python3之偏函数
通过设定参数的默认值,可以降低函数调用的难度.偏函数可以做到这一点 int()函数可以把字符串转换成十进制整数,当传入字符串时,int()默认把字符串为十进制 >>> int('12 ...
- mysql 速度检索
授权GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'127.0.0.1' IDENTIFIED BY 'zabbixpwd' WITH GRANT OPTI ...
- 2018.2.25 关于JavaScript
关于JavaScript 1.数组的归约函数reduce(function(PREV,CUR,I){})会从做导游进行迭代,每次返回的值为下一次的prev参数. 2.在循环遍历数组时若是想在找到结果后 ...
- CPP-基础:有关调用约定
在C语言中,假设咱们有这样的一个函数:int function(int a,int b) 调历时只有用result = function(1,2)的方法就能利用这个函数.然而,当高档语言被编译成计算机 ...
- RenderBody,RenderPage和RenderSection
1. RenderBody 在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面中,会看到<body>标签里 ...
- Bootstrap历练实例:大小Well
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...