• java
  • go
  • 数据库
  • linux
  • 中间件
  • 书
  • 源码
  • 夕拾

  • java
  • go
  • 数据库
  • linux
  • 中间件
  • 书
  • 源码
  • 夕拾

aop流程

目录

  • 目录
  • 简介
  • 要明确的基本概念
  • 应用场景
  • 使用
    • pointCut
  • aop
    • defaultAdvisorAutoProxyCreator

简介

  • spring aop 基于动态代理,如果使用接口的,利用jdk提供的动态代理,没有接口的利用CGLIB实现.spring-core中把CGLIB和ASM的源码包含进来了,所以不用引包
  • AOP依赖与IOC,在IOC启动的时候会创建代理实例,因此Spring中的AOP也只能作用于托管至IOC中的bean.

目标对象创建代理对象(jdk或者cglib)。然后启用代理对象的拦截器来完成各种切面的织入(在调用目标对象方法时会触发方法增强)。
这一系列的织入是通过Adapter来实现的。
通过Adapter把切面和Proxy有机的结合在一起。
AOP切面是Spring中对分散在类中公共方法的解决方案,Proxy代理是对此方案的技术实现。

要明确的基本概念

  1. advice 通知: 定义在连接点做什么,为切面增强提供织入接口。在SpringAOP中,他主要描述SpringAOP围绕方法调用而注入的切面行为。如:BeforeAdvice,AfterAdvice,ThrowsAdvice。
  2. pointcut 切点: 决定Advice通知应该作用于哪些连接点。通过PointCut来定义需要增强的方法集合。
  3. advisor 通知器: 通过Advisor,可以定义应用应该使用哪个通知并在哪个关注点使用它,通过Advisor,把Advice和PointCut结合起来。

应用场景

  1. AOP本身的功能: 把横跨多个模块的功能抽成一个切面.
  2. spring内部一些模块也通过AOP实现,如事务.

使用

pointCut

匹配规则:
下列匹配模式中,

  1. “.” 代表一个包名
  2. “..” 代表包及其子包
  3. 方法参数任意匹配使用两个点 “..”。
    1
    2
    3
    4
    5
    6
    7
    8
    // 正则
    @Pointcut("execution(* transfer(..))")
    // 所在类或包内
    @Pointcut("within(com.javadoop.springaoplearning.service..*)")
    // @annotation:方法上具有特定的注解,如 @Subscribe 用于订阅特定的事件。
    @Pointcut("execution(* .(..)) && @annotation(com.javadoop.annotation.Subscribe)")
    // 匹配 bean 的名字
    @Pointcut("bean(*Service)")

    aop

    AOP中,DefaultAdvisorAutoProxyCreator 类了,实现自动将所有的 advisor 生效。

重要的类

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
/**
* Auto-proxy creator that considers infrastructure Advisor beans only,
* ignoring any application-defined Advisors.
*/
InfrastructureAdvisorAutoProxyCreator

/**
* Create a proxy with the configured interceptors if the bean is
* identified as one to proxy by the subclass.
* @see #getAdvicesAndAdvisorsForBean
*/
@Override
public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
if (bean != null) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (this.earlyProxyReferences.remove(cacheKey) != bean) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}

/**
* Wrap the given bean if necessary, i.e. if it is eligible for being proxied.
* @param bean the raw bean instance
* @param beanName the name of the bean
* @param cacheKey the cache key for metadata access
* @return a proxy wrapping the bean, or the raw bean instance as-is
*/
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
return bean;
}
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
return bean;
}
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}

// Create proxy if we have advice.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
Object proxy = createProxy(
bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}

this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}

bean-post-processor

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
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
return bean;
}
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
return bean;
}
// 是否为aop本身系统类
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}

// Create proxy if we have advice.
// 获取advisor
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
// 创建代理
Object proxy = createProxy(
bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}

this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}

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

@Override
@Nullable
protected Object[] getAdvicesAndAdvisorsForBean(
Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) {

List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName);
if (advisors.isEmpty()) {
return DO_NOT_PROXY;
}
return advisors.toArray();
}


/**
* Find all eligible Advisors for auto-proxying this class.
* @param beanClass the clazz to find advisors for
* @param beanName the name of the currently proxied bean
* @return the empty List, not {@code null},
* if there are no pointcuts or interceptors
* @see #findCandidateAdvisors
* @see #sortAdvisors
* @see #extendAdvisors
*/
protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
// 获取容器的全部advisor
// 先从缓存中取,没有执行获取逻辑
// 1. 获取所有类型为Advisor的Bean的名称
// 2. 根据beanName获取Bean
List<Advisor> candidateAdvisors = findCandidateAdvisors();
// 筛选
// 1. 先筛选出IntroductionAdvisor(类级别),再选其他
List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
extendAdvisors(eligibleAdvisors);
if (!eligibleAdvisors.isEmpty()) {
eligibleAdvisors = sortAdvisors(eligibleAdvisors);
}
return eligibleAdvisors;
}

织入时期

1
2
3
4
5
6
7
/**
getBean()
--> createBean()
--> doCreateBean()
--> createBeanInstance()
--> instantiateBean()
*/
Mysql 索引
ConcurrentHashMap
  1. 1. 目录
  2. 2. 简介
  3. 3. 要明确的基本概念
  4. 4. 应用场景
  5. 5. 使用
    1. 5.1. pointCut
  6. 6. aop
    1. 6.1. 重要的类
    2. 6.2. bean-post-processor
    3. 6.3. 织入时期
© 2023 haoxp
Hexo theme