05_Spring AOP原理
理解AOP相关概念
Target(目标对象):代理的目标对象
Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点.(目标对象中,所有可以增强的方法)
Pointcut(切入点/切点):所谓切入点是指我们要对哪些Joinpoint进行拦截(目标对象中,确定要增强的方法).
Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(增强的代码)
Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程.(将通知应用到切点的过程)
Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类(将通知织入到目标对象之后,形成代理)
Aspect(切面): 是切入点和通知的结合(切点+通知)
一个线是一个特殊的面。
一个切入点和一个通知,组成成一个特殊的面。
注意:通知有: 前置通知,后置通知(出现异常不会调用),环绕通知,异常拦截通知,后置通知(出现异常会调用)
Spring AOP 编程
从spring容器获得目标类,如果配置aop,spring将自动生成代理。
导入jar包spring-framework-3.0.2.RELEASE-dependencies\org.aspectj\com.springsource.org.aspectj.weaver\1.6.8.RELEASE
第一种基于xml配置
1.配置通知
public class MyAdvice {
//前置通知
public void before(){
System.out.println("前置通知");
}
//后置通知 出现异常不会调用
public void afterreturning(){
System.out.println("后置通知,出现异常不会调用");
}
//环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕之前的部分");
Object obj = pjp.proceed();//调用原方法
System.out.println("环绕之后的部分");
return obj;
}
//异常拦截通知
public void afterException(){
System.out.println("出现异常");
}
//后置通知(出现异常会调用)
public void after(){
System.out.println("后置通知(出现异常会调用)");
}
}
2.配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- aop -->
<!-- 配置目标对象 -->
<bean name="userService" class="com.service.userServiceImpl"></bean>
<!-- 配置通知 -->
<bean name="myAdvice" class="com.aop.MyAdvice"></bean>
<!-- 配置通知织入到目标对象的方法 -->
<aop:config>
<!-- 配置切入点 即要增强那个方法 -->
<!-- 方法书写规则:
com.service.UserServiceImpl.save()
* com.service.UserServiceImpl.*(..)
* com.service.*.*(..)
-->
<!--切点-->
<aop:pointcut expression="execution(public void com.service.userServiceImpl.save())" id="poincut"/>
<!-- 切面 -->
<aop:aspect ref="myAdvice">
<aop:after method="after" pointcut-ref="poincut"/>
<aop:after-returning method="afterreturning" pointcut-ref="poincut"/>
<aop:after-throwing method="afterException" pointcut-ref="poincut"/>
<aop:around method="around" pointcut-ref="poincut"/>
<aop:around method="before" pointcut-ref="poincut"/>
</aop:aspect>
</aop:config>
</beans>
测试类demo
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/aop/aop.xml")
public class Demo {
@Resource(name = "userService")
private userService userService;
@Test
public void test(){
userService.save();
}
}
第二种基于Annotation配置
1.配置通知
@Component("myAdvice")
@Aspect
public class MyAdvice {
@Pointcut("execution(* com.service.userServiceImpl.*())")
public void pc(){}
//前置通知
@Before("MyAdvice.pc()")
public void before(){
System.out.println("前置通知");
}
//后置通知 出现异常不会调用
@AfterReturning("MyAdvice.pc()")
public void afterreturning(){
System.out.println("后置通知,出现异常不会调用");
}
//环绕通知
@Around("execution(* com.service.userServiceImpl.*())")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕之前的部分");
Object obj = pjp.proceed();//调用原方法
System.out.println("环绕之后的部分");
return obj;
}
//异常拦截通知
@AfterThrowing("execution(* com.service.userServiceImpl.*())")
public void afterException(){
System.out.println("出现异常");
}
//后置通知(出现异常会调用)
@After("execution(* com.service.userServiceImpl.*())")
public void after(){
System.out.println("后置通知(出现异常会调用)");
}
}
2.配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- aop -->
<!-- 配置目标对象 -->
<bean name="userService" class="com.service.userServiceImpl"></bean>
<!-- 配置通知-->
<bean name="myAdvice" class="com.aopanotation.MyAdvice"></bean>
<!-- 使用注解-->
<context:component-scan base-package="com"></context:component-scan>
<!-- 配置通知织入到目标对象的方法 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
测试代码demo
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/aopanotation/aop.xml")
public class Demo1 {
@Resource(name = "userService")
private userService userService;
@Test
public void test(){
userService.delete();
}
}
@AspectJ 简介
1.AspectJ是一个基于Java语言的AOP框架,Spring2.0以后新增了对AspectJ切点表达式支持
2.@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
新版本Spring框架,建议使用AspectJ方式来开发AOP
使用AspectJ 需要导入Spring AOP和 AspectJ相关jar包
spring-aop-3.2.0.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
spring-aspects-3.2.0.RELEASE.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
AOP切入点表达式:execution(掌握)
AOP切入点表达式支持多种形式的定义规则
•完整写法
返回类型 类的路径 类名 函数名 参数类型(用,分开)
execution(String com.chinasofti.Target.save(String))
•任意返回类型
execution(* com.chin.Target.save(String))
•任意返回类型下指定包下任意类
execution(* com.chin.*.save(String))
•任意返回类型下指定包下任意类任意函数
execution(* com.chin.*.*(String))
•任意返回类型下指定包或子包下任意类任意函数任意参数
execution(* com..*.*(..))
AOP通知类型
before:前置通知(应用:各种校验)
在方法执行前执行,如果通知抛出异常,阻止方法运行
afterReturning:后置通知(应用:常规数据处理)
方法正常返回后执行,如果方法中抛出异常,通知无法执行
必须在方法执行后才执行,所以可以获得方法的返回值。
around:环绕通知(应用:十分强大,可以做任何事情)
方法执行前后分别执行,可以阻止方法的执行
afterThrowing:抛出异常通知(应用:包装异常信息)
方法抛出异常后执行,如果方法没有抛出异常,无法执行
after:最终通知(应用:清理现场)
方法执行完毕后执行,无论方法中是否出现异常
AOP核心组件
切面(Aspect):切面是封装通用业务逻辑的组件,可以作用到其他组件上。
切入点(Pointcut):用于指定哪些组件哪些方法使用切面组件,Spring提供表达式来实现该指定。
通知(Advice):用于指定组件作用到目标组件的具体位置。
AOP前置通知:在目标组件的方法执行前执行的程序。
完成AOP的步骤
切入点程序。
切面程序。
通过配置文件将切面程序插入到切入点程序的某个位置上(通知)
补充:jar包
1.aop联盟规范
2.spring aop 实现
3.aspect 规范
4.spring aspect 实现
05_Spring AOP原理的更多相关文章
- spring ioc aop 原理
spring ioc aop 原理 spring ioc aop 的原理 spring的IoC容器是spring的核心,spring AOP是spring框架的重要组成部分. 在传统的程序设计中,当调 ...
- SSH深度历险(十一) AOP原理及相关概念学习+xml配置实例(对比注解方式的优缺点)
接上一篇 SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP,本篇我们主要是来学习使用配置XML实现AOP 本文采用强制的CGLB代理方式 Security ...
- SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP
AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...
- 面试问烂的 Spring AOP 原理、SpringMVC 过程(求求你别问了)
Spring AOP ,SpringMVC ,这两个应该是国内面试必问题,网上有很多答案,其实背背就可以.但今天笔者带大家一起深入浅出源码,看看他的原理.以期让印象更加深刻,面试的时候游刃有余. Sp ...
- spring MVC 及 AOP 原理
SpringMVC工作原理https://www.cnblogs.com/xiaoxi/p/6164383.htmlspring MVC 原理https://blog.csdn.net/y199108 ...
- spring aop 原理学习
@EnableAspectJAutoProxy: @Import(AspectJAutoProxyRegistrar.class) 实际是创建了一个以org.springframework.aop.c ...
- Spring之AOP原理、代码、使用详解(XML配置方式)
Spring 的两大核心,一是IOC,另一个是AOP,本博客从原理.AOP代码以及AOP使用三个方向来讲AOP.先给出一张AOP相关的结构图,可以放大查看. 一.Spring AOP 接口设计 1.P ...
- 34、[源码]-AOP原理-链式调用通知方法
34.[源码]-AOP原理-链式调用通知方法
- 33、[源码]-AOP原理-获取拦截器链-MethodInterceptor
33.[源码]-AOP原理-获取拦截器链-MethodInterceptor
随机推荐
- 使用<script>标签在HTML网页中插入JavaScript代码
新朋友你在哪里(如何插入JS) 我们来看看如何写入JS代码?你只需一步操作,使用<script>标签在HTML网页中插入JavaScript代码.注意, <script>标签要 ...
- vue-router 的重定向-redirect
1. 我们主要在index.js重新设置路由的重新定向redirect参数 index.js import Vue from 'vue' import Router from 'vue-router' ...
- CI的session操作
在使用session之前,要对配置文件config.php 里面的$config['encryption_key']随便赋个值,例如1234 1. 首先要加载session类,固定写法:$this-& ...
- leetcode-229-求众数②
题目描述: 方法一:摩尔投票法 class Solution: def majorityElement(self, nums: List[int]) -> List[int]: candiate ...
- php 垃圾回收机制 转载
PHP的基本GC概念PHP语言同其他语言一样,具有垃圾回收机制.那么今天我们要为大家讲解的内容就是关于PHP垃圾回收机制的相关问题.希望对大家有所帮助.PHP strtotime应用经验之谈PHP m ...
- 暴力贪心+预处理自动机——cf990E
枚举每种灯管,然后找到代价最小的那种灯管 贪心策略:灯管从0开始向右放置,如果末尾是不能放置灯管的结点,那么要往回找到最近一个可以放置灯管的结点,在那里放置灯管 所以先预处理每个不能放置灯管的结点对应 ...
- go包flag系统包简单使用
一.代码 package main import ( "flag" "fmt" ) //定义命令行参数,这个mode是内存地址,参数1是命令行名称,参数2是命令 ...
- 如何HOOK桌面窗口消息
代码详见:http://download.csdn.net/detail/swanabin/6771465 需求:截获桌面窗口鼠标单击事件,解析所选中的桌面 Item,并将解析后的 item 信息发送 ...
- 1.2python基础_数字类型_数字(Number)类型
一.整型(int型.整数) 整型 等价于C中的有符号长整型(long) 与系统的最大整型一致(如32位机器上的整型是32位,64位机器上的整型是64位), 可以表示的整数范围在[-sys.maxint ...
- Asp.Net中的HttpWebRequest类与HttpWebResponse类
相关博文:https://www.cnblogs.com/xu-yi/p/10061342.html 相关博文:https://www.cnblogs.com/zoujinhua/p/11313396 ...