Spring框架学习07——基于传统代理类的AOP实现
在Spring中默认使用JDK动态代理实现AOP编程,使用org.springframework.aop.framework.ProxyFactoryBean创建代理是Spring AOP 实现的最基本方式。
1、通知类型
根据Spring中通知在目标类方法中的连接点位置,通知可以分为6种类型:
(1)环绕通知
环绕通知(org.aopalliance.intercept.MethodInterceptor)是在目标方法执行前和执行后实施增强,可应用于日志记录、事务处理等功能。
(2)前置通知
前置通知(org.springframework.aop.MethodBeforeAdvice)是在目标方法执行前实施增强,可应用于权限管理等功能。
(3)后置返回通知
后置返回通知(org.springframework.aop.AfterReturningAdvice)是在目标方法成功执行后实施增强,可应用于关闭流、删除临时文件等功能。
(4)后置(最终)通知
后置通知(org.springframework.aop.AfterAdvice)是在目标方法执行后实施增强,与后置返回通知不同的是,不管是否发生异常都要执行该类通知,该类通知可应用于释放资源。
(5)异常通知
异常通知(org.springframework.aop.ThrowsAdvice)是在方法抛出异常后实施增强,可应用于处理异常、记录日志等功能。
(6)引入通知
引入通知(org.springframework.aop.IntroductionInterceptor)是在目标类中添加一些新的方法和属性,可应用于修改目标类(增强类)。Spring AOP 只支持方法层面的增强,所以该类型的通知只作为了解即可。
2、切面类型
- Advisor:代表一般切面,Advice本身就是一个切面,对目标类所有方法进行拦截
- PointcutAdvisor:代表具有切点的切面,可以指定拦截目标类哪些方法
- IntroductionAdvisor:代表引介切面,针对引介通知而使用切面
3、ProxyFactoryBean
ProxyFactoryBean是org.springframework.beans.factory.FactoryBean 接口的实现类,FactoryBean负责实例化一个Bean实例,ProxyFactoryBean负责为其他Bean实例创建代理实例。ProxyFactoryBean类的常用属性如下:
- target:代理的目标对象
- proxyInterfaces:代理需要实现的接口列表,如果是多个接口,可以使用以下格式赋值:<list> <value></value> </list>
- InterceptorNames:需要织入目标的Advice
- proxyTargetClass:是否对类代理而不是接口,默认为false,使用JDK动态代理;当为true时,使用CGLIB动态代理
- singleton:返回的代理实例是否为单例,默认为true
- optimize:当设置为true时强制使用CGLIB动态代理
4、Advisor一般切面的实现
在pom.xml中导入AOP联盟和Spring-aop的相关依赖
<!-- AOP联盟依赖 -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
创建StuDao接口和StuDaoImpl实现类,示例代码如下:
StuDao接口
public interface StuDao {
public void save();
public void modify();
public void delete();
}
StuDaoImpl实现类
public class StuDaoImpl implements StuDao {
@Override
public void save() {
System.out.println("保存");
} @Override
public void modify() {
System.out.println("修改");
} @Override
public void delete() {
System.out.println("删除");
}
}
创建切面类MyBeforeAdvice,仅实现前置通知
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method; public class MyBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("前置增强========");
}
}
在applicationContext.xml中配置切面并指定代理
<!--配置目标类-->
<bean id="stuDao" class="aop.StuDaoImpl"></bean>
<!--配置前置通知类型-->
<bean id="myBeforeAdvice" class="aop.MyBeforeAdvice"></bean>
<!--Spring AOP 生成代理对象-->
<bean id="stuDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置目标类-->
<property name="target" ref="stuDao"></property>
<!--代理实现的接口-->
<property name="proxyInterfaces" value="aop.StuDao"></property>
<!--采取拦截的名称-->
<property name="interceptorNames" value="myBeforeAdvice"></property>
</bean>
创建测试类
@Test
public void demo(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
StuDao stuDao = (StuDao) app.getBean("stuDaoProxy");
stuDao.save();
stuDao.modify();
stuDao.delete();
}
运行结果
5、PointcutAdvisor切点切面的实现
使用普通Advice作为切面,将对目标类所有方法进行拦截,不够灵活,在实际开发中常采用带切点的切面。
常用PointcutAdvisor实现类:
- DefaultPointcutAdvisor最常用的切面类型,它可以通过任意Pointcut和Advice组合定义切面;
- JdkRegexpMethodPointcut构造正则表达式切点(推荐)
创建StuDao接口和StuDaoImpl实现类,示例代码如下:
StuDao接口
public interface StuDao {
public void save();
public void modify();
public void delete();
}
StuDaoImpl实现类
public class StuDaoImpl implements StuDao {
@Override
public void save() {
System.out.println("保存");
} @Override
public void modify() {
System.out.println("修改");
} @Override
public void delete() {
System.out.println("删除");
}
}
下面创建一个实现环绕通知的切面类
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class MyAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation m) throws Throwable {
//前置增强方法
check();
//执行目标方法
Object obj = m.proceed();
//后置增强方法
log();
return obj;
} public void check(){
System.out.println("模拟权限控制");
}
public void log(){
System.out.println("模拟日志记录");
}
}
在applicationContext.xml中配置切面并指定代理
<!--配置目标类-->
<bean id="stuDao" class="aop.StuDaoImpl"></bean>
<!--配置通知-->
<bean id="myAspect" class="aop.MyAspect"></bean>
<!--配置带切入点的切面-->
<bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!--pattern中配置正则表达式:.表示任意字符 *表示任意次数-->
<property name="pattern" value=".*"></property>
<property name="advice" ref="myAspect"></property>
</bean>
<!--Spring AOP 生成代理对象-->
<bean id="stuDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置目标类-->
<property name="target" ref="stuDao"></property>
<!--代理实现的接口-->
<property name="proxyInterfaces" value="aop.StuDao"></property>
<!--采取拦截的名称-->
<property name="interceptorNames" value="myAdvisor"></property>
</bean>
创建测试类
@Test
public void demo(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
StuDao stuDao = (StuDao) app.getBean("stuDaoProxy");
stuDao.save();
stuDao.modify();
stuDao.delete();
}
运行结果
Spring框架学习07——基于传统代理类的AOP实现的更多相关文章
- Spring框架学习笔记(8)——AspectJ实现AOP
使用代理对象实现AOP虽然可以满足需求,但是较为复杂,而Spring提供一种简单的实现AOP的方法AspectJ 同样的计算器的DEMO 首先配置applicationContext.xml < ...
- Spring框架学习09——基于AspectJ的AOP开发
1.基于注解开发AspectJ (1)AspectJ注解 基于注解开发AspectJ要比基于XML配置开发AspectJ便捷许多,所以在实际开发中推荐使用注解方式.关于注解的相关内容如下: @Aspe ...
- Spring框架学习08——自动代理方式实现AOP
在传统的基于代理类的AOP实现中,每个代理都是通过ProxyFactoryBean织入切面代理,在实际开发中,非常多的Bean每个都配置ProxyFactoryBean开发维护量巨大.解决方案:自动创 ...
- Spring AOP(基于代理类的AOP实现)
#基于代理类的AOP实现:step1: 1 package com.sjl.factorybean; /**切面类*/ import org.aopalliance.intercept.MethodI ...
- Spring框架学习总结(上)
目录 1.Spring的概述 2.Spring的入门(IOC) 3.Spring的工厂类 4.Spring的配置 5.Spring的属性注入 6.Spring的分模块开发的配置 @ 1.Spring的 ...
- Spring框架学习一
Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...
- Spring框架学习1
AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理 Spring框架学习(一) 阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...
- Spring框架学习之IOC(二)
Spring框架学习之IOC(二) 接着上一篇的内容,下面开始IOC基于注解装配相关的内容 在 classpath 中扫描组件 <context:component-scan> 特定组件包 ...
- Spring框架学习笔记(1)
Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...
随机推荐
- A - A Secret (扩展kmp)
题目链接:https://cn.vjudge.net/contest/283743#problem/A 题目大意:给你字符串s1和s2,然后问你s2的每一个后缀在s1中出现的次数之和(可重叠). 具体 ...
- python - class类 (二) 静态属性/类方法/静态方法
静态属性: #静态属性 = 数据属性 (@property) class mianji(): def __init__(self,x,y): self.x = x self.y = y #类的函数方法 ...
- geeksforgeeks-Array-Rotation and deletion
As usual Babul is again back with his problem and now with numbers. He thought of an array of numb ...
- Java 集合和映射表
集合 可以使用集合的三个具体类HashSet.LinkedHashSet.TreeSet来创建集合 HashSet类 负载系数 当元素个数超过了容量与负载系数的乘积,容量就会自动翻倍 HashSet类 ...
- C++读写TXT文件中的string或者int型数据以及string流的用法
对文件的读写操作是我们在做项目时经常用到的,在网上看了很多博客,结合自身的项目经验总结了一下,因此写了这篇博客,有些地方可能直接从别的博客中复制过来,但是都会注明出处. 一.文件的输入输出 fstre ...
- python内置模块之unittest测试(五)
系列文章 python模块分析之random(一) python模块分析之hashlib加密(二) python模块分析之typing(三) python模块分析之logging日志(四) pytho ...
- C# 发送email邮件!
利用C#邮件发送邮箱使用到两个类SmtpClient和MailMessage.可以把SmtpClient看做发送邮件信息的客户端,而把MailMessage看做需要发送的消息. 下面是我写的发送邮件的 ...
- 026_关于shell中的特殊变量$0 $n $* $@ $! $?
一. $n:获取当前执行的shell脚本的第N个参数,n=1..9,当n为0时表示脚本的文件名,如果n大于9,用大括号括起来like${10}. $*:获取当前shell的所有参数,将所有的命令行参数 ...
- Java通过BCrypt加密
一.概述 在用户模块,对于用户密码的保护,通常都会进行加密.我们通常对密码进行加密,然后存放在数据库中,在用户进行登录的时候,将其输入的密码进行加密然后与数据库中存放的密文进行比较,以验证用户密码是否 ...
- 5个php实例,细致说明传值与传引用的区别
传值:是把实参的值赋值给行参 ,那么对行参的修改,不会影响实参的值 传引用 :真正的以地址的方式传递参数传递以后,行参和实参都是同一个对象,只是他们名字不同而已对行参的修改将影响实参的值 说明: 传值 ...