更新aop
This commit is contained in:
parent
ffc27cd535
commit
97901f14b8
116
java/spring aop 二.md
Normal file
116
java/spring aop 二.md
Normal file
@ -0,0 +1,116 @@
|
||||
### 切入点表达式
|
||||
作用 用来匹配切入的方法。
|
||||
|
||||
execution(切入点表达式 方法返回值 )
|
||||
|
||||
execution([方法修饰符] 方法返回类型 全类名.方法名(参数类型))
|
||||
|
||||
**通配符 ***
|
||||
|
||||
- 匹配方法返回值
|
||||
- 匹配方法的包名
|
||||
- 匹配方法的类名
|
||||
- 匹配方法的方法名
|
||||
- 匹配方法的参数类型
|
||||
|
||||
**通配符** `..` 标识任意多个
|
||||
|
||||
- 匹配任意层级的包
|
||||
|
||||
- 匹配任意个数 任意类型的参数
|
||||
|
||||
例子:
|
||||
|
||||
- 标识 cn.x47包下的任意子包的所有方法,任意参数
|
||||
|
||||
```xml
|
||||
<aop:beforemethod="printOne"pointcut="execution(public * cn.x47..*(..))"/>
|
||||
```
|
||||
|
||||
+ 匹配任意层级的TargetObject对象
|
||||
~~~xml
|
||||
<aop:beforemethod="printOne"pointcut="execution(public void ..TargetObject.print2())"/>
|
||||
~~~
|
||||
|
||||
+ 匹配任意个数 任意类型的参数
|
||||
~~~xml
|
||||
<aop:beforemethod="printOne"pointcut="execution(public void org.example.TargetObject.print2(..))"/>
|
||||
~~~
|
||||
+ 匹配以Object结尾的对象 2结尾 任意参数类型 的方法
|
||||
~~~xml
|
||||
<aop:beforemethod="printOne"pointcut="execution(public void org.example.*Object.*2(..))"/>
|
||||
~~~
|
||||
|
||||
**切入点配置方式**
|
||||
|
||||
+ 局部切入点
|
||||
+ 切面间共享切入点
|
||||
+ 将切入点表达式写在切面配置外面
|
||||
+ 例子:
|
||||
~~~xml
|
||||
<aop:config>
|
||||
<aop:pointcut id="abc" expression="execution(
|
||||
public void org.example.TargetObject.print2())"/>
|
||||
<aop:aspect ref="advice">
|
||||
<aop:before method="printOne" pointcut-ref="abc"/>
|
||||
</aop:aspect>
|
||||
</aop:config>
|
||||
~~~
|
||||
|
||||
+ 切面内共享切入点
|
||||
+ 将切入点配置在切面里 切入点外
|
||||
+ 例子:
|
||||
~~~xml
|
||||
<aop:config>
|
||||
<aop:aspect ref="advice">
|
||||
<aop:pointcut id="abc" expression="execution(
|
||||
public void org.example.TargetObject.print2())"/>
|
||||
<aop:before method="printOne" pointcut-ref="abc"/>
|
||||
</aop:aspect>
|
||||
</aop:config>
|
||||
~~~
|
||||
|
||||
**切面执行顺序**
|
||||
|
||||
+ 同一切面
|
||||
+ 先配置先执行
|
||||
+ 不同切面
|
||||
+ 前置 前配置先执行
|
||||
+ 后置 先配置后执行 后配置先执行
|
||||
|
||||
**通知获取切入点参数**
|
||||
|
||||
+ 通过 JoinPoint 对象获取
|
||||
~~~xml
|
||||
public void printOne(JoinPoint jp) {
|
||||
Object[] args = jp.getArgs();
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
Object arg = args[i];
|
||||
}
|
||||
|
||||
}
|
||||
~~~
|
||||
|
||||
+ 通过切入点指定参数类型 参数形参
|
||||
|
||||
```xml
|
||||
//切入点配置
|
||||
<aop:before method="printOne" pointcut="execution(* org.example.TargetObject.print2(String)) && args(a)"/>
|
||||
```
|
||||
|
||||
```java
|
||||
//通知
|
||||
public void printOne(String a) {
|
||||
System.out.println("我是参数" + a);
|
||||
System.out.println("我是通知");
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
**注解配置**
|
||||
|
||||
执行优先级
|
||||
|
||||
环绕在前 前置后置 在后 返回最后
|
204
java/spring aop.md
Normal file
204
java/spring aop.md
Normal file
@ -0,0 +1,204 @@
|
||||
## spring aop 面向切面编程
|
||||
|
||||
**连接点**
|
||||
|
||||
泛指类中的方法
|
||||
|
||||
**切入点**
|
||||
|
||||
被抽取了共同代码的逻辑方法
|
||||
|
||||
切入点一定是连接点 连接点不一定是切入点
|
||||
|
||||
**通知**
|
||||
|
||||
被抽取出来的 共同代码 可以认定为是一个方法
|
||||
|
||||
通知 按照被抽取的代码位置 分为
|
||||
|
||||
1. 前置通知(Before advice) 在逻辑代码前面执行
|
||||
2. 后置通知(After (finally) advice) 在逻辑代码后面执行,不管逻辑代码是否正常执行
|
||||
3. 返回通知(After returning advice) 在逻辑代码,正常完成后执行的通知
|
||||
4. 异常通知(After throwing advice) 抛出异常后通知
|
||||
5. 环绕通知(Around Advice) 在逻辑代码前后都执行(执行两次)
|
||||
|
||||
**引入**
|
||||
|
||||
泛指 类中的共同的成员变量 多个通知要使用
|
||||
|
||||
引入 机制可以增加额外的变量
|
||||
|
||||
引入机制是在类的编译器 或者类加载期完成
|
||||
|
||||
**目标对象**
|
||||
|
||||
原始的对象抽离出共同代码后的对象
|
||||
|
||||
缺少完整逻辑代码的对象
|
||||
|
||||
**代理对象**
|
||||
|
||||
Spring 为目标对象注入通知 成为一个可以正常执行的对象
|
||||
|
||||
**织入**
|
||||
|
||||
将目标对象代理并注入通知的过程
|
||||
|
||||
**切面**
|
||||
|
||||
切面是一个设计概念,指切入点与通知的匹配模式
|
||||
|
||||
程序设计视可以设置多个切面,用来描述切入点与通知之间的关系。
|
||||
|
||||
|
||||
|
||||
## 代码
|
||||
### pom 文件
|
||||
|
||||
~~~xml
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
|
||||
|
||||
<spring.version>5.2.8.RELEASE</spring.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aop</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aspects</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>1.6.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.6.11</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
~~~
|
||||
|
||||
### applicationContext.xml
|
||||
|
||||
~~~xml
|
||||
<?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">
|
||||
<aop:config>
|
||||
<aop:aspect ref="advice">
|
||||
<aop:before method="printOne" pointcut="execution(public void org.example.TargetObject.print2())"/>
|
||||
</aop:aspect>
|
||||
</aop:config>
|
||||
<bean id="target" class="org.example.TargetObject"></bean>
|
||||
<bean id="advice" class="org.example.Advice"></bean>
|
||||
</beans>
|
||||
|
||||
~~~
|
||||
|
||||
### 目标对象
|
||||
~~~java
|
||||
|
||||
package org.example;
|
||||
|
||||
/**
|
||||
* @author by xKing
|
||||
* @create on 2020/10/30 21:21
|
||||
*/
|
||||
public class TargetObject {
|
||||
public void print2() {
|
||||
System.out.println("我是目标对象");
|
||||
System.out.println("3");
|
||||
}
|
||||
|
||||
|
||||
public void print3() {
|
||||
System.out.println("2");
|
||||
System.out.println("3");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
~~~
|
||||
|
||||
|
||||
### 通知对象
|
||||
~~~java
|
||||
package org.example;
|
||||
|
||||
/**
|
||||
* @author by xKing
|
||||
* @create on 2020/10/30 21:22
|
||||
*/
|
||||
public class Advice {
|
||||
public void printOne() {
|
||||
System.out.println("我是通知");
|
||||
}
|
||||
}
|
||||
|
||||
~~~
|
||||
|
||||
### 测试类
|
||||
~~~java
|
||||
|
||||
package org.example;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*/
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
ClassPathXmlApplicationContext cat = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
Object target = cat.getBean("target");
|
||||
((TargetObject) target).print2();
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
~~~
|
||||
|
||||
|
||||
|
||||
### 输出结果
|
||||
|
||||
~~~sh
|
||||
1
|
||||
2
|
||||
3
|
||||
Hello World!
|
||||
|
||||
Process finished with exit code 0
|
||||
|
||||
|
||||
~~~
|
Loading…
x
Reference in New Issue
Block a user