一.AOP
AOP
和Filter
能够实现的 功能相似。
AOP
和Filter
的区别:
AOP
拦截的是类中方法(切点),只要方法能够被Spring管理,那么这个方法就能够被拦截。Filter
拦截的是请求。
1.AOP:中文名称面向切面编程
2.英文名称:(Aspect Oriented Programming)
3.正常程序执行流程都是纵向执行流程
3.1 又叫面向切面编程,在原有纵向执行流程中添加横切面
3.2 不需要修改原有程序代码
3.2.1 高扩展性
3.2.2 原有功能相当于释放了部分逻辑.让职责更加明确.
4.面向切面编程是什么?
4.1 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面过程就叫做面向切面编程.
5.常用概念
5.1 原有功能: 切点, pointcut
5.2 前置通知: 在切点之前执行的功能. before advice
5.3 后置通知: 在切点之后执行的功能,after advice
5.4 如果切点执行过程中出现异常,会触发异常通知.throws advice
5.5 所有功能总称叫做切面.
5.6 织入: 把切面嵌入到原有功能的过程叫做织入
6.spring 提供了2 种AOP 实现方式
6.1 Schema-based
6.1.1 每个通知都需要实现接口或类
6.1.2 配置spring 配置文件时在<aop:config>
配置
6.2 AspectJ
6.2.1 每个通知不需要实现接口或类
6.2.2 配置spring 配置文件是在<aop:config>
的子标签<aop:aspect>
中配置
二. Schema-based 实现步骤(需要在类中实现接口)
1. 导入jar
2. 新建通知类
2.1 新建前置通知类
2.1.1 arg0:
切点方法对象Method 对象
2.1.2 arg1:
切点方法参数
2.1.3 arg2:
切点在哪个对象中
public class MyBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("执行前置通知");
}
}
2.2 新建后置通知类
2.2.1 arg0
: 切点方法返回值
2.2.2 arg1
:切点方法对象
2.2.3 arg2
:切点方法参数
2.2.4 arg3
:切点方法所在类的对象
public class MyAfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("执行后置通知");
}
}
3. 配置 spring 配置文件
3.1 引入 aop 命名空间
3.2 配置通知类的<bean>
3.3 配置切面
3.4 *
通配符,匹配任意方法名,任意类名,任意一级包名
(下面图中第一个*
表示返回值类型,也就是我们不关心方法返回值类型。图上的解释‘声明通配符’是错误的解释。)
3.5 如果希望匹配任意方法参数(..)
<?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 id="mybefore" class="com.bjsxt.advice.MyBeforeAdvice"></bean>
<bean id="myafter" class="com.bjsxt.advice.MyAfterAdvice"></bean>
<!-- 配置切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(*com.bjsxt.test.Demo.demo2())" id="mypoint" />
<!-- 通知 -->
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint" />
<aop:advisor advice-ref="myafter" pointcut-ref="mypoint" />
</aop:config>
<!-- 配置Demo 类,测试使用 -->
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
</beans>
4. 编写测试代码
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Demo demo = ac.getBean("demo", Demo.class);
demo.demo1();
demo.demo2();
demo.demo3();
}
}
- 运行结果:
三. 配置异常通知的步骤(AspectJ 方式)
1. 只有当切点报异常, 才能触发异常通知
2. 在spring 中有AspectJ 方式提供了异常通知的办法.
2.1 如果希望通过schema-base 实现需要按照特定的要求自己编写方法.
3. 实现步骤:
3.1 新建类,在类写任意名称的方法
public class MyThrowAdvice {
public void myexception(Exception e1) {
System.out.println("执行异常通知" + e1.getMessage());
}
}
3.2 在spring 配置文件中配置
3.2.1 <aop:aspect>
的 ref
属性表示:方法在哪个类中.
3.2.2 <aop: xxxx/>
表示什么通知
3.2.3 method: 当触发这个通知时,调用哪个方法
3.2.4 throwing: 异常对象名,必须和通知中方法参数名相同(可以不在通知中声明异常对象)
<bean id="mythrow" class="com.bjsxt.advice.MyThrowAdvice"></bean>
<aop:config>
<aop:aspect ref="mythrow">
<aop:pointcut expression="execution(*com.bjsxt.test.Demo.demo1())" id="mypoint" />
<aop:after-throwing method="myexception" pointcut-ref="mypoint" throwing="e1" />
</aop:aspect>
</aop:config>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
四. 异常通知(Schema-based 方式)
1. 新建一个类实现throwsAdvice 接口
1.1 必须自己写方法,且必须叫afterThrowing
1.2 有两种参数方式
1.2.1 必须是 1个 或 4个
1.3 异常类型要与切点报的异常类型一致
public class MyThrow implements ThrowsAdvice {
// public void afterThrowing(Method m, Object[] args, Object target, Exception ex) {
// System.out.println("执行异常通知");
// }
public void afterThrowing(Exception ex) throws Throwable {
System.out.println("执行异常通过-schema-base 方式 ");
}
}
2. 在ApplicationContext.xml 配置
<bean id="mythrow"class="com.bjsxt.advice.MyThrow"></bean>
<aop:config>
<aop:pointcut expression="execution(*com.bjsxt.test.Demo.demo1())" id="mypoint"/>
<aop:advisor advice-ref="mythrow" pointcut-ref="mypoint" />
</aop:config>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
五.环绕通知(Schema-based 方式)
1. 把前置通知和后置通知都写到一个通知中,组成了环绕通知
2. 实现步骤
2.1 新建一个类实现MethodInterceptor
public class MyArround implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("环绕-前置");
Object result = arg0.proceed();// 放行,调用切点方式
System.out.println("环绕-后置");
return result;
}
}
2.2 配置 applicationContext.xml
<bean id="myarround" class="com.bjsxt.advice.MyArround"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint" />
<aop:advisor advice-ref="myarround" pointcut-ref="mypoint" />
</aop:config>
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
六.使用AspectJ 方式实现
1. 新建类,不用实现
1.1 类中方法名任意
// 本示例非sxt原示例,我本地稍微修改了下
package com.bjsxt.advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAdvice {
public void aopBefFunc2arg(String name, int age) {
System.out.println("MyAdvice.aopBefFunc2arg()");
System.out.println("前置" + name);
}
public void aopBefFunc1arg(String name) {
System.out.println("MyAdvice.aopBefFunc1arg()");
System.out.println("前置:" + name);
}
public void aopAfteringFunc0arg() {
System.out.println("MyAdvice.aopAfteringFunc0arg()");
System.out.println("后置2");
}
public void aopAfterFunc0arg() {
System.out.println("MyAdvice.aopAfterFunc0arg()");
System.out.println("后置1");
}
public void aopThrowFunc() {
System.out.println("MyAdvice.aopThrowFunc()");
System.out.println("异常");
}
public Object myarround(ProceedingJoinPoint p) throws Throwable {
System.out.println("MyAdvice.myarround()");
System.out.println("before Object result = p.proceed()");
Object result = p.proceed();
System.out.println("after Object result = p.proceed()");
return result;
}
}
1.2 配置 spring 配置文件
1.2.1 <aop:after/>
后置通知,是否出现异常都执行
1.2.2 <aop:after-returing/>
后置通知,只有当切点正确执行时执行
1.2.3 <aop:after/>
和<aop:after-returing/>
和<aop:after-throwing/>
执行顺序和配置顺序有关
1.2.4 execution()
括号不能扩上args
1.2.5 中间使用 and
不能使用 &&
,由 spring 把 and
解析成 &&
1.2.6 args(名称)
名称自定义的.顺序和demo1(参数,参数)
对应
1.2.7 <aop:before/>
, arg-names=” 名称”
名称来源于expression=””
中args()
,名称必须一样
1.2.7.1 args()
有几个参数,arg-names
里面必须有几个参数
1.2.7.2 arg-names=””
里面名称必须和通知方法参数名对应
// 本示例非sxt原示例,稍微修改了下
<?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 id="demo" class="com.bjsxt.test.Demo"></bean>
<bean id="myadvice" class="com.bjsxt.advice.MyAdvice"></bean>
<aop:config>
<!-- 配置切面 -->
<aop:aspect ref="myadvice">
<!-- 配置切点 -->
<aop:pointcut
expression="execution(* com.bjsxt.test.Demo.demoMethod(String,int)) and args(name,age)"
id="pointcut_2_arg" />
<aop:pointcut
expression="execution(* com.bjsxt.test.Demo.demoMethod(String)) and args(name)"
id="pointcut_1_arg" />
<aop:pointcut
expression="execution(* com.bjsxt.test.Demo.demoMethod())"
id="pointcut_0_arg" />
<aop:pointcut
expression="execution(* com.bjsxt.test.Demo.*(..))" id="pointcut_all" />
<aop:before method="aopBefFunc2arg"
pointcut-ref="pointcut_2_arg" arg-names="name,age" />
<aop:before method="aopBefFunc1arg"
pointcut-ref="pointcut_1_arg" arg-names="name" />
<aop:after method="aopAfterFunc0arg"
pointcut-ref="pointcut_0_arg" />
<aop:after method="aopAfterFunc0arg"
pointcut-ref="pointcut_all" />
<!-- <aop:after method="myafter" pointcut-ref="mypoint1" arg-names="name"/> -->
<!-- <aop:after method="myafter" pointcut-ref="mypoint"/> -->
<!-- <aop:after-returning method="myaftering" pointcut-ref="mypoint"/> -->
<!-- <aop:after-throwing method="mythrow" pointcut-ref="mypoint"/> -->
<!-- <aop:around method="myarround" pointcut-ref="mypoint"/> -->
</aop:aspect>
</aop:config>
</beans>
七. 使用注解(基于Aspect)
1. spring 不会自动去寻找注解,必须告诉spring 哪些包下的类中可能有注解
1.1 引入xmlns:context
<context:component-scanbase-package="com.bjsxt.advice"></context:component-scan>
2. @Component
2.1 相当于<bean/>
2.2 如果没有参数,把类名首字母变小写,相当于<bean id=””/>
2.3 @Component(“自定义名称”)
2. 实现步骤:
3.1 在 spring 配置文件中设置注解在哪些包中
<context:component-scan base-package="com.bjsxt.advice,com.bjsxt.test"></context:component-scan>
3.2 在 Demo 类中添加@Componet
3.2.1 在方法上添加@Pointcut(“”)
定义切点
@Component
public class Demo {
@Pointcut("execution(* com.bjsxt.test.Demo.demo1())")
public void demo1() throws Exception{
// int i = 5/0;
System.out.println("demo1");
}
}
3.3 在通知类中配置
3.3.1 @Component
类被spring 管理
3.3.2 @Aspect
相当于<aop:aspect/>
表示通知方法在当前类中
@Component
@Aspect
public class MyAdvice {
@Before("com.bjsxt.test.Demo.demo1()")
public void mybefore() {
System.out.println("前置");
}
@After("com.bjsxt.test.Demo.demo1()")
public void myafter() {
System.out.println("后置通知");
}
@AfterThrowing("com.bjsxt.test.Demo.demo1()")
public void mythrow() {
System.out.println("异常通知");
}
@Around("com.bjsxt.test.Demo.demo1()")
public Object myarround(ProceedingJoinPoint p) throws Throwable {
System.out.println("环绕-前置");
Object result = p.proceed();
System.out.println("环绕-后置");
return result;
}
}
八.代理设计模式
1. 设计模式:前人总结的一套解决特定问题的代码.
2. 代理设计模式优点:
2.1 保护真实对象
2.2 让真实对象职责更明确.
2.3 扩展
3. 代理设计模式
3.1 真实对象.(老总)
3.2 代理对象(秘书)
3.3 抽象对象(抽象功能),谈小目标
九. 静态代理设计模式
1. 由代理对象代理所有真实对象的功能.
1.1 自己编写代理类
1.2 每个代理的功能需要单独编写
2. 静态代理设计模式的缺点:
2.1 当代理功能比较多时,代理类中方法需要写很多.
十. 动态代理
1. 为了解决静态代理频繁编写代理功能缺点.
2. 分类:
2.1 JDK 提供的
2.2 cglib 动态代理
十一. JDK 动态代理
1. 和cglib 动态代理对比
1.1 优点:jdk 自带,不需要额外导入jar
1.2 缺点:
1.2.1 真实对象必须实现接口
1.2.2 利用反射机制.效率不高.
2. 使用JDK 动态代理时可能出现下面异常
2.1 出现原因:希望把接口对象转换为具体真实对象
十二: cglib 动态代理
1. cglib 优点:
1.1 基于字节码,生成真实对象的子类.
1.1.1 运行效率高于JDK 动态代理.
1.2 不需要实现接口
2. cglib 缺点:
2.1 非JDK 功能,需要额外导入jar
3. 使用 spring aop 时,只要出现 Proxy 和真实对象转换异常
3.1 设置为true 使用cglib
3.2 设置为false 使用jdk(默认值)
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>