AOP的半自动代理 及 缺点
AOP的通知类型:
aop联盟 定义 aop通知类型,spring 对 aop联盟规范支持。
aop联盟定义5种通知
前置通知 org.springframework.aop.MethodBeforeAdvice
在目标方法执行前实施增强
后置通知 org.springframework.aop.AfterReturningAdvice
在目标方法执行后实施增强
环绕通知 org.aopalliance.intercept.MethodInterceptor
在目标方法执行前后实施增强
异常抛出通知 org.springframework.aop.ThrowsAdvice
在方法抛出异常后实施增强
引介通知 org.springframework.aop.IntroductionInterceptor(了解)对一个类进行增强(添加方法或属性等)
在目标类中添加一些新的方法和属性
如果使用aop联盟规范进行aop开发,所有的通知必须实现接口。(底层为了确定通知方法名称)
AOP的半自动代理(不带有切入点的切面---所有方法)
1.切面类
需要总结:
需要根据AOP联盟确定一个通知类型:这里使用环绕通知(环绕通知需要手动开启方法)
所以切面类随便写,随便实现一个通知类型,重写里面的invok方法在,通过参数调用
proceed()执行目标类,前后可以写代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.mickeymouse.proxy;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* 切面类:包含两个通知(增强)
环绕通知必须手动执行方法
* @author Mickey-Mouse
*
*/
public class MyAspect implements MethodInterceptor {
public Object invoke(MethodInvocation mi) throws Throwable {
System.out.println( "已校验是否符合身份" );
//环绕通知必须手动执行方法
Object object = mi.proceed();
System.out.println( "方法已经执行完--->后处理" );
return object;
}
}
|
2.目标类(已实现接口)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.mickeymouse.proxy;
public class ProductsServiceImpl implements ProductsService {
/**
* 目标类:
* 里面有两个连接点
*/
public void addProducts(){
System.out.println( "添加商品成功" );
}
public void updateProducts(){
System.out.println( "修改商品成功" );
}
}
|
3.半自动代理XML配置
与AOP的自动代理的区别在于:
半自动代理没有使用AOP约束,要手动配置代理类与目标类的关系
半自动代理是基于ProxyFactoryBean的代理类:
缺点:
* 配置麻烦:
* 需要为每一个要增强的类配置一个ProxyFactoryBean.(只要有增强,就需要写一个ProxyFactoryBean的配置)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<? 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"
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.xsd">
<!-- bean definitions here -->
<!-- 半自动代理模式 -->
<!-- 生成切面类 -->
< bean id = "myAspect" class = "com.mickeymouse.proxy.MyAspect" />
<!-- 生成目标类 -->
< bean id = "productsService" class = "com.mickeymouse.proxy.ProductsServiceImpl" />
<!-- 让Spring来创建代理工厂对象 -->
< bean id = "proxyFactoryBean" class = "org.springframework.aop.framework.ProxyFactoryBean" >
<!-- 设置目标类 -->
< property name = "target" ref = "productsService" ></ property >
<!-- 设置切面类"名字" -->
< property name = "interceptorNames" value = "myAspect" ></ property >
<!-- 设置切面类需要连接的接口(optimize与接口二选一即可,Spring可以自动识别,接口就动态代理,没有就CGLIB) -->
< property name = "proxyInterfaces" value = "com.mickeymouse.proxy.ProductsService" ></ property >
<!-- 是否强制使用使用CGLIB -->
< property name = "optimize" value = "true" ></ property >
</ bean >
</ beans >
|
测试类:
1
2
3
4
5
6
7
|
@Test
public void test1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml" );
ProductsService bean = applicationContext.getBean( "proxyFactoryBean" ,ProductsService. class );
bean.addProducts();
bean.updateProducts();
}
|
结果:
AOP的半自动代理:(配置带有切入点的切面---->个别方法)(了解)
配置文件:(其他部分是一样的 , 同上)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Spring帶有切入点的切面=============== -->
<!-- 配置目标类 -->
< bean id = "customerDao" class = "com.itheima.spring.demo4.CustomerDao" />
<!-- 配置通知:(环绕通知) -->
< bean id = "myAroundAdvice" class = "com.itheima.spring.demo4.MyAroundAdvice" />
<!-- 配置带有切入点的切面 -->
< bean id = "myAdvisor" class = "org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
<!-- 表达式: 正则表达式 : .:任意字符 *:任意次数-->
<!-- 拦截所有方法 -->
<!-- <property name="pattern" value=".*"/> -->
<!-- 只拦截update方法 -->
<!-- <property name="pattern" value="com\.itheima\.spring\.demo4\.CustomerDao\.update"/> -->
<!-- 拦截update和delete方法 -->
<!-- <property name="patterns" value="com\.itheima\.spring\.demo4\.CustomerDao\.update,com\.itheima\.spring\.demo4\.Cus tomerDao\.delete"/> -->
<!-- 拦截所有以save和update结束名的方法 -->
< property name = "patterns" value = ".*save.*,.*update.*" />
<!-- 配置增强 -->
< property name = "advice" ref = "myAroundAdvice" />
</ bean >
<!-- 配置生成代理 -->
< bean id = "proxyFactoryBean" class = "org.springframework.aop.framework.ProxyFactoryBean" >
<!-- 配置目标 -->
< property name = "target" ref = "customerDao" />
<!-- 配置代理目标类 -->
< property name = "proxyTargetClass" value = "true" />
<!-- 配置拦截的名称 -->
< property name = "interceptorNames" value = "myAdvisor" />
</ bean >
</ beans >
|
转载于:https://my.oschina.net/mickeymouse/blog/519118
AOP的半自动代理 及 缺点相关推荐
- AOP的自动代理(基于AspectJ框架)
AOP的通知类型: aop联盟 定义 aop通知类型,spring 对 aop联盟规范支持. AOP 联盟不是Spring的,先是AOP联盟定义了一个接口org.springframework.aop ...
- Spring AOP自动创建代理 和 ProxyFactoryBean创建代理
Advice 通知类型 AOP联盟为通知Advice定义了org.aopalliance.aop.Interface.Advice,Spring按照通知Advice在目标方法的连接点位置,可以分为5种 ...
- 【Spring AOP】静态代理设计模式、Spring 动态代理开发详解、切入点详解(切入点表达式、切入点函数)
AOP 编程 静态代理设计模式 1. 为什么需要代理设计模式 2. 代理设计模式 名词解释 代理开发的核心要素 静态代理编码 静态代理存在的问题 Spring 动态代理开发 搭建开发环境 Spring ...
- Spring源码-AOP(六)-自动代理与DefaultAdvisorAutoProxyCreator
2019独角兽企业重金招聘Python工程师标准>>> Spring AOP 源码解析系列,建议大家按顺序阅读,欢迎讨论 Spring源码-AOP(一)-代理模式 Spring源码- ...
- Spring 初识Aop JDK动态代理实现 原理初显
Spring 初识Aop JDK动态代理实现 原理初显 一.项目结构 二.具体步骤: 1.创建maven项目 创建好包结构 2.写一个TestDao接口 及实现类 3. 写一个自己的切面类 4.jav ...
- spring—AOP 的动态代理技术
AOP 的动态代理技术 常用的动态代理技术 JDK 代理 : 基于接口的动态代理技术 cglib 代理:基于父类的动态代理技术 JDK 代理 public class proxy {@Testpubl ...
- Spring : Spring Aop CGLIB动态代理调用过程
1.美图 2.概述 CGLIB动态代理参考: CGLIB动态代理 CGLIB原理解析参考:CGLIB原理解析 3.分析 Spring AOP CGLIB动态代理调用过程分析,CGLIB动态代理调用过程 ...
- AOP jdk动态代理
一: jdk动态代理是Spring AOP默认的代理方法.要求 被代理类要实现接口,只有接口里的方法才能被代理,主要步骤是先创建接口,接口里创建要被代理的方法,然后定义一个实现类实现该接口,接着将被代 ...
- AOP、动态代理、CGlib(原理实践)
Java 代理模式实现方式,主要有如下五种方法: 1. 静态代理,工程师编辑代理类代码,实现代理模式:在编译期就生成了代理类. 2. 基于 JDK 实现动态代理,通过jdk提供的工具方法Proxy.n ...
最新文章
- 谈谈 Docker 网络
- Serverless——前端的3.0时代
- JDK 9、10和11中的安全性增强
- 老人机彻底不能用了?联通逐渐关闭2G、3G网络?回应:手机制式不支持
- 从source folder 下将其所有子文件夹的*.* 文件拷贝到 target folder (不拷贝文件夹名仅拷贝文件)...
- 160508Junit使用
- 在取证过程中,常见的一些注册表键值整理
- Thinkpad T470 内置电池问题
- 我的 2020 总结:Never Settle
- vue实现websoket即时通讯
- 网络打印协议之LPR或RAW
- iOS:开放平台引用(二)--微信分享
- MQTT连接阿里云IOT
- LeetCode/LintCode 题解丨一周爆刷双指针:数组中的最长山脉
- 中国房价为什么会居高不下?
- sql 累计占比_SQL语句-求总和的百分比
- 基于FPGA的CAN通信,FPGA驱动SJA1000T芯片代码,实现标准帧与扩展帧的通信驱动
- CTFHub | bak文件
- 小麦麦穗品种识别数据集
- 利用Gitlab进行代码的协作开发
热门文章
- 台电固态硬盘用什么测试软件,台电SSD测评结果出炉 高性价比卖点是否名副其实?...
- ERROR in Template execution failed: ReferenceError: process is not defined(使用electron-vue出现的错误)
- python之查看图像位深度,以及改变图像位深度
- vue2过滤器《书山有路勤为径,学海无涯苦作舟》
- 知识管理——知识经济时代的增资利器
- 量化投资学习——因子IC、IR的介绍(转载)
- 开源免费OA:O2OA中的日程安排功能是什么样的?
- mts格式转换成mp4怎么转?
- 熟悉GitHub、VS工具的使用(《构建之法》第二次作业)
- 可视猫眼门铃雷达方案,人体存在感应雷达模组,智能亮屏响铃