一起学习Spring4.0(四)Spring AOP

本节讲述Spring AOP(面向切面编程)。

代理设计模式的原理

使用一个代理将对象包装起来,然后用该代理对象取代原始对象。任何对原始对象的调用都要通过代理。代理对象决定是否以及何时将方法调用转到原始对象上。

新建项目 spring-02。在项目下新建包com.leezp.spring.aop.helloworld。
在该包下新建ArithmeticCalculator.java接口。

1
2
3
4
5
6
7
8
9
10
11
package com.leezp.spring.aop.helloworld;
public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}

新建接口的实现类ArithmeticCalculatorImpl.java。

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
package com.leezp.spring.aop.helloworld;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int res = i + j;
return res;
}
@Override
public int sub(int i, int j) {
int res = i - j;
return res;
}
@Override
public int mul(int i, int j) {
int res = i * j;
return res;
}
@Override
public int div(int i, int j) {
int res = i / j;
return res;
}
}

新建动态代理类ArithmeticCalculatorLoggingProxy.java。

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.leezp.spring.aop.helloworld;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
/**
* 动态代理
*
* @author Lee
*
*/
public class ArithmeticCalculatorLoggingProxy {
// 要代理的对象
private ArithmeticCalculator target;
public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {
this.target = target;
}
public ArithmeticCalculator getLoggingproxy() {
ArithmeticCalculator proxy = null;
// 代理对象由哪一个类加载器负责加载
ClassLoader loader = target.getClass().getClassLoader();
// 代理对象的类型,即其中有哪些方法
Class[] interfaces = new Class[] { ArithmeticCalculator.class };
// 当调用代理对象其中的方法时,该执行的代码
InvocationHandler h = new InvocationHandler() {
/**
* proxy:正在返回的那个代理对象。一般情况下,在invoke方法中都不使用该对象或该对象的方法,
* 防止出现再次调用invoke函数的死循环 ;method:正在被调用的方法 ;args:调用方法时,传入的参数
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName();
// 日志
System.out.println("The method " + methodName + " begins with "
+ Arrays.asList(args));
// 执行方法
Object res = method.invoke(target, args);
// 日志
System.out.println("The method " + methodName + " ends with "
+ res);
return res;
}
};
proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader,
interfaces, h);
return proxy;
}
}
```

新建Main.java。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
``` bash
package com.leezp.spring.aop.helloworld;
public class Main {
public static void main(String[] args) {
/*
* ArithmeticCalculator ari = null; ari = new
* ArithmeticCalculatorImpl();
*/
ArithmeticCalculator target = new ArithmeticCalculatorImpl();
ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(
target).getLoggingproxy();
System.out.println(proxy.getClass().getName());// 打印代理名称
int res = proxy.add(1, 2);
System.out.println(res);
res = proxy.div(4, 2);
System.out.println(res);
}
}

在控制台观察运行结果。

1
2
3
4
5
6
7
$Proxy0
The method add begins with [1, 2]
The method add ends with 3
3
The method div begins with [4, 2]
The method div ends with 2
2

获取源代码

AOP 简介

AOP(Aspect-Oriented Programming,面向切面编程):是一种新的方法论,是对传统OOP(Object-Oriented Programming,面向对象编程)的补充。

AOP的主要编程对象是切面(aspect),而切面模块化横切关注点

在应用AOP编程时,仍然需要定义公共功能,但可以明确的定义这个功能在哪里,以什么方式应用,并且不必修改受影响的类。这样一来横切关注点就被模块化到特殊的对象(切面)里。

AOP的好处:
1.每个事物逻辑位于一个位置,代码不分散,便于维护和升级。
2.业务模块更简洁,只包含核心业务代码。

AOP 术语

切面(Aspect):横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象

通知(Active):切面必须要完成的工作

目标(Target):被通知的对象

代理(Proxy):向目标对象应用通知之后创建的对象

连接点(Joinpoint):程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。连接点由两个信息确定:方法表示的程序执行点。相对点表示方位。例如ArithmeticCalculator#add()方法执行前的连接点执行点为ArithmeticCalculator#add();方位为该方法执行前的位置。

切点(pointcut):每个类都拥有多个连接点:例如ArithmeticCalculator的所有方法实际上都是连接点,即连接点是程序类中客观存在的事务AOP通过切点定位到特定的连接点。类比:连接点相当于数据库中的记录,切点相当于查询条件。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。

AspectJ

Java社区里最完整最流行的AOP框架。

在Spring2.0以上版本中,可以使用基于AspectJ注解或基于XML配置的AOP。

在Spring中启用AspectJ注解支持

要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库aopalliance.jaraspectj.weaver.jar和spring-aspects.jar。

将aop Schema添加到根元素中

要在Spring IOC容器中启动AspectJ注解支持,只要在Bean配置文件中定义一个空的XML元素

当Spring IOC容器侦测到Bean配置文件中的元素时,会自动为与AspectJ切面匹配的Bean创建代理。

将下图的jar包添加到lib文件夹下并build path。

要在Spring中声明AspectJ切面,只需要在IOC容器中将切面声明为Bean实例。当在Spring IOC容器中初始化AspectJ切面之后,Spring IOC 容器就会为那些与AspectJ切面相匹配的Bean创建代理。

在AspectJ注解中,切面只是一个带有@Aspect注解的Java类

新建包com.leezp.spring.aop.impl。
在该包下新建接口ArithmeticCalculator.java。

1
2
3
4
5
6
7
8
9
10
11
package com.leezp.spring.aop.impl;
public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}

