spring
This commit is contained in:
@ -1,34 +1,35 @@
|
||||
# spring AOP(xml配置方式)
|
||||
|
||||
## 目录<br/>
|
||||
# spring AOP( XML 配置方式)
|
||||
<nav>
|
||||
<a href="#"></a><br/>
|
||||
<a href="#一说明">一、说明</a><br/>
|
||||
<a href="#11-项目结构说明">1.1 项目结构说明</a><br/>
|
||||
<a href="#12-依赖说明">1.2 依赖说明</a><br/>
|
||||
<a href="#二spring-aop">二、spring aop</a><br/>
|
||||
<a href="#21-创建待切入接口及其实现类">2.1 创建待切入接口及其实现类</a><br/>
|
||||
<a href="#22-创建自定义切面类">2.2 创建自定义切面类</a><br/>
|
||||
<a href="#11-项目结构说明">1.1 项目结构说明</a><br/>
|
||||
<a href="#12-依赖说明">1.2 依赖说明</a><br/>
|
||||
<a href="#二Spring-AOP">二、Spring AOP</a><br/>
|
||||
<a href="#21-准备工作">2.1 准备工作</a><br/>
|
||||
<a href="#22-自定义切面">2.2 自定义切面</a><br/>
|
||||
<a href="#23-配置切面">2.3 配置切面</a><br/>
|
||||
<a href="#24-测试切面">2.4 测试切面</a><br/>
|
||||
<a href="#附-关于切面表达式的说明">附: 关于切面表达式的说明</a><br/>
|
||||
## 正文<br/>
|
||||
<a href="#三切面表达式">三、切面表达式</a><br/>
|
||||
</nav>
|
||||
|
||||
##
|
||||
|
||||
|
||||
## 一、说明
|
||||
|
||||
### 1.1 项目结构说明
|
||||
|
||||
切面配置位于 resources 下的 aop.xml 文件,其中 CustomAdvice 是自定义切面类,OrderService 是待切入的方法。
|
||||
切面配置位于 resources 下的 `aop.xml` ,其中 CustomAdvice 是自定义切面类,OrderService 是待切入的方法。
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-aop.png"/> </div>
|
||||
|
||||
|
||||
|
||||
### 1.2 依赖说明
|
||||
|
||||
除了 spring 的基本依赖外,需要导入 aop 依赖包
|
||||
除了 Spring 的基本依赖外,还需需要导入 AOP 的依赖:
|
||||
|
||||
```xml
|
||||
<!--aop 相关依赖-->
|
||||
<!--aop 相关依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aop</artifactId>
|
||||
@ -38,9 +39,11 @@
|
||||
|
||||
|
||||
|
||||
## 二、spring aop
|
||||
## 二、Spring AOP
|
||||
|
||||
#### 2.1 创建待切入接口及其实现类
|
||||
### 2.1 准备工作
|
||||
|
||||
创建待切入的接口及其实现类:
|
||||
|
||||
```java
|
||||
public interface OrderService {
|
||||
@ -64,20 +67,16 @@ public class OrderServiceImpl implements OrderService {
|
||||
return new Order(id, "new Product", new Date());
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
#### 2.2 创建自定义切面类
|
||||
### 2.2 自定义切面
|
||||
|
||||
```java
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 自定义切面
|
||||
*/
|
||||
public class CustomAdvice {
|
||||
|
||||
|
||||
//前置通知
|
||||
/**
|
||||
*前置通知
|
||||
*/
|
||||
public void before(JoinPoint joinPoint) {
|
||||
//获取节点名称
|
||||
String name = joinPoint.getSignature().getName();
|
||||
@ -85,12 +84,16 @@ public class CustomAdvice {
|
||||
System.out.println(name + "方法调用前:获取调用参数" + Arrays.toString(args));
|
||||
}
|
||||
|
||||
//后置通知 (抛出异常后不会被执行)
|
||||
/**
|
||||
*后置通知 (抛出异常后不会被执行)
|
||||
*/
|
||||
public void afterReturning(JoinPoint joinPoint, Object result) {
|
||||
System.out.println("后置返回通知结果" + result);
|
||||
}
|
||||
|
||||
//环绕通知
|
||||
/**
|
||||
*环绕通知
|
||||
*/
|
||||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
System.out.println("环绕通知-前");
|
||||
//调用目标方法
|
||||
@ -99,14 +102,16 @@ public class CustomAdvice {
|
||||
return proceed;
|
||||
}
|
||||
|
||||
//异常通知
|
||||
/**
|
||||
*异常通知
|
||||
*/
|
||||
public void afterException(JoinPoint joinPoint, Exception exception) {
|
||||
System.err.println("后置异常通知:" + exception);
|
||||
}
|
||||
};
|
||||
|
||||
;
|
||||
|
||||
// 后置通知 总会执行 但是不能访问到返回值
|
||||
/**
|
||||
*后置通知 总会执行 但是不能访问到返回值
|
||||
*/
|
||||
public void after(JoinPoint joinPoint) {
|
||||
System.out.println("后置通知");
|
||||
}
|
||||
@ -114,7 +119,7 @@ public class CustomAdvice {
|
||||
|
||||
```
|
||||
|
||||
#### 2.3 配置切面
|
||||
### 2.3 配置切面
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
@ -122,9 +127,9 @@ public class CustomAdvice {
|
||||
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">
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/aop
|
||||
http://www.springframework.org/schema/aop/spring-aop.xsd">
|
||||
|
||||
<!--开启后允许使用 Spring AOP 的@AspectJ 注解 如果是纯 xml 配置 可以不用开启这个声明-->
|
||||
<aop:aspectj-autoproxy/>
|
||||
@ -154,7 +159,7 @@ public class CustomAdvice {
|
||||
</beans>
|
||||
```
|
||||
|
||||
#### 2.4 测试切面
|
||||
### 2.4 测试切面
|
||||
|
||||
```java
|
||||
@RunWith(SpringRunner.class)
|
||||
@ -174,20 +179,20 @@ public class AopTest {
|
||||
|
||||
|
||||
|
||||
## 附: 关于切面表达式的说明
|
||||
## 三、切面表达式
|
||||
|
||||
切面表达式遵循以下格式:
|
||||
|
||||
```shell
|
||||
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
|
||||
throws-pattern?)
|
||||
throws-pattern?)
|
||||
```
|
||||
|
||||
- 除了返回类型模式,名字模式和参数模式以外,所有的部分都是可选的;
|
||||
- `*`,它代表了匹配任意的返回类型;
|
||||
- `()` 匹配了一个不接受任何参数的方法, 而 `(..)` 匹配了一个接受任意数量参数的方法(零或者更多)。 模式 `(*)` 匹配了一个接受一个任何类型的参数的方法。 模式 `(*,String)` 匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是 String 类型。
|
||||
- 除了返回类型模式,名字模式和参数模式以外,所有的部分都是可选的。
|
||||
- `*` 代表了匹配任意的返回类型。
|
||||
- `()` 匹配一个不接受任何参数的方法, `(..)` 匹配一个接受任意数量参数的方法(零或者更多)。 `(*)` 匹配一个接受任意类型的参数的方法。 模式 `(*,String)` 匹配了一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是 String 类型。
|
||||
|
||||
下面给出一些常见切入点表达式的例子。
|
||||
下面为一些常见切入点表达式:
|
||||
|
||||
- 任意公共方法的执行:
|
||||
|
||||
@ -195,25 +200,25 @@ execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-patter
|
||||
execution(public * *(..))
|
||||
```
|
||||
|
||||
- 任何一个以“set”开始的方法的执行:
|
||||
- 任何一个以 `set` 开头的方法的执行:
|
||||
|
||||
```java
|
||||
execution(* set*(..))
|
||||
```
|
||||
|
||||
- `AccountService` 接口的任意方法的执行:
|
||||
- `AccountService` 接口上任意方法的执行:
|
||||
|
||||
```java
|
||||
execution(* com.xyz.service.AccountService.*(..))
|
||||
```
|
||||
|
||||
- 定义在 service 包里的任意方法的执行:
|
||||
- 定义在 service 包里任意方法的执行:
|
||||
|
||||
```java
|
||||
execution(* com.xyz.service.*.*(..))
|
||||
```
|
||||
|
||||
- 定义在 service 包或者子包里的任意方法的执行:
|
||||
- 定义在 service 包或者子包里任意方法的执行:
|
||||
|
||||
```java
|
||||
execution(* com.xyz.service..*.*(..))
|
||||
|
Reference in New Issue
Block a user