新建接口的实现类ArithmeticCalculatorImpl.java。

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
package com.leezp.spring.aop.impl;
import org.springframework.stereotype.Component;
@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int res = i + j;
return res;
}
@Override
public int sub(int i, int j) {
int res = i - j;
return res;
}
@Override
public int mul(int i, int j) {
int res = i * j;
return res;
}
@Override
public int div(int i, int j) {
int res = i / j;
return res;
}
}

新建applicationContext.xml并添加beans、aop、context节点。

1
2
3
4
5
6
7
8
9
10
11
12
<?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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.leezp.spring.aop.impl"></context:component-scan>
</beans>

新建Main.java。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.leezp.spring.aop.impl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
ArithmeticCalculator arithmeticCalculator = ctx
.getBean(ArithmeticCalculator.class);
int res = arithmeticCalculator.add(3, 4);
System.out.println(res);
}
}

在控制台观察运行结果。

1
7

获取源代码

新建一个日志处理的切面LoggingAspect.java。

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
40
package com.leezp.spring.aop.impl;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
//把这个类声明为一个切面:需要1.把该类放入到IOC容器中@Component;2.声明为一个切面@Aspect
@Aspect
@Component
public class LoggingAspect {
// 声明该方法是一个前置通知:在目标方法开始之前执行
// 执行(public 返回值 包名 接口名 方法(参数))
// **还需要在配置文件中加配置<aop:aspectj-autoproxy></aop:aspectj-autoproxy>使@Before这个注解起作用**
// @Before作用:当调用一个目标方法和声明的注解相匹配的时候,
// aop框架自动为那个方法所在的类生成一个代理对象,
// 在调用目标方法之前,将这句话加进去。
// .*代表接口里的所有方法
@Before("execution(public int com.leezp.spring.aop.impl.ArithmeticCalculator.*(int,int))")
public void beforeMethod(JoinPoint joinPoint) {
// 连接点 JoinPoint
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method " + methodName + " begins with" + args);
}
// 后置通知:在目标方法执行后(无论是否发生异常)执行的通知
// 注意:在后置通知中还不能访问目标方法的执行结果,要在**返回通知**里访问。
@After("execution(public int com.leezp.spring.aop.impl.ArithmeticCalculator.*(int,int))")
public void afterMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends");
}
}

修改 applicationContext.xml。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.leezp.spring.aop.impl"></context:component-scan>
<!-- 在配置文件中加配置使@Before起作用 -->
<!-- 使AspectJ注解起作用:自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

修改Main.java。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.leezp.spring.aop.impl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
ArithmeticCalculator arithmeticCalculator = ctx
.getBean(ArithmeticCalculator.class);
int res = arithmeticCalculator.add(3, 4);
System.out.println(res);
res = arithmeticCalculator.div(12, 0);
System.out.println(res);
}
}
```

在控制台观察运行结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
``` bash
Exception in thread "main" java.lang.ArithmeticException: / by zero
The method add begins with[3, 4]
The method add ends
7
The method div begins with[12, 0] at com.leezp.spring.aop.impl.ArithmeticCalculatorImpl.div(ArithmeticCalculatorImpl.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at $Proxy8.div(Unknown Source)
at com.leezp.spring.aop.impl.Main.main(Main.java:17)
The method div ends

获取源代码

Spring AOP配置总结

1.加入jar包。
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.2.jar
spring-aop-4.0.4.RELEASE.jar
spring-aspects-4.0.4.RELEASE.jar
spring-beans-4.0.4.RELEASE.jar
spring-context-4.0.4.RELEASE.jar
spring-core-4.0.4.RELEASE.jar
spring-expression-4.0.4.RELEASE.jar
2.在配置文件中加入aop的命名空间。
3.基于注解的方式
①.在配置文件中加入如下配置:
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
②.把横切关注点的代码抽象到切面的类中。
i.切面首先是一个IOC中的bean,即加入@Component注解
ii.切面还需要加入@Aspect注解
③.在类中声明各种通知:
通知是标注有某种注解的简单的Java方法
一个切面可以包括一个或多个通知
AspectJ支持5种类型的通知注解:
——@Before:前置通知,在方法执行之前执行
——@After:后置通知,在方法执行之后执行(连接点返回结果或者抛异常的时候,都执行)
——@AfterRunning:返回通知,在方法返回结果之后执行
——@AfterThrowing:异常通知,在方法抛出异常之后执行
——@Around:环绕通知,围绕着方法执行

i.声明一个方法
ii.在方法前加一个注解 例如 @Before(“方法签名“),

利用方法签名编写AspectJ切入点表达式:
最典型的切入点表达式是根据方法的签名来匹配各种方法:
匹配ArithmeticCalculator中声明的所有方法:第一个代表任意修饰符即任意返回值,第二个代表任意方法,“..”代表匹配任意数量的参数。若目标类与接口与该平面在同一个包中,可以省略包名。

匹配ArithmeticCalculator接口的所有公共方法。

匹配ArithmeticCalculator中返回double类型数值的方法。

匹配第一个参数为double类型的方法,”..”匹配任意数量任意类型的参数。

匹配参数类型为double,double类型的方法。

④.可以在通知方法中声明一个类型 JoinPoint的参数。然后就能访问链接细节,如方法名称参数值

AspectJ支持的5种类型的通知注解、设置切面优先级、相同名称切入点的引用的Demo

基于配置文件方式配置AOP


com.leezp.spring.aop包是用注解方式配置AOP。
com.leezp.spring.aop.xml包时用配置文件的方式来配置AOP。

获取源代码

版权声明:本文为博主原创文章,转载请注明出处 Leezp’s Blog

点击按钮打赏作者!